diff options
| author | M4xw <m4x@m4xw.net> | 2022-03-22 02:51:23 +0100 |
|---|---|---|
| committer | M4xw <m4x@m4xw.net> | 2022-03-22 02:51:23 +0100 |
| commit | 39cc86c2608e2f75ace33fc7f43bc4f2ad743f1a (patch) | |
| tree | 0d3fd9e995d7484eaaeb158ac808bd6a39121189 /soh/src/code/gamealloc.c | |
| parent | 0bb0e7b53bd80bdc7f78e08c441691737e039b2b (diff) | |
git subrepo clone https://github.com/HarbourMasters/soh.git
subrepo:
subdir: "soh"
merged: "ba904bbd0"
upstream:
origin: "https://github.com/HarbourMasters/soh.git"
branch: "master"
commit: "ba904bbd0"
git-subrepo:
version: "0.4.1"
origin: "???"
commit: "???"
Diffstat (limited to 'soh/src/code/gamealloc.c')
| -rw-r--r-- | soh/src/code/gamealloc.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/soh/src/code/gamealloc.c b/soh/src/code/gamealloc.c new file mode 100644 index 000000000..451c56de2 --- /dev/null +++ b/soh/src/code/gamealloc.c @@ -0,0 +1,80 @@ +#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, size_t size, const char* file, s32 line) { + GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry), file, line); + + if (ptr != NULL) { + 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, size_t size) { + GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry), "../gamealloc.c", 93); + + if (ptr != NULL) { + 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 != NULL) { + 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 = this->base.next; + GameAllocEntry* cur; + + 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; +} |
