summaryrefslogtreecommitdiff
path: root/core/allocators.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/allocators.h')
-rw-r--r--core/allocators.h144
1 files changed, 69 insertions, 75 deletions
diff --git a/core/allocators.h b/core/allocators.h
index 14deeb8739..de92d02226 100644
--- a/core/allocators.h
+++ b/core/allocators.h
@@ -30,13 +30,13 @@
#define ALLOCATORS_H
#include "os/memory.h"
-template<int PREALLOC_COUNT=64, int MAX_HANDS=8>
+template <int PREALLOC_COUNT = 64, int MAX_HANDS = 8>
class BalloonAllocator {
enum {
- USED_FLAG=(1<<30),
- USED_MASK=USED_FLAG-1
+ USED_FLAG = (1 << 30),
+ USED_MASK = USED_FLAG - 1
};
struct Balloon {
@@ -46,7 +46,6 @@ class BalloonAllocator {
uint32_t hand;
};
-
struct Hand {
int used;
@@ -55,136 +54,132 @@ class BalloonAllocator {
Balloon *last;
};
-
Hand hands[MAX_HANDS];
-
-
public:
+ void *alloc(size_t p_size) {
- void* alloc(size_t p_size) {
+ size_t max = (1 << MAX_HANDS);
+ ERR_FAIL_COND_V(p_size > max, NULL);
- size_t max=(1<<MAX_HANDS);
- ERR_FAIL_COND_V( p_size>max, NULL );
+ unsigned int hand = 0;
- unsigned int hand=0;
+ while (p_size > (size_t)(1 << hand))
+ ++hand;
- while(p_size>(size_t)(1<<hand)) ++hand;
+ Hand &h = hands[hand];
- Hand &h=hands[hand];
+ if (h.used == h.allocated) {
- if (h.used==h.allocated) {
+ for (int i = 0; i < PREALLOC_COUNT; i++) {
- for(int i=0;i<PREALLOC_COUNT;i++) {
-
- Balloon *b = (Balloon*)memalloc(sizeof(Balloon)+(1<<hand));
- b->hand=hand;
+ Balloon *b = (Balloon *)memalloc(sizeof(Balloon) + (1 << hand));
+ b->hand = hand;
if (h.last) {
- b->prev=h.last;
- h.last->next=b;
- h.last=b;
+ b->prev = h.last;
+ h.last->next = b;
+ h.last = b;
} else {
- b->prev=NULL;
- h.last=b;
- h.first=b;
+ b->prev = NULL;
+ h.last = b;
+ h.first = b;
}
}
- h.last->next=NULL;
- h.allocated+=PREALLOC_COUNT;
+ h.last->next = NULL;
+ h.allocated += PREALLOC_COUNT;
}
- Balloon *pick=h.last;
+ Balloon *pick = h.last;
- ERR_FAIL_COND_V( (pick->hand&USED_FLAG), NULL );
+ ERR_FAIL_COND_V((pick->hand & USED_FLAG), NULL);
// remove last
- h.last=h.last->prev;
- h.last->next=NULL;
+ h.last = h.last->prev;
+ h.last->next = NULL;
- pick->next=h.first;
- h.first->prev=pick;
- pick->prev=NULL;
- h.first=pick;
+ pick->next = h.first;
+ h.first->prev = pick;
+ pick->prev = NULL;
+ h.first = pick;
h.used++;
- pick->hand|=USED_FLAG;
+ pick->hand |= USED_FLAG;
- return (void*)(pick+1);
+ return (void *)(pick + 1);
}
- void free(void* p_ptr) {
+ void free(void *p_ptr) {
- Balloon *b=(Balloon*)p_ptr;
- b-=1;
+ Balloon *b = (Balloon *)p_ptr;
+ b -= 1;
- ERR_FAIL_COND(!(b->hand&USED_FLAG) );
+ ERR_FAIL_COND(!(b->hand & USED_FLAG));
- b->hand=b->hand&USED_MASK; // not used
- int hand=b->hand;
+ b->hand = b->hand & USED_MASK; // not used
+ int hand = b->hand;
- Hand &h=hands[hand];
+ Hand &h = hands[hand];
- if (b==h.first)
- h.first=b->next;
+ if (b == h.first)
+ h.first = b->next;
if (b->prev)
- b->prev->next=b->next;
+ b->prev->next = b->next;
if (b->next)
- b->next->prev=b->prev;
+ b->next->prev = b->prev;
- if (h.last!=b) {
- h.last->next=b;
- b->prev=h.last;
- b->next=NULL;
- h.last=b;
+ if (h.last != b) {
+ h.last->next = b;
+ b->prev = h.last;
+ b->next = NULL;
+ h.last = b;
}
h.used--;
- if (h.used<=(h.allocated-(PREALLOC_COUNT*2))) { // this is done to ensure no alloc/free is done constantly
+ if (h.used <= (h.allocated - (PREALLOC_COUNT * 2))) { // this is done to ensure no alloc/free is done constantly
- for(int i=0;i<PREALLOC_COUNT;i++) {
- ERR_CONTINUE( h.last->hand& USED_FLAG );
+ for (int i = 0; i < PREALLOC_COUNT; i++) {
+ ERR_CONTINUE(h.last->hand & USED_FLAG);
- Balloon *new_last=h.last->prev;
+ Balloon *new_last = h.last->prev;
if (new_last)
- new_last->next=NULL;
- memfree( h.last );
- h.last=new_last;
+ new_last->next = NULL;
+ memfree(h.last);
+ h.last = new_last;
}
- h.allocated-=PREALLOC_COUNT;
+ h.allocated -= PREALLOC_COUNT;
}
}
BalloonAllocator() {
- for(int i=0;i<MAX_HANDS;i++) {
+ for (int i = 0; i < MAX_HANDS; i++) {
- hands[i].allocated=0;
- hands[i].used=0;
- hands[i].first=NULL;
- hands[i].last=NULL;
+ hands[i].allocated = 0;
+ hands[i].used = 0;
+ hands[i].first = NULL;
+ hands[i].last = NULL;
}
-
}
void clear() {
- for(int i=0;i<MAX_HANDS;i++) {
+ for (int i = 0; i < MAX_HANDS; i++) {
- while(hands[i].first) {
+ while (hands[i].first) {
- Balloon *b=hands[i].first;
- hands[i].first=b->next;
+ Balloon *b = hands[i].first;
+ hands[i].first = b->next;
memfree(b);
}
- hands[i].allocated=0;
- hands[i].used=0;
- hands[i].first=NULL;
- hands[i].last=NULL;
+ hands[i].allocated = 0;
+ hands[i].used = 0;
+ hands[i].first = NULL;
+ hands[i].last = NULL;
}
}
@@ -194,5 +189,4 @@ public:
}
};
-
#endif // ALLOCATORS_H