fixed_mem_pool.h File Reference

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"
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

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.

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:
2013-10-06

Define Documentation

#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 != NULL) \
            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.
#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 != NULL) \
            NVWA::fixed_mem_pool<_Cls>::deallocate(ptr); \
    }

Declares the nothrow allocation and deallocation functions.

Parameters:
_Cls class to use the fixed_mem_pool
#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 != 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).

Parameters:
_Cls class to use the fixed_mem_pool
#define MEM_POOL_ALIGNMENT   sizeof(void*)

Defines the alignment of memory blocks.

 All Classes Namespaces Files Functions Variables Typedefs Friends Defines

Generated by  doxygen 1.6.2