VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::memory::MemoryPool Class Referenceabstract

Memory pool interface. More...

#include <TMemoryPool.hpp>

Inheritance diagram for tklb::memory::MemoryPool:
Collaboration diagram for tklb::memory::MemoryPool:

Classes

struct  Block
 Struct used to mark every allocation;. More...
 
struct  SharedPool
 Pools can share the same space, but use it differently. More...
 

Public Member Functions

 MemoryPool (void *pool, Size size)
 Construct a pool from memory provided. More...
 
 ~MemoryPool ()
 
Size getAllocated () const
 
Size getTotalSize () const
 
virtual void * allocate (Size size)=0
 
virtual void deallocate (void *ptr)=0
 
virtual void * reallocate (void *ptr, size_t size)=0
 
void * clearallocate (size_t num, size_t size)
 Allocates space for num of structs with size size and clears the memory. More...
 
template<class T , typename ... Args>
T * create (Args &&... args)
 Acts like new. More...
 
template<class T , typename ... Args>
T * createArray (size_t count, Args &&... args)
 
template<class T >
void dispose (T *ptr)
 Acts like delete. More...
 
template<class T >
void disposeArray (size_t count, T *ptr)
 Acts like delete. More...
 
void deallocateAligned (void *ptr)
 
void * allocateAligned (const Size size, const Size align=DEFAULT_ALIGN)
 Allocate aligned if simd is enabled. More...
 

Protected Types

using Size = uintptr_t
 
using Byte = unsigned char
 
using Mutex = SpinLock
 
using Lock = LockGuard< Mutex >
 

Protected Attributes

SharedPoolmPool
 

Static Protected Attributes

static constexpr Size DEFAULT_ALIGN = xsimd::default_arch::alignment()
 

Detailed Description

Memory pool interface.

Definition at line 22 of file TMemoryPool.hpp.

Member Typedef Documentation

◆ Byte

using tklb::memory::MemoryPool::Byte = unsigned char
protected

Definition at line 28 of file TMemoryPool.hpp.

◆ Lock

Definition at line 30 of file TMemoryPool.hpp.

◆ Mutex

Definition at line 29 of file TMemoryPool.hpp.

◆ Size

using tklb::memory::MemoryPool::Size = uintptr_t
protected

Definition at line 27 of file TMemoryPool.hpp.

Constructor & Destructor Documentation

◆ MemoryPool()

tklb::memory::MemoryPool::MemoryPool ( void *  pool,
Size  size 
)
inline

Construct a pool from memory provided.

The first few bytes need to be zero to indicate when the pool needs to be initialized.

Definition at line 76 of file TMemoryPool.hpp.

