QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
#include <new>
#include <assert.h>
#include <stdlib.h>
#include "class_level_lock.h"
#include "mem_pool_base.h"

Go to the source code of this file.

Classes

class  qx::memory::fixed_mem_pool< _Tp >
 

Namespaces

namespace  qx
 Root namespace for all QxOrm library features.
 
namespace  qx::memory
 QxOrm library memory leak detection (by Wu Yongwei)
 

Macros

#define MEM_POOL_ALIGNMENT   4
 
#define DECLARE_FIXED_MEM_POOL(_Cls)
 
#define DECLARE_FIXED_MEM_POOL__NOTHROW(_Cls)
 
#define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK(_Cls)
 

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:

  • Use one of the macros DECLARE_FIXED_MEM_POOL, DECLARE_FIXED_MEM_POOL__NOTHROW, and DECLARE_FIXED_MEM_POOL__THROW_NOCHECK at the end of the class (say, class _Cls) definitions
  • Call fixed_mem_pool<_Cls>::initialize at the beginning of the program
  • Optionally, specialize fixed_mem_pool<_Cls>::bad_alloc_handler to change the behaviour when all memory blocks are allocated
  • 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
Version
1.14, 2005/09/19
Author
Wu Yongwei

Definition in file fixed_mem_pool.h.

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 = fixed_mem_pool<_Cls>::allocate()) \
return __ptr; \
else \
throw std::bad_alloc(); \
} \
static void operator delete(void* __ptr) \
{ \
if (__ptr != NULL) \
fixed_mem_pool<_Cls>::deallocate(__ptr); \
}

Declares the normal (exceptionable) overload of operator new and operator delete.

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.

Definition at line 267 of file fixed_mem_pool.h.

◆ DECLARE_FIXED_MEM_POOL__NOTHROW

#define DECLARE_FIXED_MEM_POOL__NOTHROW ( _Cls)
Value:
public: \
static void* operator new(size_t __size) throw() \
{ \
assert(__size == sizeof(_Cls)); \
return fixed_mem_pool<_Cls>::allocate(); \
} \
static void operator delete(void* __ptr) \
{ \
if (__ptr != NULL) \
fixed_mem_pool<_Cls>::deallocate(__ptr); \
}

Declares the non-exceptionable overload of operator new and operator delete.

Parameters
_Clsclass to use the fixed_mem_pool

Definition at line 289 of file fixed_mem_pool.h.

◆ 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 fixed_mem_pool<_Cls>::allocate(); \
} \
static void operator delete(void* __ptr) \
{ \
if (__ptr != NULL) \
fixed_mem_pool<_Cls>::deallocate(__ptr); \
}

Declares the exceptionable, non-checking overload of operator new and operator delete.

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

Definition at line 314 of file fixed_mem_pool.h.

◆ MEM_POOL_ALIGNMENT

#define MEM_POOL_ALIGNMENT   4

Defines the alignment of memory blocks.

Definition at line 75 of file fixed_mem_pool.h.