Skip to content

Commit

Permalink
A Whole New World (clang-format edition)
Browse files Browse the repository at this point in the history
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?

I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon

A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format

A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
  • Loading branch information
akien-mga committed Mar 5, 2017
1 parent 45438e9 commit 5dbf180
Show file tree
Hide file tree
Showing 1,318 changed files with 140,937 additions and 166,890 deletions.
144 changes: 69 additions & 75 deletions core/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -46,7 +46,6 @@ class BalloonAllocator {
uint32_t hand;
};


struct Hand {

int used;
Expand All @@ -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;
}
}

Expand All @@ -194,5 +189,4 @@ class BalloonAllocator {
}
};


#endif // ALLOCATORS_H
Loading

2 comments on commit 5dbf180

@Faless
Copy link
Collaborator

@Faless Faless commented on 5dbf180 May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, I just read the commit message.
Simply awesome 🏆 !

@GarmOfGnipahellir
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found this commit and I have to say that the message is pure gold! 🥇

Please sign in to comment.