diff options
| author | Jack Walker <7463599+Jack-Walker@users.noreply.github.com> | 2020-03-17 00:31:30 -0400 |
|---|---|---|
| committer | Jack Walker <7463599+Jack-Walker@users.noreply.github.com> | 2020-03-17 00:31:30 -0400 |
| commit | 087f561f7786a812c815b9198f24e6acd0477497 (patch) | |
| tree | 8e8b1efeb798ff0b341d39de024d7b8554013c4a /src/code/gamealloc.c | |
| parent | be78236d36a5eb4ef80acef6188751f6b5d176ae (diff) | |
First proper commit.
Diffstat (limited to 'src/code/gamealloc.c')
| -rw-r--r-- | src/code/gamealloc.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/code/gamealloc.c b/src/code/gamealloc.c new file mode 100644 index 000000000..87a32db16 --- /dev/null +++ b/src/code/gamealloc.c @@ -0,0 +1,95 @@ +#include <global.h> + +void GameAlloc_Log(GameAlloc* this) +{ + GameAllocEntry* iter; + + osSyncPrintf("this = %08x\n", this); + + iter = this->base.next; + while (iter != &this->base) + { + osSyncPrintf("ptr = %08x size = %d\n", iter, iter->size); + iter = iter->next; + } +} + +void* GameAlloc_MallocDebug(GameAlloc* this, u32 size, const char* file, s32 line) +{ + GameAllocEntry* ptr; + + ptr = SystemArena_MallocDebug(size+sizeof(GameAllocEntry), file, line); + if (ptr) + { + ptr->size = size; + ptr->prev = this->head; + this->head->next = ptr; + this->head = ptr; + ptr->next = &this->base; + this->base.prev = this->head; + return ptr + 1; + } + else + return NULL; +} + +void* GameAlloc_Malloc(GameAlloc* this, u32 size) +{ + GameAllocEntry* ptr; + + ptr = SystemArena_MallocDebug(size+sizeof(GameAllocEntry), "../gamealloc.c", 93); + if (ptr) + { + ptr->size = size; + ptr->prev = this->head; + this->head->next = ptr; + this->head = ptr; + ptr->next = &this->base; + this->base.prev = this->head; + return ptr + 1; + } + else + return NULL; +} + +void GameAlloc_Free(GameAlloc* this, void* data) +{ + GameAllocEntry* ptr; + + if (data) + { + ptr = &((GameAllocEntry*)data)[-1]; + LogUtils_CheckNullPointer("ptr->prev", ptr->prev, "../gamealloc.c", 120); + LogUtils_CheckNullPointer("ptr->next", ptr->next, "../gamealloc.c", 121); + ptr->prev->next = ptr->next; + ptr->next->prev = ptr->prev; + this->head = this->base.prev; + SystemArena_FreeDebug(ptr, "../gamealloc.c", 125); + } +} + +void GameAlloc_Cleanup(GameAlloc* this) +{ + GameAllocEntry* next; + GameAllocEntry* cur; + + next = this->base.next; + while (&this->base != next) + { + cur = next; + next = next->next; + SystemArena_FreeDebug(cur, "../gamealloc.c", 145); + } + + this->head = &this->base; + this->base.next = &this->base; + this->base.prev = &this->base; + +} + +void GameAlloc_Init(GameAlloc* this) +{ + this->head = &this->base; + this->base.next = &this->base; + this->base.prev = &this->base; +} |
