/******************************************************************** created: 2008/06/20 created: 20:6:2008 10:22 filename: d:\Projects\ServiceSpace\ServiceSpace\ServiceSpaceLib\obj_pool.h file path: d:\Projects\ServiceSpace\ServiceSpace\ServiceSpaceLib file base: obj_pool file ext: h author: purpose: *********************************************************************/ #ifndef __UTILIB_MEM_POOL_H__ #define __UTILIB_MEM_POOL_H__ #include #include "pool_lock.h" #include "block_pool.h" namespace utilib { // decouple template struct __obj_alloc { typedef block_pool __alloc_type; static __alloc_type __alloc; }; template typename __obj_alloc::__alloc_type __obj_alloc::__alloc; template struct __obj_alloc_type_ship { }; template inline void * __obj_allocate(__obj_alloc_type_ship &) { return __obj_alloc::__alloc.allocate(); } template inline void __obj_deallocate(__obj_alloc_type_ship &, void *p) { __obj_alloc::__alloc.deallocate(p); } template< class T, size_t Count = 50, class Grow = grow::arithmetic<1>, class Lock = pool_lock > class obj_pool { public: typedef T value_type; typedef value_type * pointer; typedef const value_type * const_pointer; typedef value_type & reference; typedef const value_type & const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; template struct rebind { typedef obj_pool other; }; T * allocate(size_t n = 1, void * = 0) { if (n == 1) { __obj_alloc_type_ship tmp; return (pointer)__obj_allocate(tmp); } return 0; } T * allocate(const T & val) { void * p = allocate(); return new(p) T(val); } template T * allocate(_Initializer initer) { T * p = static_cast(allocate()); initer(p); return p; } void deallocate(void * p, size_t n = 1) { if (n == 1) { __obj_alloc_type_ship ship; __obj_deallocate(ship, p); } } size_type max_size()const { return 1; } }; } #endif