1 #ifndef PROTON_POOL_HEADER
2 #define PROTON_POOL_HEADER
6 #ifndef PROTON_POOL_DEBUG
7 #define PROTON_POOL_DEBUG 1
11 #define PROTON_POOL_THROW_IF PROTON_THROW_IF
13 #define PROTON_POOL_THROW_IF(cond, out)
19 void pool_free(
void* p);
27 void* __mmdup(
void* p);
55 return (_next==
this || _next==NULL);
58 void erase_from_list()
76 list_header* next()
const
81 list_header* prev()
const
86 void insert_after(list_header* prev)
88 PROTON_POOL_THROW_IF(_prev,
"invalid prev");
89 PROTON_POOL_THROW_IF(_next,
"invalid next");
100 void insert_before(list_header* next)
102 PROTON_POOL_THROW_IF(_prev,
"invalid prev");
103 PROTON_POOL_THROW_IF(_next,
"invalid next");
116 inline pool_block* get_block(list_header* lh)
118 return (pool_block*)(lh);
136 char* _unalloc_chunk;
159 return get_block(_prev);
163 return get_block(_next);
168 return _chunk_cnt==_chunk_cap;
172 return _chunk_cnt==0;
182 friend void proton::pool_free(
void* p);
193 size_t _total_block_size;
194 size_t _min_block_size;
221 void init(
size_t chunk_size,
size_t chunk_min_size,
mem_pool* parent);
225 size_t chunk_size()
const
229 size_t chunk_min_size()
const
234 void* malloc(
size_t size,
size_t n=1);
237 void get_info(
size_t&free_cnt,
size_t& free_cap,
size_t& empty_cap,
size_t& full_cnt);
238 void print_info(
bool print_null);
248 #define PROTON_META_BLOCK_MAX 128
257 size_t _seg_linear_cnt;
259 void compute_sizes(
size_t max,
size_t align,
size_t factor);
265 mem_pool(
size_t max=16*1024*
sizeof(
long),
size_t factor=16);
271 void* malloc(
size_t size,
size_t n=1);
275 size_t get_seg_cnt()
const
279 size_t get_max_chunk_size()
const
281 return _segs[_seg_cnt-1].chunk_size();
290 inline void pool_free(
void *p)
295 ch->
parent->parent()->free_chunk(ch);
298 detail::mmfree((
void*)ch);
303 inline void* pool_dup(
void *p)
306 detail::chunk_header* ch=(detail::chunk_header*)(p)-1;
311 detail::chunk_header* q=(detail::chunk_header*)detail::__mmdup((
void*)ch);
323 template<
typename pool_tag>mem_pool* get_pool_()
325 static mem_pool
alloc;
332 inline void* tmp_malloc(
size_t size)
334 return get_pool_<tmp_pool>()->malloc(size);
337 inline void* per_malloc(
size_t size)
339 return get_pool_<per_pool>()->malloc(size);
342 #define tmp_new(T,arg) (new(tmp_malloc(sizeof(T))) T arg)
343 #define per_new(T,arg) (new(per_malloc(sizeof(T))) T arg)
344 template<
typename T>
void pool_delete(T* p)
358 typedef size_t size_type;
359 typedef ptrdiff_t difference_type;
361 typedef const T* const_pointer;
362 typedef T& reference;
363 typedef const T& const_reference;
364 typedef T value_type;
365 template<
class U>
struct rebind{
381 static pointer address(reference x)
385 static const_pointer address(const_reference x)
390 static size_type max_size()
392 return size_type(-1);
395 static pointer allocate(size_type n)
397 static detail::seg_pool* meta=get_pool_<pool_tag>()->get_seg(
sizeof(value_type));
398 pointer r=(pointer)meta->malloc(
sizeof(T), n);
400 throw std::bad_alloc();
404 static pointer allocate(size_type n,
const void *
const)
409 static void deallocate(pointer p, size_type n)
437 static void construct(pointer p,
const T& val)
442 static void construct(pointer p)
447 static void destroy(pointer p)
452 bool operator==(
const smart_allocator &)
const
455 bool operator!=(
const smart_allocator &)
const
460 template<
class _Ty,
class _Other,
typename pool_type>
inline
461 bool operator==(
const smart_allocator<_Ty,pool_type>&,
const smart_allocator<_Other,pool_type>&)
466 template<
class _Ty,
class _Other,
typename pool_type>
inline
467 bool operator!=(
const smart_allocator<_Ty,pool_type>&,
const smart_allocator<_Other,pool_type>&)
472 template<
typename pool_tag>
class smart_allocator<void, pool_tag>
475 typedef void* pointer;
476 typedef const void* const_pointer;
477 typedef void value_type;
478 template <
class U>
struct rebind {
479 typedef smart_allocator<U, pool_tag> other;
487 #endif // PROTON_POOL_HEADER