VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE > Class Template Reference

Basically a bad std::vector without exceptions which can also work with foreign memory. More...

#include <THeapBuffer.hpp>

Inheritance diagram for tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >:
Collaboration diagram for tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >:

Public Types

using Size = SIZE
 everything using the heapbuffer uses this type Always needs to be unsigned! More...
 

Public Member Functions

 HeapBuffer ()=default
 
 HeapBuffer (const Size size)
 Setup the buffer with a size. More...
 
template<int Alignment2, class Allocator2 , typename Size2 >
 HeapBuffer (const HeapBuffer< T, Alignment2, Allocator2, Size2 > &source)
 Copy Constructor, calls set() Failed allocations have to be checked. More...
 
 HeapBuffer (const T *data, Size size)
 
 HeapBuffer (HeapBuffer &&source)
 Move contructor, will mark the original buffer as injected. More...
 
HeapBufferoperator= (HeapBuffer &&source)
 
HeapBufferoperator= (const HeapBuffer &)=delete
 
 HeapBuffer (const HeapBuffer *)=delete
 
 ~HeapBuffer ()
 
template<int Alignment2, class Allocator2 , typename Size2 >
bool set (const HeapBuffer< T, Alignment2, Allocator2, Size2 > &source)
 Resizes and copies the contents of the source Buffer This will do a memory::copy,so none of the object contructors will called. More...
 
bool set (const T *data, Size size)
 Set data from array. More...
 
void inject (T *mem, const Size size, const Size realSize=0)
 Provide foreign memory to borrow. More...
 
void inject (const T *mem, const Size size, const Size realSize=0)
 Provide const foreign memory to use Using non const accessors will cause assertions in debug mode. More...
 
void disown ()
 The memory is no longer manager by this instance. More...
 
bool empty () const
 
T * data ()
 
const T * data () const
 
const T & operator[] (const Size index) const
 
T & operator[] (const Size index)
 
const T * begin () const
 
const T * end () const
 
T * begin ()
 
T * end ()
 
T & last ()
 
T & first ()
 
bool reserve (const Size size)
 Will make sure the desired space is allocated. More...
 
bool resize (const Size size, const bool downsize=true)
 Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon as an allocation happens. More...
 
bool push (const T &object)
 Push the object to the back of the buffer TODO tklb move version. More...
 
bool pop (T *object)
 Get the last element in the buffer. More...
 
bool remove (const Size index)
 Removes the object at a given index and fills the gap with another Will never shrink the buffer/allocate. More...
 
bool remove (const T &object)
 If a == comparisin is possible the object itself can be used. More...
 
bool remove (const T *object)
 Otherwise use memory location. More...
 
bool destroyPointers ()
 If T is a pointer type, delete will be called for all pointers in buffer and resize(0) is called. More...
 
void clear ()
 Clears the buffer but doesn't free the memory. More...
 
bool injected () const
 Whether the memory is managed by the instance or is borrowed. More...
 
Size size () const
 Returns the amount of elements in the container. More...
 
Size reserved () const
 Returns the real allocated size in elements. More...
 
Size allocated () const
 Returns the size of the allocated space. More...
 

Static Public Member Functions

static Size closestChunkSize (Size size, Size chunk)
 
static bool isAligned (const void *ptr)
 

Static Public Attributes

static constexpr size_t Alignment = ALIGNMENT
 
static constexpr size_t ChunkSize = 16
 

Private Member Functions

bool allocate (Size chunk) noexcept
 Allocated the exact size requsted and copies existing objects. More...
 

Private Attributes

ALLOCATOR mAllocator
 
T * mBuf = nullptr
 
Size mSize = 0
 
Size mRealSize = 0
 the actually allocated size if it's 0 the memory is not actually owned by the buffer and won't be delete with the object. More...
 

Detailed Description

template<typename T, size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
class tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >

Basically a bad std::vector without exceptions which can also work with foreign memory.

Template Parameters
TElement type need to have a default contructor
ALIGNMENTMemory alignment in bytes needs to be a power of 2. Defaults to 0
ALLOCATORNormal allocator class defaults to non basic malloc/free wrapper
SIZESize type used for index, defaults to unsigned int to save some space

Definition at line 49 of file THeapBuffer.hpp.

Member Typedef Documentation

◆ Size

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
using tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::Size = SIZE

everything using the heapbuffer uses this type Always needs to be unsigned!

Definition at line 55 of file THeapBuffer.hpp.

Constructor & Destructor Documentation

