2 #ifndef PARAENGINE_MOBILE 3 #include "util/dl_malloc_include.h" 5 #include "util/PoolBase.h" 17 #ifdef PARAENGINE_MOBILE 18 #define FixedSizedAllocator std::allocator 21 #define FixedSizedAllocator Pool_Char_alloc 22 extern void* mainthread_malloc(
size_t bytes);
23 extern void mainthread_free(
void* mem);
27 typedef std::size_t size_type;
28 typedef std::ptrdiff_t difference_type;
30 static char * malloc(
const size_type bytes)
31 {
return (
char*) mainthread_malloc(bytes); }
33 static void free(
char *
const block)
34 { mainthread_free(block); }
39 typedef std::size_t size_type;
40 typedef std::ptrdiff_t difference_type;
42 static char * malloc(
const size_type bytes)
43 {
return new (std::nothrow)
char[bytes]; }
44 static void free(
char *
const block)
50 typedef std::size_t size_type;
51 typedef std::ptrdiff_t difference_type;
53 static char * malloc(
const size_type bytes)
54 {
return reinterpret_cast<char *
>(std::malloc(bytes)); }
55 static void free(
char *
const block)
66 template<
typename T,
typename UserAllocator = default_user_allocator_dl_malloc>
80 template<
typename T,
typename UserAllocator>
85 typedef UserAllocator user_allocator;
86 typedef value_type* pointer;
87 typedef const value_type* const_pointer;
88 typedef value_type& reference;
89 typedef const value_type& const_reference;
90 typedef std::size_t size_type;
91 typedef std::ptrdiff_t difference_type;
108 inline pointer address(reference r) {
return &r; }
109 inline const_pointer address(const_reference r) {
return &r; }
112 inline size_type max_size()
const {
113 return std::numeric_limits<size_type>::max() /
sizeof(T);
117 inline pointer allocate(size_type cnt,
118 typename std::allocator<void>::const_pointer = 0)
120 return reinterpret_cast<pointer
>(user_allocator::malloc(cnt *
sizeof (T)));
122 inline void deallocate(pointer p, size_type)
124 user_allocator::free((
char *
const)p);
128 inline void construct(pointer p,
const T& t) {
new(p) T(t); }
129 inline void destroy(pointer p) { p->~T(); }
131 inline bool operator==(
DL_Allocator const&) {
return true; }
132 inline bool operator!=(
DL_Allocator const& a) {
return !operator==(a); }
143 template <
typename T,
typename UserAllocator = default_user_allocator_dl_malloc>
160 template <
typename T,
typename UserAllocator>
164 typedef T value_type;
165 typedef UserAllocator user_allocator;
167 typedef value_type * pointer;
168 typedef const value_type * const_pointer;
169 typedef value_type & reference;
170 typedef const value_type & const_reference;
171 typedef boost::pool<UserAllocator> pool_type;
172 typedef typename pool_type::size_type size_type;
173 typedef typename pool_type::difference_type difference_type;
176 static const int char_pool_init_size_bytes = 32;
178 static const int char_pool_init_size = 5;
180 static const int char_pool_count = 7;
187 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes);
188 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2);
189 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2*2);
190 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2*2*2);
191 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2*2*2*2);
192 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2*2*2*2*2);
193 m_mem_pools[i++] =
new pool_type(char_pool_init_size_bytes*2*2*2*2*2*2);
194 PE_ASSERT(i==char_pool_count);
198 for(
int i=0;i<char_pool_count;++i)
200 delete m_mem_pools[i];
203 pool_type& operator [] (
int nIndex)
205 return *(m_mem_pools[nIndex]);
208 pool_type* m_mem_pools[char_pool_count];
213 template <
typename U>
226 inline pointer address(reference r) {
return &r; }
227 inline const_pointer address(const_reference r) {
return &r; }
229 inline size_type max_size()
const {
230 return std::numeric_limits<size_type>::max() /
sizeof(T);
234 inline pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0)
236 int n = (int)(cnt *
sizeof (T));
241 if(nIndex < char_pool_count)
243 ret =
static_cast<pointer
>(s_mem_pools[nIndex].malloc());
247 ret =
reinterpret_cast<pointer
>(user_allocator::malloc(n));
250 boost::throw_exception(std::bad_alloc());
254 inline void deallocate(pointer ptr, size_type cnt)
256 int n = (int)(cnt *
sizeof (T));
257 if (ptr == 0 || n == 0)
262 if(nIndex < char_pool_count)
264 s_mem_pools[nIndex].free(ptr);
268 user_allocator::free((
char *
const)ptr);
273 inline void construct(pointer p,
const T& t) {
new(p) T(t); }
274 inline void destroy(pointer p) { p->~T(); }
277 inline bool operator!=(
Pool_Char_alloc const& a) {
return !operator==(a); }
282 template <
typename T,
typename UserAllocator>
This class can be used as a custom allocator for stl containers, which constantly create and delete s...
Definition: ParaMemPool.h:144
this is a general single-threaded allocator, which is used by the main game (rendering) thread in Par...
Definition: ParaMemPool.h:67
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: ParaMemPool.h:48
Definition: ParaMemPool.h:37
Definition: ParaMemPool.h:96
Definition: ParaMemPool.h:214
Definition: ParaMemPool.h:182
Definition: ParaMemPool.h:25
static SingletonMemPool_Type s_mem_pools
predefined memory pool (free lists)
Definition: ParaMemPool.h:211
static int log2_ceil(unsigned int x)
this is a fast version of log2.
Definition: ParaMath.h:458