Definition of a fixed-size memory pool template for structs/classes.
More...
#include <new>
#include <assert.h>
#include <stddef.h>
#include "_nvwa.h"
#include "c++11.h"
#include "class_level_lock.h"
#include "mem_pool_base.h"
#include "static_assert.h"
Go to the source code of this file.
|
| nvwa |
| Namespace of the nvwa project.
|
|
Definition of a fixed-size memory pool template for structs/classes.
This is a easy-to-use class template for pre-allocated memory pools. The client side needs to do the following things:
- Use one of the macros
at the end of the class (say,
class
_Cls) definitions.
- Optionally, specialize fixed_mem_pool::alignment to change the alignment value for this specific type.
- Optionally, specialize fixed_mem_pool::bad_alloc_handler to change the behaviour when all memory blocks are allocated.
- Call fixed_mem_pool<_Cls>::initialize at the beginning of the program.
- Optionally, call fixed_mem_pool<_Cls>::deinitialize at exit of the program to check for memory leaks.
- Optionally, call fixed_mem_pool<_Cls>::get_alloc_count to check memory usage when the program is running.
- Date
- 2014-11-29
◆ DECLARE_FIXED_MEM_POOL
#define DECLARE_FIXED_MEM_POOL |
( |
|
_Cls | ) |
|
Value:public: \
static void* operator new(size_t size) \
{ \
assert(size == sizeof(_Cls)); \
if (void* ptr = NVWA::fixed_mem_pool<_Cls>::allocate()) \
return ptr; \
else \
throw std::bad_alloc(); \
} \
static void operator delete(void* ptr) \
{ \
if (ptr != _NULLPTR) \
NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \
}
Declares the normal (throwing) allocation and deallocation functions.
- Parameters
-
_Cls | class to use the fixed_mem_pool |
- See also
- DECLARE_FIXED_MEM_POOL__THROW_NOCHECK, which, too, defines an operator new that will never return null, but requires more discipline on the programmer's side.
◆ DECLARE_FIXED_MEM_POOL__NOTHROW
#define DECLARE_FIXED_MEM_POOL__NOTHROW |
( |
|
_Cls | ) |
|
Value:public: \
static void* operator new(size_t size) _NOEXCEPT \
{ \
assert(size == sizeof(_Cls)); \
return NVWA::fixed_mem_pool<_Cls>::allocate(); \
} \
static void operator delete(void* ptr) \
{ \
if (ptr != _NULLPTR) \
NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \
}
Declares the nothrow allocation and deallocation functions.
- Parameters
-
_Cls | class to use the fixed_mem_pool |
◆ DECLARE_FIXED_MEM_POOL__THROW_NOCHECK
#define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK |
( |
|
_Cls | ) |
|
Value:public: \
static void* operator new(size_t size) \
{ \
assert(size == sizeof(_Cls)); \
return NVWA::fixed_mem_pool<_Cls>::allocate(); \
} \
static void operator delete(void* ptr) \
{ \
if (ptr != _NULLPTR) \
NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \
}
Declares the throwing, non-checking allocation and deallocation functions.
N.B. Using this macro requires users to explicitly specialize fixed_mem_pool::bad_alloc_handler so that it shall never return false
(it may throw exceptions, say, std::bad_alloc
, or simply abort). Otherwise a segmentation fault might occur (instead of returning a null pointer).
- Parameters
-
_Cls | class to use the fixed_mem_pool |