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/listalloc.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/listalloc.c')
| -rw-r--r-- | soh/src/code/listalloc.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/soh/src/code/listalloc.c b/soh/src/code/listalloc.c new file mode 100644 index 000000000..18d43328d --- /dev/null +++ b/soh/src/code/listalloc.c @@ -0,0 +1,62 @@ +#include "global.h" + +ListAlloc* ListAlloc_Init(ListAlloc* this) { + this->prev = NULL; + this->next = NULL; + return this; +} + +void* ListAlloc_Alloc(ListAlloc* this, size_t size) { + ListAlloc* ptr = SystemArena_MallocDebug(size + sizeof(ListAlloc), "../listalloc.c", 40); + ListAlloc* next; + + if (ptr == NULL) { + return NULL; + } + + next = this->next; + if (next != NULL) { + next->next = ptr; + } + + ptr->prev = next; + ptr->next = NULL; + this->next = ptr; + + if (this->prev == NULL) { + this->prev = ptr; + } + + return (u8*)ptr + sizeof(ListAlloc); +} + +void ListAlloc_Free(ListAlloc* this, void* data) { + ListAlloc* ptr = &((ListAlloc*)data)[-1]; + + if (ptr->prev != NULL) { + ptr->prev->next = ptr->next; + } + + if (ptr->next != NULL) { + ptr->next->prev = ptr->prev; + } + + if (this->prev == ptr) { + this->prev = ptr->next; + } + + if (this->next == ptr) { + this->next = ptr->prev; + } + + SystemArena_FreeDebug(ptr, "../listalloc.c", 72); +} + +void ListAlloc_FreeAll(ListAlloc* this) { + ListAlloc* iter = this->prev; + + while (iter != NULL) { + ListAlloc_Free(this, (u8*)iter + sizeof(ListAlloc)); + iter = this->prev; + } +} |
