Lunchbox  1.13.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
buffer.ipp
1 
2 /* Copyright (c) 2007-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2011-2012, Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 namespace lunchbox
20 {
21 template< class T > Buffer< T >::Buffer( Buffer< T >& from )
22 {
23  _data = from._data; _size = from._size; _maxSize = from._maxSize;
24  from._data = 0; from._size = 0; from._maxSize = 0;
25 }
26 
27 template< class T > T* Buffer< T >::pack()
28 {
29  if( _maxSize != _size )
30  {
31  _data = static_cast< T* >( realloc( _data, _size * sizeof( T )));
32  _maxSize = _size;
33  }
34  return _data;
35 }
36 
37 template< class T >
39 {
40  replace( from );
41  return *this;
42 }
43 
44 template< class T > T* Buffer< T >::resize( const uint64_t newSize )
45 {
46  _size = newSize;
47  if( newSize <= _maxSize )
48  return _data;
49 
50  // avoid excessive reallocs
51  const uint64_t nElems = newSize + (newSize >> 3);
52  const uint64_t nBytes = nElems * sizeof( T );
53  _data = static_cast< T* >( realloc( _data, nBytes ));
54  _maxSize = nElems;
55  return _data;
56 }
57 
58 template< class T > void Buffer< T >::grow( const uint64_t newSize )
59 {
60  if( newSize > _size )
61  resize( newSize );
62 }
63 
64 template< class T > T* Buffer< T >::reserve( const uint64_t newSize )
65 {
66  if( newSize <= _maxSize )
67  return _data;
68 
69  _data = static_cast< T* >( realloc( _data, newSize * sizeof( T )));
70  _maxSize = newSize;
71  return _data;
72 }
73 
74 template< class T > T* Buffer< T >::reset( const uint64_t newSize )
75 {
76  reserve( newSize );
77  setSize( newSize );
78  return _data;
79 }
80 
81 template< class T >
82 void Buffer< T >::append( const T* data, const uint64_t size )
83 {
84  LBASSERT( data );
85  LBASSERT( size );
86 
87  const uint64_t oldSize = _size;
88  resize( oldSize + size );
89  memcpy( _data + oldSize, data, size * sizeof( T ));
90 }
91 
92 template< class T > void Buffer< T >::append( const T& element )
93 {
94  resize( _size + 1 );
95  _data[ _size - 1 ] = element;
96 }
97 
98 template< class T >
99 void Buffer< T >::replace( const void* data, const uint64_t size )
100 {
101  LBASSERT( data );
102  LBASSERT( size );
103 
104  reserve( size );
105  memcpy( _data, data, size * sizeof( T ));
106  _size = size;
107 }
108 
109 template< class T > void Buffer< T >::swap( Buffer< T >& buffer )
110 {
111  T* tmpData = buffer._data;
112  const uint64_t tmpSize = buffer._size;
113  const uint64_t tmpMaxSize = buffer._maxSize;
114 
115  buffer._data = _data;
116  buffer._size = _size;
117  buffer._maxSize = _maxSize;
118 
119  _data = tmpData;
120  _size = tmpSize;
121  _maxSize = tmpMaxSize;
122 }
123 
124 template< class T > bool Buffer< T >::setSize( const uint64_t size )
125 {
126  LBASSERT( size <= _maxSize );
127  if( size > _maxSize )
128  return false;
129 
130  _size = size;
131  return true;
132 }
133 }
T * reserve(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:64
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
Definition: buffer.ipp:38
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
Definition: buffer.ipp:82
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
Definition: buffer.ipp:124
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
Definition: buffer.ipp:109
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:58
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
T * resize(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:44
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
Definition: buffer.ipp:99
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Definition: buffer.ipp:74
Buffer()
Construct a new, empty buffer.
Definition: buffer.h:47
A simple memory buffer with some helper functions.
Definition: buffer.h:43
T * pack()
Tighten the allocated memory to the size of the buffer.
Definition: buffer.ipp:27