File: buffer.cpp

package info (click to toggle)
libwibble 0.1.9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 500 kB
  • ctags: 1,183
  • sloc: cpp: 5,760; sh: 113; makefile: 71
file content (80 lines) | stat: -rw-r--r-- 1,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Variable-size, reference-counted memory buffer
 *
 * Copyright (C) 2003--2006  Enrico Zini <enrico@debian.org>
 *
 * 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 <wibble/sys/buffer.h>

#include <string.h>	// memcpy
#include <stdlib.h>	// malloc, free, realloc

namespace wibble {
namespace sys {

Buffer::Data::Data(size_t size) throw () : _ref(0), _size(size)
{
	_data = malloc(size);
}

Buffer::Data::Data(void* buf, size_t size, bool own) throw ()
	: _ref(0), _size(size)
{
	if (own)
		_data = buf;
	else
	{
		_data = malloc(size);
		memcpy(_data, buf, size);
	}
}

Buffer::Data::Data(const void* buf, size_t size) throw ()
	: _ref(0), _size(size)
{
	_data = malloc(size);
	memcpy(_data, buf, size);
}

Buffer::Data::~Data() throw ()
{
	if (_data)
		free(_data);
}
	

void Buffer::Data::resize(size_t size) throw ()
{
	if (size == 0)
	{
		if (_data)
		{
			free(_data);
			_data = 0;
		}
	} else if (_data == 0) {
		_data = malloc(size);
	} else {
		_data = realloc(_data, size);
	}
	_size = size;
}

}
}

// vim:set ts=4 sw=4: