Nvwa  1.1
Classes | Namespaces | Macros
fixed_mem_pool.h File Reference

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"
Include dependency graph for fixed_mem_pool.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

 nvwa
 Namespace of the nvwa project.
 

Macros

#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. More...
 
#define DECLARE_FIXED_MEM_POOL__NOTHROW(_Cls)
 Declares the nothrow allocation and deallocation functions. More...
 
#define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK(_Cls)
 Declares the throwing, non-checking allocation and deallocation functions. More...
 

Detailed Description

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:

Date
2014-11-29

Macro Definition Documentation

◆ 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
_Clsclass 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
_Clsclass 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
_Clsclass to use the fixed_mem_pool