00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #include "fix_alloc.hpp"
00016
00017 namespace {
00018
00024 const size_t CHUNK_SIZE=4*1024;
00025
00029 const size_t SVP=sizeof(void*);
00030
00037 const size_t MAX_SIZE=128;
00038
00043 const size_t HEADS_NUM=(MAX_SIZE+SVP-1)/SVP;
00044
00049 void* heads[HEADS_NUM];
00050
00051 }
00052
00053 void fixed_alloc_private::get_mem(void*& head, size_t type_sz)
00054 {
00055 size_t n=(CHUNK_SIZE>type_sz) ? CHUNK_SIZE/type_sz : 1;
00056 head=operator new(n*type_sz);
00057
00058 char* last=(char*)head+(n-1)*type_sz;
00059 for (char* ptr=(char*)head; ; ptr+=type_sz) {
00060 if (ptr!=last) *(void**)ptr=ptr+type_sz;
00061 else {
00062 *(void**)ptr=0;
00063 break;
00064 }
00065 }
00066 }
00067
00068 void* sized_alloc::alloc(size_t size)
00069 {
00070 size_t index=(size+SVP-1)/SVP;
00071 if (index>=HEADS_NUM) return operator new(size);
00072
00073 void*& head=heads[index];
00074 if (!head) fixed_alloc_private::get_mem(head, index*SVP);
00075
00076 void* ret=head;
00077 head=*(void**)head;
00078
00079 return ret;
00080 }
00081
00082 void sized_alloc::free(void* ptr, size_t size)
00083 {
00084 size_t index=(size+SVP-1)/SVP;
00085 if (index>=HEADS_NUM) operator delete(ptr);
00086 else {
00087 void*& head=heads[index];
00088
00089 *(void**)ptr=head;
00090 head=ptr;
00091 }
00092 }