Definition of a fixed-size memory pool template for structs/classes. More...
#include <new>#include <assert.h>#include <stdlib.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.
Classes | |
| class | nvwa::fixed_mem_pool< _Tp > |
| Class template to manipulate a fixed-size memory pool. More... | |
| struct | nvwa::fixed_mem_pool< _Tp >::alignment |
| Specializable struct to define the alignment of an object in the fixed_mem_pool. More... | |
| struct | nvwa::fixed_mem_pool< _Tp >::block_size |
| Struct to calculate the block size based on the (specializable) alignment value. More... | |
Namespaces | |
| namespace | nvwa |
Defines | |
| #define | MEM_POOL_ALIGNMENT sizeof(void*) |
| Defines the alignment of memory blocks. | |
| #define | DECLARE_FIXED_MEM_POOL(_Cls) |
| Declares the normal (throwing) allocation and deallocation functions. | |
| #define | DECLARE_FIXED_MEM_POOL__NOTHROW(_Cls) |
| Declares the nothrow allocation and deallocation functions. | |
| #define | DECLARE_FIXED_MEM_POOL__THROW_NOCHECK(_Cls) |
| Declares the throwing, non-checking allocation and deallocation functions. | |
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:
class _Cls) definitions.| #define DECLARE_FIXED_MEM_POOL | ( | _Cls | ) |
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 != NULL) \ NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \ }
Declares the normal (throwing) allocation and deallocation functions.
| _Cls | class to use the fixed_mem_pool |
NULL, but requires more discipline on the programmer's side. | #define DECLARE_FIXED_MEM_POOL__NOTHROW | ( | _Cls | ) |
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 != NULL) \ NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \ }
Declares the nothrow allocation and deallocation functions.
| _Cls | class to use the fixed_mem_pool |
| #define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK | ( | _Cls | ) |
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 != NULL) \ 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).
| _Cls | class to use the fixed_mem_pool |
| #define MEM_POOL_ALIGNMENT sizeof(void*) |
Defines the alignment of memory blocks.
1.6.2