76 : mPool(*static_cast<SharedPool*>(pool)) {
77 if (mPool.size == 0) { // New pool
78 TKLB_ASSERT(sizeof(SharedPool) <= size)
79 // Initialize pool
80 SharedPool* p = new (pool) SharedPool();
81 mPool.size = size - sizeof(SharedPool);
82 mPool.memory = reinterpret_cast<Byte*>(p + 1);
83 }
84 TKLB_ASSERT(size == (mPool.size + sizeof(SharedPool)))
85 mPool.users++;
86 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
Byte * memory
Start of the usable pool space this does not store the pointer, but the location location itself is u...
Definition: TMemoryPool.hpp:65

◆ ~MemoryPool()

tklb::memory::MemoryPool::~MemoryPool ( )
inline

Definition at line 88 of file TMemoryPool.hpp.

88 {
90 mPool.users--;
91 if (mPool.users == 0) {
93 }
94 }

Member Function Documentation

◆ allocate()

virtual void * tklb::memory::MemoryPool::allocate ( Size  size)
pure virtual

Implemented in tklb::memory::MemoryPoolStack, and tklb::memory::MemoryPoolStd.

Here is the caller graph for this function:

◆ allocateAligned()

void * tklb::memory::MemoryPool::allocateAligned ( const Size  size,
const Size  align = DEFAULT_ALIGN 
)
inline

Allocate aligned if simd is enabled.

Does a normal allocation otherwise.

Definition at line 173 of file TMemoryPool.hpp.

173 {
174 // malloc is already already aligned to sizeof(size_t)
175 void* result = allocate(size + align);
176 if (result != nullptr && align != 0) {
177 // Mask with zeroes at the end to floor the pointer to an aligned block
178 const Size mask = ~(Size(align - 1));
179 const Size pointer = reinterpret_cast<Size>(result);
180 const Size floored = pointer & mask;
181 const Size aligned = floored + align;
182
183 // Not enough space before aligned memory to store original ptr
184 // This only happens when malloc doesn't align to sizeof(size_t)
185 TKLB_ASSERT(sizeof(Size) <= (aligned - pointer))
186
187 result = reinterpret_cast<void*>(aligned);
188 Size* original = reinterpret_cast<Size*>(result) - 1;
189 *(original) = pointer;
190 }
191 TKLB_ASSERT(Size(result) % align == 0)
192 return result;
193 }
virtual void * allocate(Size size)=0
Here is the call graph for this function:

◆ clearallocate()

void * tklb::memory::MemoryPool::clearallocate ( size_t  num,
size_t  size 
)
inline

Allocates space for num of structs with size size and clears the memory.

Parameters
numNumber of structs
sizeSize of a single struct

Definition at line 111 of file TMemoryPool.hpp.

111 {
112 const size_t total = size * num;
113 void* ptr = allocate(total);
114 if (ptr == nullptr) { return nullptr; }
115 set(ptr, 0, total);
116 return ptr;
117 }
static void set(void *dst, const unsigned char val, size_t size)
memset wrapper
Definition: TMemoryUtil.hpp:40
Here is the call graph for this function:

◆ create()

template<class T , typename ... Args>
T * tklb::memory::MemoryPool::create ( Args &&...  args)
inline

Acts like new.

Allocate and construct object.

Parameters
argsArguments passed to class contructor

Definition at line 124 of file TMemoryPool.hpp.

124 {
125 void* ptr = allocate(sizeof(T));
126 if (ptr == nullptr) { return nullptr; }
127 new (ptr) T(std::forward<Args>(args)...);
128 return reinterpret_cast<T*>(ptr);
129 }
Here is the call graph for this function:

◆ createArray()

template<class T , typename ... Args>
T * tklb::memory::MemoryPool::createArray ( size_t  count,
Args &&...  args 
)
inline

Definition at line 132 of file TMemoryPool.hpp.

132 {
133 unsigned char* ptr = reinterpret_cast<unsigned char*>(allocate(sizeof(T) * count));
134 if (ptr == nullptr) { return nullptr; }
135 for (size_t i = 0; i < count; i++) {
136 new (ptr + i * sizeof(T)) T(std::forward<Args>(args)...);
137 }
138 return reinterpret_cast<T*>(ptr);
139 }
Here is the call graph for this function:

◆ deallocate()

virtual void tklb::memory::MemoryPool::deallocate ( void *  ptr)
pure virtual

Implemented in tklb::memory::MemoryPoolStack, and tklb::memory::MemoryPoolStd.

Here is the caller graph for this function:

◆ deallocateAligned()

void tklb::memory::MemoryPool::deallocateAligned ( void *  ptr)
inline

Definition at line 163 of file TMemoryPool.hpp.

163 {
164 if (ptr == nullptr) { return; }
165 // Get the orignal allocation address to free the memory
166 deallocate(*(reinterpret_cast<void**>(ptr) - 1));
167 }
virtual void deallocate(void *ptr)=0
Here is the call graph for this function:

◆ dispose()

template<class T >
void tklb::memory::MemoryPool::dispose ( T *  ptr)
inline

Acts like delete.

Destroy the object and dispose the memory.

Definition at line 145 of file TMemoryPool.hpp.

145 {
146 if (ptr == nullptr) { return; }
147 ptr->~T();
148 deallocate(ptr);
149 }
Here is the call graph for this function:

◆ disposeArray()

template<class T >
void tklb::memory::MemoryPool::disposeArray ( size_t  count,
T *  ptr 
)
inline

Acts like delete.

Destroy the object and dispose the memory.

Definition at line 155 of file TMemoryPool.hpp.

155 {
156 if (ptr == nullptr) { return; }
157 for (size_t i = 0; i < count; i++) {
158 (ptr + i * sizeof(T))->~T();
159 }
160 deallocate(ptr);
161 }
Here is the call graph for this function:

◆ getAllocated()

Size tklb::memory::MemoryPool::getAllocated ( ) const
inline

Definition at line 96 of file TMemoryPool.hpp.

96{ return mPool.allocated; }

◆ getTotalSize()

Size tklb::memory::MemoryPool::getTotalSize ( ) const
inline

Definition at line 98 of file TMemoryPool.hpp.

98{ return mPool.size; }

◆ reallocate()

virtual void * tklb::memory::MemoryPool::reallocate ( void *  ptr,
size_t  size 
)
pure virtual

Member Data Documentation

◆ DEFAULT_ALIGN

constexpr Size tklb::memory::MemoryPool::DEFAULT_ALIGN = xsimd::default_arch::alignment()
staticconstexprprotected

Definition at line 33 of file TMemoryPool.hpp.

◆ mPool

SharedPool& tklb::memory::MemoryPool::mPool
protected

Definition at line 68 of file TMemoryPool.hpp.


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