32 #include "os/memory.h" 33 template<
int PREALLOC_COUNT=64,
int MAX_HANDS=8>
59 Hand hands[MAX_HANDS];
65 void* alloc(
size_t p_size) {
67 size_t max=(1<<MAX_HANDS);
68 ERR_FAIL_COND_V( p_size>max, NULL );
72 while(p_size>(
size_t)(1<<hand)) ++hand;
76 if (h.used==h.allocated) {
78 for(
int i=0;i<PREALLOC_COUNT;i++) {
80 Balloon *b = (Balloon*)memalloc(
sizeof(Balloon)+(1<<hand));
96 h.allocated+=PREALLOC_COUNT;
101 ERR_FAIL_COND_V( (pick->hand&USED_FLAG), NULL );
112 pick->hand|=USED_FLAG;
114 return (
void*)(pick+1);
117 void free(
void* p_ptr) {
119 Balloon *b=(Balloon*)p_ptr;
122 ERR_FAIL_COND(!(b->hand&USED_FLAG) );
124 b->hand=b->hand&USED_MASK;
133 b->prev->next=b->next;
135 b->next->prev=b->prev;
146 if (h.used<=(h.allocated-(PREALLOC_COUNT*2))) {
148 for(
int i=0;i<PREALLOC_COUNT;i++) {
149 ERR_CONTINUE( h.last->hand& USED_FLAG );
151 Balloon *new_last=h.last->prev;
157 h.allocated-=PREALLOC_COUNT;
163 for(
int i=0;i<MAX_HANDS;i++) {
165 hands[i].allocated=0;
175 for(
int i=0;i<MAX_HANDS;i++) {
177 while(hands[i].first) {
179 Balloon *b=hands[i].first;
180 hands[i].first=b->next;
184 hands[i].allocated=0;
198 #endif // ALLOCATORS_H Definition: allocators.h:34