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