/*  $Id: netpacket_generic.cpp,v 1.4 2003/07/20 22:34:40 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "netpacket_generic.h"
#include <string.h>

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket_Generic construction:

CL_NetPacket_Generic::CL_NetPacket_Generic()
: data(0), size(0), allocated_size(0), ref_count(0)
{
}

CL_NetPacket_Generic::~CL_NetPacket_Generic()
{
	delete[] data;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket_Generic attributes:


/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket_Generic operations:

int CL_NetPacket_Generic::add_ref()
{
	return ++ref_count;
}

int CL_NetPacket_Generic::release_ref()
{
	ref_count--;
	if (ref_count > 0) return ref_count;

	delete this;
	return 0;
}

#define cl_min(a, b) ((a < b) ? a : b)

void CL_NetPacket_Generic::resize(int new_size)
{
	if (new_size <= allocated_size)
	{
		size = new_size;
		return;
	}

	int new_allocated_size = new_size + 8*1024;
	unsigned char *old_data = data;
	data = new unsigned char[new_allocated_size];
	memcpy(data, old_data, cl_min(size, new_size));
	delete[] old_data;
	allocated_size = new_allocated_size;
	size = new_size;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket_Generic implementation:
