55 #ifndef NVWA_FIXED_MEM_POOL_H 56 #define NVWA_FIXED_MEM_POOL_H 65 #include "static_assert.h" 72 #ifndef MEM_POOL_ALIGNMENT 73 #define MEM_POOL_ALIGNMENT sizeof(void*) 101 static const size_t value =
105 static void* allocate();
106 static void deallocate(
void*);
107 static bool initialize(
size_t size);
108 static int deinitialize();
109 static int get_alloc_count();
110 static bool is_initialized();
112 static bool bad_alloc_handler();
142 if (
void* result = _S_first_avail_ptr)
144 _S_first_avail_ptr = *(
void**)_S_first_avail_ptr;
149 if (!bad_alloc_handler())
162 if (block_ptr == _NULLPTR)
165 assert(_S_alloc_cnt != 0);
167 *(
void**)block_ptr = _S_first_avail_ptr;
168 _S_first_avail_ptr = block_ptr;
180 STATIC_ASSERT(alignment::value > 0 && alignment::value <= 8192,
182 STATIC_ASSERT((alignment::value & (alignment::value - 1)) == 0,
183 Alignment_must_be_power_of_two);
184 STATIC_ASSERT(block_size::value >=
sizeof(
void*),
185 Alignment_too_small);
186 assert(!is_initialized());
188 _S_mem_pool_ptr = mem_pool_base::alloc_sys(size * block_size::value);
189 _S_first_avail_ptr = _S_mem_pool_ptr;
190 if (_S_mem_pool_ptr == _NULLPTR)
192 char* block = (
char*)_S_mem_pool_ptr;
195 char* next = block + block_size::value;
196 *(
void**)block = next;
199 *(
void**)block = _NULLPTR;
213 if (_S_alloc_cnt != 0)
215 assert(is_initialized());
216 mem_pool_base::dealloc_sys(_S_mem_pool_ptr);
217 _S_mem_pool_ptr = _NULLPTR;
218 _S_first_avail_ptr = _NULLPTR;
241 return _S_mem_pool_ptr != _NULLPTR;
269 #define DECLARE_FIXED_MEM_POOL(_Cls) \ 271 static void* operator new(size_t size) \ 273 assert(size == sizeof(_Cls)); \ 274 if (void* ptr = NVWA::fixed_mem_pool<_Cls>::allocate()) \ 277 throw std::bad_alloc(); \ 279 static void operator delete(void* ptr) \ 281 if (ptr != _NULLPTR) \ 282 NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \ 290 #define DECLARE_FIXED_MEM_POOL__NOTHROW(_Cls) \ 292 static void* operator new(size_t size) _NOEXCEPT \ 294 assert(size == sizeof(_Cls)); \ 295 return NVWA::fixed_mem_pool<_Cls>::allocate(); \ 297 static void operator delete(void* ptr) \ 299 if (ptr != _NULLPTR) \ 300 NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \ 315 #define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK(_Cls) \ 317 static void* operator new(size_t size) \ 319 assert(size == sizeof(_Cls)); \ 320 return NVWA::fixed_mem_pool<_Cls>::allocate(); \ 322 static void operator delete(void* ptr) \ 324 if (ptr != _NULLPTR) \ 325 NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \ 328 #endif // NVWA_FIXED_MEM_POOL_H Helper class for class-level locking.
Definition: class_level_lock.h:76
In essence Loki ClassLevelLockable re-engineered to use a fast_mutex class.
Specializable struct to define the alignment of an object in the fixed_mem_pool.
Definition: fixed_mem_pool.h:91
static void * _S_mem_pool_ptr
Pointer to the allocated chunk of memory.
Definition: fixed_mem_pool.h:114
Type that provides locking/unlocking semantics.
Definition: class_level_lock.h:89
static int _S_alloc_cnt
Count of allocations.
Definition: fixed_mem_pool.h:116
Struct to calculate the block size based on the (specializable) alignment value.
Definition: fixed_mem_pool.h:99
C++11 feature detection macros and workarounds.
#define MEM_POOL_ALIGNMENT
Defines the alignment of memory blocks.
Definition: fixed_mem_pool.h:73
Header file for the memory pool base.
Class template to manipulate a fixed-size memory pool.
Definition: fixed_mem_pool.h:83
static void * _S_first_avail_ptr
Pointer to the first available memory block.
Definition: fixed_mem_pool.h:115
Common definitions for preprocessing.