2 #include "util/mutex.h" 3 #include "math/ParaMath.h" 4 #include "util/PoolBase.h" 5 #include <boost/pool/object_pool.hpp> 10 template <
typename UserAllocator = boost::default_user_allocator_new_delete,
typename Mutex = ParaEngine::mutex>
24 #define NPL_char_pool_init_size_bytes 32 26 #define NPL_char_pool_init_size 5 28 #define NPL_char_pool_count 7 47 template <
typename UserAllocator,
52 typedef char value_type;
53 typedef UserAllocator user_allocator;
54 typedef Mutex mutex_type;
56 typedef value_type * pointer;
57 typedef const value_type * const_pointer;
58 typedef value_type & reference;
59 typedef const value_type & const_reference;
61 typedef typename pool_type::size_type size_type;
62 typedef typename pool_type::difference_type difference_type;
75 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes,1024);
76 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2,512);
77 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2*2,256);
78 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2*2*2,128);
79 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2,64);
80 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2*2,32);
81 m_mem_pools[i++] =
new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2*2*2,32);
82 PE_ASSERT(i==NPL_char_pool_count);
86 for(
int i=0;i<NPL_char_pool_count;++i)
88 delete m_mem_pools[i];
91 pool_type& operator [] (
int nIndex)
93 return *(m_mem_pools[nIndex]);
96 pool_type* m_mem_pools[NPL_char_pool_count];
114 static pointer address(reference r)
116 static const_pointer address(const_reference s)
118 static size_type max_size()
119 {
return (std::numeric_limits<size_type>::max)(); }
126 static pointer allocate(
const size_type n)
132 if(nIndex < NPL_char_pool_count)
134 ret =
static_cast<pointer
>(s_mem_pools[nIndex].malloc());
138 ret = UserAllocator::malloc(n);
141 boost::throw_exception(std::bad_alloc());
144 static pointer allocate(
const size_type n,
const void *
const)
145 {
return allocate(n); }
146 static void deallocate(
const pointer ptr,
const size_type n)
148 if (ptr == 0 || n == 0)
153 if(nIndex < NPL_char_pool_count)
155 s_mem_pools[nIndex].free(ptr);
159 UserAllocator::free(ptr);
165 template <
typename UserAllocator,
typename Mutex>
different physics engine has different winding order.
Definition: EventBinding.h:32
std::basic_string< char, std::char_traits< char >, ParaEngine::CNPLPool_Char_alloc<> > NPLString
NPL String can be used instead of std::string, for strings which are created and deleted very regular...
Definition: NPLMemPool.h:21
Definition: NPLMemPool.h:65
cross platform mutex
Definition: Mutex.hpp:88
Definition: NPLMemPool.h:70
This class can be used as a custom allocator for stl containers, which constantly create and delete s...
Definition: NPLMemPool.h:11
static SingletonMemPool_Type s_mem_pools
predefined memory pool (free lists)
Definition: NPLMemPool.h:98
static int log2_ceil(unsigned int x)
this is a fast version of log2.
Definition: ParaMath.h:458
CNPLPool_Char_alloc CNPLPool_Char_allocator
a quick define
Definition: NPLMemPool.h:11
this is a thread safe version of boost::pool.
Definition: PoolBase.h:9