23 _data = from._data; _size = from._size; _maxSize = from._maxSize;
24 from._data = 0; from._size = 0; from._maxSize = 0;
29 if( _maxSize != _size )
31 _data =
static_cast< T*
>( realloc( _data, _size *
sizeof( T )));
47 if( newSize <= _maxSize )
51 const uint64_t nElems = newSize + (newSize >> 3);
52 const uint64_t nBytes = nElems *
sizeof( T );
53 _data =
static_cast< T*
>( realloc( _data, nBytes ));
66 if( newSize <= _maxSize )
69 _data =
static_cast< T*
>( realloc( _data, newSize *
sizeof( T )));
87 const uint64_t oldSize = _size;
88 resize( oldSize + size );
89 memcpy( _data + oldSize, data, size *
sizeof( T ));
95 _data[ _size - 1 ] = element;
105 memcpy( _data, data, size *
sizeof( T ));
111 T* tmpData = buffer._data;
112 const uint64_t tmpSize = buffer._size;
113 const uint64_t tmpMaxSize = buffer._maxSize;
115 buffer._data = _data;
116 buffer._size = _size;
117 buffer._maxSize = _maxSize;
121 _maxSize = tmpMaxSize;
126 LBASSERT( size <= _maxSize );
127 if( size > _maxSize )
T * reserve(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
T * resize(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Buffer()
Construct a new, empty buffer.
A simple memory buffer with some helper functions.
T * pack()
Tighten the allocated memory to the size of the buffer.