From 6ded207d0d2f6c1328c3b41eabb4680aa9b66a1c Mon Sep 17 00:00:00 2001 From: Cuyler36 Date: Wed, 31 May 2023 05:55:14 -0400 Subject: [PATCH] Implement & link gamealloc.c --- config/rel_slices.yml | 2 ++ include/gamealloc.h | 2 ++ rel/gamealloc.c | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 rel/gamealloc.c diff --git a/config/rel_slices.yml b/config/rel_slices.yml index 64ecbcf7..7e232bc2 100644 --- a/config/rel_slices.yml +++ b/config/rel_slices.yml @@ -79,6 +79,8 @@ THA_GA.c: .text: [0x80404AE0, 0x80404B40] TwoHeadArena.c: .text: [0x80404B40, 0x80404C68] +gamealloc.c: + .text: [0x80405370, 0x804054B0] gfxalloc.c: .text: [0x804054b0, 0x80405518] graph.c: diff --git a/include/gamealloc.h b/include/gamealloc.h index 1d503728..5101730f 100644 --- a/include/gamealloc.h +++ b/include/gamealloc.h @@ -21,6 +21,8 @@ typedef struct gameAlloc_s { /* 0x10 */ GameAllocList* tail; } GameAlloc; +extern void* gamealloc_malloc(GameAlloc* gamealloc, size_t size); + #ifdef __cplusplus }; #endif diff --git a/rel/gamealloc.c b/rel/gamealloc.c new file mode 100644 index 00000000..9d583f43 --- /dev/null +++ b/rel/gamealloc.c @@ -0,0 +1,55 @@ +#include "gamealloc.h" + +#include "libc64/malloc.h" + +extern void* gamealloc_malloc(GameAlloc* gamealloc, size_t size) { + GameAllocList* alloc = (GameAllocList*)malloc(size + sizeof(GameAllocList)); + + if (alloc != NULL) { + alloc->alloc_size = size; + alloc->prev = gamealloc->tail; + gamealloc->tail->next = alloc; + gamealloc->tail = alloc; + gamealloc->tail->next = &gamealloc->head; + gamealloc->head.prev = gamealloc->tail; + + return (void*)(alloc + 1); // data starts after GameAllocList structure + } + else { + return NULL; + } +} + +extern void gamealloc_free(GameAlloc* gamealloc, void* ptr) { + if (ptr != NULL) { + GameAllocList* alloc = ((GameAllocList*)ptr) - 1; // back up to GameAllocList struct + + alloc->prev->next = alloc->next; + alloc->next->prev = alloc->prev; + gamealloc->tail = gamealloc->head.prev; + + free(alloc); + } +} + +extern void gamealloc_cleanup(GameAlloc* gamealloc) { + GameAllocList* now_p = gamealloc->head.next; + GameAllocList* end_p = &gamealloc->head; + + /* Go through entire list and free each until we wrap around to the head list */ + while (now_p != end_p) { + GameAllocList* temp_p = now_p; + now_p = now_p->next; + free(temp_p); + } + + gamealloc->tail = end_p; + gamealloc->head.next = gamealloc->tail; + gamealloc->head.prev = gamealloc->tail; +} + +extern void gamealloc_init(GameAlloc* gamealloc) { + gamealloc->tail = &gamealloc->head; + gamealloc->head.next = gamealloc->tail; + gamealloc->head.prev = gamealloc->tail; +}