Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
buffer.ipp
1 
2 /* Copyright (c) 2007-2016, Stefan Eilemann <eile@equalizergraphics.com>
3  * 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( const Buffer< T >& from )
22  : _data( nullptr )
23  , _size( 0 )
24  , _maxSize( 0 )
25 {
26  *this = from;
27 }
28 
29 template< class T > Buffer< T >::Buffer( Buffer< T >&& from )
30  : _data( std::move( from._data ))
31  , _size( from._size )
32  , _maxSize( from._maxSize )
33 {
34  from._data = nullptr;
35  from._size = 0;
36  from._maxSize = 0;
37 }
38 
39 template< class T > T* Buffer< T >::pack()
40 {
41  if( _maxSize != _size )
42  {
43  _data = static_cast< T* >( realloc( _data, _size * sizeof( T )));
44  _maxSize = _size;
45  }
46  return _data;
47 }
48 
49 template< class T >
51 {
52  if( this != &from )
53  replace( from );
54  return *this;
55 }
56 
57 template< class T >
59 {
60  if( this == &from )
61  return *this;
62  std::swap( _data, from._data );
63  std::swap( _size, from._size );
64  std::swap( _maxSize, from._maxSize );
65  return *this;
66 }
67 
68 template< class T > T* Buffer< T >::resize( const uint64_t newSize )
69 {
70  _size = newSize;
71  if( newSize <= _maxSize )
72  return _data;
73 
74  // avoid excessive reallocs
75  const uint64_t nElems = newSize + (newSize >> 3);
76  const uint64_t nBytes = nElems * sizeof( T );
77  _data = static_cast< T* >( realloc( _data, nBytes ));
78  _maxSize = nElems;
79  return _data;
80 }
81 
82 template< class T > void Buffer< T >::grow( const uint64_t newSize )
83 {
84  if( newSize > _size )
85  resize( newSize );
86 }
87 
88 template< class T > T* Buffer< T >::reserve( const uint64_t newSize )
89 {
90  if( newSize <= _maxSize )
91  return _data;
92 
93  _data = static_cast< T* >( realloc( _data, newSize * sizeof( T )));
94  _maxSize = newSize;
95  return _data;
96 }
97 
98 template< class T > T* Buffer< T >::reset( const uint64_t newSize )
99 {
100  reserve( newSize );
101  setSize( newSize );
102  return _data;
103 }
104 
105 template< class T >
106 void Buffer< T >::append( const T* data, const uint64_t size )
107 {
108  LBASSERT( data );
109  LBASSERT( size );
110 
111  const uint64_t oldSize = _size;
112  resize( oldSize + size );
113  memcpy( _data + oldSize, data, size * sizeof( T ));
114 }
115 
116 template< class T > void Buffer< T >::append( const T& element )
117 {
118  append( &element, 1 );
119 }
120 
121 template< class T >
122 void Buffer< T >::replace( const void* data, const uint64_t size )
123 {
124  LBASSERT( data );
125  LBASSERT( size );
126 
127  reserve( size );
128  memcpy( _data, data, size * sizeof( T ));
129  _size = size;
130 }
131 
132 template< class T > void Buffer< T >::swap( Buffer< T >& buffer )
133 {
134  T* tmpData = buffer._data;
135  const uint64_t tmpSize = buffer._size;
136  const uint64_t tmpMaxSize = buffer._maxSize;
137 
138  buffer._data = _data;
139  buffer._size = _size;
140  buffer._maxSize = _maxSize;
141 
142  _data = tmpData;
143  _size = tmpSize;
144  _maxSize = tmpMaxSize;
145 }
146 
147 template< class T > bool Buffer< T >::setSize( const uint64_t size )
148 {
149  LBASSERT( size <= _maxSize );
150  if( size > _maxSize )
151  return false;
152 
153  _size = size;
154  return true;
155 }
156 }
T * reserve(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:88
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
Definition: buffer.ipp:50
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
Definition: buffer.ipp:106
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
Definition: buffer.ipp:147
STL namespace.
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
Definition: buffer.ipp:132
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:82
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:68
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
Definition: buffer.ipp:122
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Definition: buffer.ipp:98
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:39