#include <mem_pool.hpp>
Public Member Functions | |
mem_pool (size_t max_chunk_size=2 *1024, size_t min_objs_in_chunk=4) | |
~mem_pool () | |
void * | allocate (size_t size) |
void | deallocate (void *ptr, size_t size) |
Definition at line 28 of file mem_pool.hpp.
ders::mem_pool::mem_pool | ( | size_t | max_chunk_size = 2*1024 , |
|
size_t | min_objs_in_chunk = 4 | |||
) |
Definition at line 102 of file mem_pool.cpp.
00102 : 00103 impl(new mp_impl(max_chunk_size, min_objs_in_chunk)) 00104 { 00105 }
ders::mem_pool::~mem_pool | ( | ) |
Definition at line 107 of file mem_pool.cpp.
00108 { 00109 #ifndef NDEBUG 00110 for (int i=0; i<IMPL->allocated.size(); i++) { 00111 fprintf(stderr, "(%p,%d)", IMPL->allocated.key(i), (int)IMPL->allocated. 00112 val(i)); 00113 } 00114 00115 assert(IMPL->allocated.size()==0); 00116 #endif 00117 delete IMPL; 00118 }
void * ders::mem_pool::allocate | ( | size_t | size | ) |
Definition at line 120 of file mem_pool.cpp.
00121 { 00122 assert(size>0); 00123 void* ret; 00124 00125 if (size<=mp_impl::MOS1) { 00126 void*& head=IMPL->heads1[(size-1)/mp_impl::SZVP]; 00127 if (!head) IMPL->format_new_chunk(head, size, mp_impl::SZVP); 00128 00129 ret=head; 00130 head=*reinterpret_cast<void**>(head); 00131 } 00132 else if (size<=mp_impl::MOS2) { 00133 void*& head=IMPL->heads2[(size-mp_impl::MOS1-1)/mp_impl::SZVP8]; 00134 if (!head) IMPL->format_new_chunk(head, size, mp_impl::SZVP8); 00135 00136 ret=head; 00137 head=*reinterpret_cast<void**>(head); 00138 } 00139 else ret=operator new(size); 00140 00141 #ifndef NDEBUG 00142 int pos=IMPL->allocated.insert(ret, size); 00143 assert(pos!=-1); 00144 #endif 00145 return ret; 00146 }
void ders::mem_pool::deallocate | ( | void * | ptr, | |
size_t | size | |||
) |
Definition at line 148 of file mem_pool.cpp.
00149 { 00150 #ifndef NDEBUG 00151 int pos=IMPL->allocated.find(ptr); 00152 assert(pos!=-1); 00153 assert(IMPL->allocated.val(pos)==size); 00154 IMPL->allocated.erase(pos); 00155 #endif 00156 00157 if (size<=mp_impl::MOS1) { 00158 void*& head=IMPL->heads1[(size-1)/mp_impl::SZVP]; 00159 00160 *reinterpret_cast<void**>(ptr)=head; 00161 head=ptr; 00162 } 00163 else if (size<=mp_impl::MOS2) { 00164 void*& head=IMPL->heads2[(size-mp_impl::MOS1-1)/mp_impl::SZVP8]; 00165 00166 *reinterpret_cast<void**>(ptr)=head; 00167 head=ptr; 00168 } 00169 else operator delete(ptr); 00170 }