◆ HeapBuffer() [1/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( )
default

◆ HeapBuffer() [2/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( const Size  size)
inline

Setup the buffer with a size.

User has to check if allocation was successful.

Parameters
sizeSize in elements of the buffer

Definition at line 83 of file THeapBuffer.hpp.

83 {
84 if (size != 0) { resize(size); }
85 }
Size size() const
Returns the amount of elements in the container.
bool resize(const Size size, const bool downsize=true)
Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon ...
Here is the call graph for this function:

◆ HeapBuffer() [3/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
template<int Alignment2, class Allocator2 , typename Size2 >
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( const HeapBuffer< T, Alignment2, Allocator2, Size2 > &  source)
inline

Copy Constructor, calls set() Failed allocations have to be checked.

Definition at line 92 of file THeapBuffer.hpp.

92{ set(source); }
bool set(const HeapBuffer< T, Alignment2, Allocator2, Size2 > &source)
Resizes and copies the contents of the source Buffer This will do a memory::copy,so none of the objec...
Here is the call graph for this function:

◆ HeapBuffer() [4/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( const T *  data,
Size  size 
)
inline

Definition at line 94 of file THeapBuffer.hpp.

94{ set(data, size); }
Here is the call graph for this function:

◆ HeapBuffer() [5/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE > &&  source)
inline

Move contructor, will mark the original buffer as injected.

Definition at line 99 of file THeapBuffer.hpp.

99 :
100 mBuf(source.mBuf),
101 mSize(source.mSize),
102 mRealSize(source.mRealSize)
103 {
104 TKLB_ASSERT_STATE(IS_CONST = source.IS_CONST)
105 source.disown();
106 }
#define TKLB_ASSERT_STATE(condition)
This can be used to write some additional code only needed when doing assertions.
Definition: TAssert.h:32
Size mRealSize
the actually allocated size if it's 0 the memory is not actually owned by the buffer and won't be del...
Definition: THeapBuffer.hpp:72
void disown()
The memory is no longer manager by this instance.

◆ HeapBuffer() [6/6]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::HeapBuffer ( const HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE > *  )
delete

◆ ~HeapBuffer()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::~HeapBuffer ( )
inline

Definition at line 120 of file THeapBuffer.hpp.

120 {
121 if (injected()) { return; }
122 resize(0);
123 }
bool injected() const
Whether the memory is managed by the instance or is borrowed.
Here is the call graph for this function:

Member Function Documentation

◆ allocate()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::allocate ( Size  chunk)
inlineprivatenoexcept

Allocated the exact size requsted and copies existing objects.

Will not call their destructors or constructors!

Definition at line 401 of file THeapBuffer.hpp.

401 {
402 T* oldBuf = mBuf;
403 if (0 < chunk) {
404 const Size bytes = chunk * sizeof(T);
405 // TODO tklb Consider using realloc
406 void* newBuf = mAllocator.allocate(bytes + Alignment);
407 if (newBuf == nullptr) {
408 TKLB_ASSERT(false);
409 return false;
410 }
411
412 if (Alignment != 0) {
413 // Mask with zeroes at the end to floor the pointer to an aligned block
414 // for these casts to work, the type needs to be as wide as void*
415 const size_t _align = Alignment;
416 const size_t mask = ~(size_t(Alignment - 1));
417 const size_t pointer = reinterpret_cast<size_t>(newBuf);
418 const size_t floored = pointer & mask;
419 const size_t aligned = floored + Alignment;
420
421 // Not enough space before aligned memory to store original ptr
422 // This only happens when malloc doesn't align to sizeof(size_t)
423 TKLB_ASSERT(sizeof(size_t) <= (aligned - pointer))
424
425 newBuf = reinterpret_cast<void*>(aligned);
426 size_t* original = reinterpret_cast<size_t*>(newBuf) - 1;
427 *(original) = pointer;
428 TKLB_ASSERT(isAligned(newBuf))
429 }
430
431
432 if (0 < mSize && oldBuf != nullptr && newBuf != nullptr) {
433 // copy existing content
434 // TODO tklb only copy the new realsize which might be smaller
435 memcpy(newBuf, oldBuf, std::min(mSize, chunk) * sizeof(T));
436 }
437 mBuf = (T*) newBuf;
438 } else {
439 mBuf = nullptr;
440 }
441
442 if (oldBuf != nullptr && !injected() && mRealSize > 0) {
443 // Get rif of oldbuffer, object destructors were already called
444 if (Alignment == 0) {
445 mAllocator.deallocate(reinterpret_cast<unsigned char*>(oldBuf), mRealSize);
446 } else {
447 auto original = *(reinterpret_cast<void**>(oldBuf) - 1);
448 mAllocator.deallocate(reinterpret_cast<unsigned char*>(original), mRealSize);
449 }
450 }
451 TKLB_ASSERT_STATE(IS_CONST = false)
452 mRealSize = chunk;
453 // TKLB_CHECK_HEAP()
454 return true;
455 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
static constexpr size_t Alignment
Definition: THeapBuffer.hpp:57
static bool isAligned(const void *ptr)
SIZE Size
everything using the heapbuffer uses this type Always needs to be unsigned!
Definition: THeapBuffer.hpp:55
ALLOCATOR mAllocator
Definition: THeapBuffer.hpp:62
T min(const T &v1, const T &v2)
Definition: TMath.hpp:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ allocated()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::allocated ( ) const
inline

Returns the size of the allocated space.

Definition at line 384 of file THeapBuffer.hpp.

384{ return mRealSize * sizeof(T); }

◆ begin() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::begin ( )
inline

Definition at line 220 of file THeapBuffer.hpp.

220{ return mBuf; }

◆ begin() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
const T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::begin ( ) const
inline

Definition at line 217 of file THeapBuffer.hpp.

217{ return mBuf; }

◆ clear()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
void tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::clear ( )
inline

Clears the buffer but doesn't free the memory.

Definition at line 364 of file THeapBuffer.hpp.

364{ resize(0, false); }
Here is the call graph for this function:

◆ closestChunkSize()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
static Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::closestChunkSize ( Size  size,
Size  chunk 
)
inlinestatic

Definition at line 386 of file THeapBuffer.hpp.

386 {
387 const double chunkMin = std::max(double(chunk), 1.0);
388 return chunk * Size(std::ceil(size / double(chunkMin)));
389 }
T max(const T &v1, const T &v2)
Definition: TMath.hpp:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::data ( )
inline

Definition at line 193 of file THeapBuffer.hpp.

193 {
194 // Don't use non const access when using injected const memory
195 TKLB_ASSERT(!IS_CONST)
196 return mBuf;
197 }
Here is the caller graph for this function:

◆ data() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
const T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::data ( ) const
inline

Definition at line 199 of file THeapBuffer.hpp.

199{ return mBuf; }

◆ destroyPointers()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::destroyPointers ( )
inline

If T is a pointer type, delete will be called for all pointers in buffer and resize(0) is called.

Definition at line 349 of file THeapBuffer.hpp.

349 {
350 if (!std::is_pointer<T>::value) {
351 TKLB_ASSERT(false)
352 return false;
353 }
354 for (Size i = 0; i < mSize; i++) {
355 delete mBuf[i];
356 }
357 resize(0);
358 return true;
359 }
Here is the call graph for this function:

◆ disown()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
void tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::disown ( )
inline

The memory is no longer manager by this instance.

Leak prone.

Definition at line 185 of file THeapBuffer.hpp.

185 {
186 if (empty()) { return; }
188 mRealSize = 0;
189 }
bool empty() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ empty()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::empty ( ) const
inline

Definition at line 191 of file THeapBuffer.hpp.

191{ return mSize == 0; }
Here is the caller graph for this function:

◆ end() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::end ( )
inline

Definition at line 221 of file THeapBuffer.hpp.

221{ return mBuf + mSize; }

◆ end() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
const T * tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::end ( ) const
inline

Definition at line 218 of file THeapBuffer.hpp.

218{ return mBuf + mSize; }

◆ first()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::first ( )
inline

Definition at line 228 of file THeapBuffer.hpp.

228 {
229 TKLB_ASSERT(0 < mSize)
230 return mBuf[0];
231 }

◆ inject() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
void tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::inject ( const T *  mem,
const Size  size,
const Size  realSize = 0 
)
inline

Provide const foreign memory to use Using non const accessors will cause assertions in debug mode.

Parameters
memNon modifyable memory to use
sizeSize of the memory in elements
realSizeThe actual size if it's chunk allocated

Definition at line 176 of file THeapBuffer.hpp.

176 {
177 inject(const_cast<T*>(mem), size, realSize);
178 TKLB_ASSERT_STATE(IS_CONST = true)
179 }
void inject(T *mem, const Size size, const Size realSize=0)
Provide foreign memory to borrow.
Here is the call graph for this function:

◆ inject() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
void tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::inject ( T *  mem,
const Size  size,
const Size  realSize = 0 
)
inline

Provide foreign memory to borrow.

Parameters
memModifyable memory to use
sizeSize of the memory in elements
realSizeThe actual size if it's chunk allocated

Definition at line 161 of file THeapBuffer.hpp.

161 {
162 if (!injected() && mBuf != nullptr) { resize(0); };
163 TKLB_ASSERT_STATE(IS_CONST = false)
164 disown();
165 mBuf = mem;
166 mSize = size;
167 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ injected()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::injected ( ) const
inline

Whether the memory is managed by the instance or is borrowed.

Definition at line 369 of file THeapBuffer.hpp.

369{ return mRealSize == 0 && 0 < mSize && mBuf != nullptr; }
Here is the caller graph for this function:

◆ isAligned()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
static bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::isAligned ( const void *  ptr)
inlinestatic

Definition at line 391 of file THeapBuffer.hpp.

391 {
392 if (Alignment == 0) { return true; }
393 return size_t(ptr) % Alignment == 0;
394 }
Here is the caller graph for this function:

◆ last()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::last ( )
inline

Definition at line 223 of file THeapBuffer.hpp.

223 {
224 TKLB_ASSERT(0 < mSize)
225 return mBuf[mSize - 1];
226 }

◆ operator=() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
HeapBuffer & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::operator= ( const HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE > &  )
delete

◆ operator=() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
HeapBuffer & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::operator= ( HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE > &&  source)
inline

Definition at line 108 of file THeapBuffer.hpp.

108 {
109 TKLB_ASSERT_STATE(IS_CONST = source.IS_CONST)
110 mBuf = source.mBuf;
111 mSize = source.mSize;
112 mRealSize = source.mRealSize;
113 source.disown();
114 return *this;
115 }

◆ operator[]() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::operator[] ( const Size  index)
inline

Definition at line 208 of file THeapBuffer.hpp.

208 {
209 #ifdef TKLB_MEM_TRACE
210 TKLB_ASSERT(index < mSize)
211 #endif
212 // Don't use non const access when using injected const memory
213 TKLB_ASSERT(!IS_CONST)
214 return mBuf[index];
215 }

◆ operator[]() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
const T & tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::operator[] ( const Size  index) const
inline

Definition at line 201 of file THeapBuffer.hpp.

201 {
202 #ifdef TKLB_MEM_TRACE
203 TKLB_ASSERT(index < mSize)
204 #endif
205 return mBuf[index];
206 }

◆ pop()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::pop ( T *  object)
inline

Get the last element in the buffer.

Will never shrink the buffer/allocate

Definition at line 300 of file THeapBuffer.hpp.

300 {
301 if (0 == mSize) { return false; }
302 new (object) T(*(mBuf + mSize - 1));
303 mSize--;
304 return true;
305 }

◆ push()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::push ( const T &  object)
inline

Push the object to the back of the buffer TODO tklb move version.

Definition at line 280 of file THeapBuffer.hpp.

280 {
281 Size newSize = mSize + 1;
282 if (mRealSize < newSize) {
283 if (allocate(closestChunkSize(newSize, ChunkSize))) {
284 new (mBuf + mSize) T(object);
285 } else {
286 TKLB_ASSERT(false)
287 return false; // ! Allocation failed
288 }
289 } else {
290 new (mBuf + mSize) T(object);
291 }
292 mSize = newSize;
293 return true;
294 }
bool allocate(Size chunk) noexcept
Allocated the exact size requsted and copies existing objects.
static constexpr size_t ChunkSize
Definition: THeapBuffer.hpp:59
static Size closestChunkSize(Size size, Size chunk)
else() add_library(tklb STATIC $
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove() [1/3]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::remove ( const Size  index)
inline

Removes the object at a given index and fills the gap with another Will never shrink the buffer/allocate.

Definition at line 311 of file THeapBuffer.hpp.

311 {
312 if (mSize <= index) { return false; }
313 mBuf[index].~T(); // Call destructor
314 if (index != mSize - 1) { // fill the gap with the last element
315 memcpy(mBuf + index, mBuf + (mSize - 1), sizeof(T));
316 }
317 mSize--;
318 return true;
319 }
Here is the caller graph for this function:

◆ remove() [2/3]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::remove ( const T &  object)
inline

If a == comparisin is possible the object itself can be used.

Definition at line 324 of file THeapBuffer.hpp.

324 {
325 for (Size i = 0; i < mSize; i++) {
326 if (mBuf[i] == object) {
327 return remove(i);
328 }
329 }
330 return false;
331 }
bool remove(const Size index)
Removes the object at a given index and fills the gap with another Will never shrink the buffer/alloc...
Here is the call graph for this function:

◆ remove() [3/3]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::remove ( const T *  object)
inline

Otherwise use memory location.

Definition at line 336 of file THeapBuffer.hpp.

336 {
337 for (Size i = 0; i < mSize; i++) {
338 if ((mBuf + i) == object) {
339 return remove(i);
340 }
341 }
342 return false;
343 }
Here is the call graph for this function:

◆ reserve()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::reserve ( const Size  size)
inline

Will make sure the desired space is allocated.

Returns
Whether the allocation was succesful

Definition at line 237 of file THeapBuffer.hpp.

237 {
238 if (size < mRealSize) { return true; }
239 return allocate(size);
240 }
Here is the call graph for this function:

◆ reserved()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::reserved ( ) const
inline

Returns the real allocated size in elements.

Definition at line 379 of file THeapBuffer.hpp.

379{ return mRealSize; }

◆ resize()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::resize ( const Size  size,
const bool  downsize = true 
)
inline

Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon as an allocation happens.

Parameters
sizeThe new size
downsizeWhether to downsize and reallocate
Returns
Whether the allocation was successful

Definition at line 250 of file THeapBuffer.hpp.

250 {
251 const Size chunked = closestChunkSize(size, ChunkSize);
252
253 if (size < mSize && mBuf != nullptr && !injected()) { // downsize means destroy objects
254 for (Size i = size; i < mSize; i++) {
255 mBuf[i].~T(); // Call destructor
256 }
257 }
258
259 if (mRealSize < chunked || (downsize && (mRealSize > chunked)) ) {
260 // Upsize or downsize + downsize requested
261 if (!allocate(chunked)) {
262 return false; // ! Allocation failed
263 }
264 }
265
266 if (mSize < size) { // upsize means construct objects
267 for (Size i = mSize; i < size; i++) {
268 T* test = new (mBuf + i) T();
269 }
270 }
271
272 mSize = size;
273 return true;
274 }
constexpr int test
Definition: main.cpp:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set() [1/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
template<int Alignment2, class Allocator2 , typename Size2 >
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::set ( const HeapBuffer< T, Alignment2, Allocator2, Size2 > &  source)
inline

Resizes and copies the contents of the source Buffer This will do a memory::copy,so none of the object contructors will called.

Returns
True on success

Definition at line 132 of file THeapBuffer.hpp.

132 {
133 return set(source.data(), source.size());
134 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set() [2/2]

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
bool tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::set ( const T *  data,
Size  size 
)
inline

Set data from array.

Parameters
datadata array to copy
sizesize in elements of the data array
Returns
True on success

Definition at line 142 of file THeapBuffer.hpp.

142 {
143 if (mBuf != nullptr) {
144 resize(0); // Clear first so no old data gets copied on resize
145 }
146 if (!resize(size)) {
147 return false; // ! Allocation failed
148 }
149 for (Size i = 0; i < mSize; i++) {
150 new (mBuf + i) T(*(data + i));
151 }
152 return true;
153 }
Here is the call graph for this function:

◆ size()

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::size ( ) const
inline

Returns the amount of elements in the container.

Definition at line 374 of file THeapBuffer.hpp.

374{ return mSize; }
Here is the caller graph for this function:

Member Data Documentation

◆ Alignment

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
constexpr size_t tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::Alignment = ALIGNMENT
staticconstexpr

Definition at line 57 of file THeapBuffer.hpp.

◆ ChunkSize

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
constexpr size_t tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::ChunkSize = 16
staticconstexpr

Definition at line 59 of file THeapBuffer.hpp.

◆ mAllocator

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
ALLOCATOR tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::mAllocator
private

Definition at line 62 of file THeapBuffer.hpp.

◆ mBuf

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
T* tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::mBuf = nullptr
private

Definition at line 63 of file THeapBuffer.hpp.

◆ mRealSize

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::mRealSize = 0
private

the actually allocated size if it's 0 the memory is not actually owned by the buffer and won't be delete with the object.

No safety mechanism if memory becomes invalid

Definition at line 72 of file THeapBuffer.hpp.

◆ mSize

template<typename T , size_t ALIGNMENT = 0, class ALLOCATOR = StdAllocator <unsigned char>, typename SIZE = unsigned int>
Size tklb::HeapBuffer< T, ALIGNMENT, ALLOCATOR, SIZE >::mSize = 0
private

Definition at line 64 of file THeapBuffer.hpp.


The documentation for this class was generated from the following file: