37 #ifndef NVWA_STATIC_MEM_POOL_H 38 #define NVWA_STATIC_MEM_POOL_H 52 # ifdef _STATIC_MEM_POOL_DEBUG 54 # define _STATIC_MEM_POOL_TRACE(_Lck, _Msg) \ 57 static_mem_pool_set::lock guard; \ 58 std::cerr << "static_mem_pool: " << _Msg << std::endl; \ 60 std::cerr << "static_mem_pool: " << _Msg << std::endl; \ 64 # define _STATIC_MEM_POOL_TRACE(_Lck, _Msg) \ 86 typedef std::vector<mem_pool_base*> container_type;
87 container_type _M_memory_pool_set;
104 template <
size_t _Sz,
int _Gid = -1>
123 _S_instance_p = _S_create_instance();
125 return *_S_instance_p;
136 assert(_S_instance_p != _NULLPTR);
137 return *_S_instance_p;
151 if (_S_memory_block_p)
153 void* result = _S_memory_block_p;
154 _S_memory_block_p = _S_memory_block_p->_M_next;
158 return _S_alloc_sys(_S_align(_Sz));
167 assert(ptr != _NULLPTR);
170 block->
_M_next = _S_memory_block_p;
171 _S_memory_block_p = block;
173 virtual void recycle() _OVERRIDE;
178 _STATIC_MEM_POOL_TRACE(
true,
"static_mem_pool<" << _Sz <<
',' 179 << _Gid <<
"> is created");
193 _S_memory_block_p = _NULLPTR;
195 _S_instance_p = _NULLPTR;
197 _STATIC_MEM_POOL_TRACE(
false,
"static_mem_pool<" << _Sz <<
',' 198 << _Gid <<
"> is destroyed");
200 static size_t _S_align(
size_t size)
204 static void* _S_alloc_sys(
size_t size);
207 static bool _S_destroyed;
216 template <
size_t _Sz,
int _G
id>
bool 228 template <
size_t _Sz,
int _G
id>
250 _STATIC_MEM_POOL_TRACE(
false,
"static_mem_pool<" << _Sz <<
',' 251 << _Gid <<
"> is recycled");
254 template <
size_t _Sz,
int _G
id>
258 void* result = mem_pool_base::alloc_sys(size);
261 static_mem_pool_set::instance().recycle();
262 result = mem_pool_base::alloc_sys(size);
267 template <
size_t _Sz,
int _G
id>
271 throw std::runtime_error(
"dead reference detected");
273 static_mem_pool_set::instance();
277 static_mem_pool_set::instance().add(inst_p);
281 _STATIC_MEM_POOL_TRACE(
true,
282 "Exception occurs in static_mem_pool_set::add");
301 #define DECLARE_STATIC_MEM_POOL(_Cls) \ 303 static void* operator new(size_t size) \ 305 assert(size == sizeof(_Cls)); \ 307 ptr = NVWA::static_mem_pool<sizeof(_Cls)>:: \ 308 instance_known().allocate(); \ 309 if (ptr == _NULLPTR) \ 310 throw std::bad_alloc(); \ 313 static void operator delete(void* ptr) \ 316 NVWA::static_mem_pool<sizeof(_Cls)>:: \ 317 instance_known().deallocate(ptr); \ 329 #define DECLARE_STATIC_MEM_POOL__NOTHROW(_Cls) \ 331 static void* operator new(size_t size) _NOEXCEPT \ 333 assert(size == sizeof(_Cls)); \ 334 return NVWA::static_mem_pool<sizeof(_Cls)>:: \ 335 instance_known().allocate(); \ 337 static void operator delete(void* ptr) \ 340 NVWA::static_mem_pool<sizeof(_Cls)>:: \ 341 instance_known().deallocate(ptr); \ 354 #define DECLARE_STATIC_MEM_POOL_GROUPED(_Cls, _Gid) \ 356 static void* operator new(size_t size) \ 358 assert(size == sizeof(_Cls)); \ 360 ptr = NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \ 361 instance_known().allocate(); \ 362 if (ptr == _NULLPTR) \ 363 throw std::bad_alloc(); \ 366 static void operator delete(void* ptr) \ 369 NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \ 370 instance_known().deallocate(ptr); \ 383 #define DECLARE_STATIC_MEM_POOL_GROUPED__NOTHROW(_Cls, _Gid) \ 385 static void* operator new(size_t size) _NOEXCEPT \ 387 assert(size == sizeof(_Cls)); \ 388 return NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \ 389 instance_known().allocate(); \ 391 static void operator delete(void* ptr) \ 394 NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \ 395 instance_known().deallocate(ptr); \ 398 #endif // NVWA_STATIC_MEM_POOL_H Base class for memory pools.
Definition: mem_pool_base.h:48
static static_mem_pool & instance_known()
Gets the known instance of the static memory pool.
Definition: static_mem_pool.h:134
void * allocate()
Allocates memory and returns its pointer.
Definition: static_mem_pool.h:147
Singleton class template to manage the allocation/deallocation of memory blocks of one specific size...
Definition: static_mem_pool.h:105
static static_mem_pool & instance()
Gets the instance of the static memory pool.
Definition: static_mem_pool.h:118
Structure to store the next available memory block.
Definition: mem_pool_base.h:57
Helper class for class-level locking.
Definition: class_level_lock.h:76
In essence Loki ClassLevelLockable re-engineered to use a fast_mutex class.
void deallocate(void *ptr)
Deallocates memory by putting the memory block into the pool.
Definition: static_mem_pool.h:165
_Block_list * _M_next
Pointer to the next memory block.
Definition: mem_pool_base.h:59
Singleton class to maintain a set of existing instantiations of static_mem_pool.
Definition: static_mem_pool.h:74
Type that provides locking/unlocking semantics.
Definition: class_level_lock.h:89
C++11 feature detection macros and workarounds.
Header file for the memory pool base.
Common definitions for preprocessing.