From bd0c96e41818de39d243604f2a56bdee779956f2 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sun, 6 Aug 2023 14:14:33 -0400 Subject: [PATCH] `gamealloc.c` (#33) * C file * match file * review --- include/gamealloc.h | 23 +++++++++++++---- include/malloc.h | 2 +- src/code/gamealloc.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ yamls/jp/code.yaml | 2 +- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/code/gamealloc.c diff --git a/include/gamealloc.h b/include/gamealloc.h index 5c4d0c87..2d7aa9df 100644 --- a/include/gamealloc.h +++ b/include/gamealloc.h @@ -2,11 +2,24 @@ #define GAMEALLOC_H #include "ultra64.h" +#include "unk.h" -// void func_800D3720_jp(); -// void gamealloc_malloc(); -// void gamealloc_free(); -// void gamealloc_cleanup(); -// void gamealloc_init(); +typedef struct GameAllocEntry { + /* 0x0 */ struct GameAllocEntry* next; + /* 0x4 */ struct GameAllocEntry* prev; + /* 0x8 */ size_t size; + /* 0xC */ UNK_TYPE1 unk_0C[0x4]; +} GameAllocEntry; // size = 0x10 + +typedef struct GameAlloc { + /* 0x00 */ GameAllocEntry base; + /* 0x10 */ GameAllocEntry* head; +} GameAlloc; // size = 0x14 + +void func_800D3720_jp(GameAlloc* this); +void* gamealloc_malloc(GameAlloc* this, size_t size); +void gamealloc_free(GameAlloc* this, void* ptr); +void gamealloc_cleanup(GameAlloc* this); +void gamealloc_init(GameAlloc* this); #endif diff --git a/include/malloc.h b/include/malloc.h index 5a948163..dfc2bc40 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -3,7 +3,7 @@ #include "ultra64.h" -// void malloc(); +void* malloc(size_t size); void* malloc_r(size_t size); // void realloc(); void free(void* ptr); diff --git a/src/code/gamealloc.c b/src/code/gamealloc.c new file mode 100644 index 00000000..f953028b --- /dev/null +++ b/src/code/gamealloc.c @@ -0,0 +1,60 @@ +#include "gamealloc.h" +#include "malloc.h" +#include "libc/stdint.h" + +void func_800D3720_jp(GameAlloc* this) { + GameAllocEntry* iter = this->base.next; + + while (&this->base != iter) { + iter = iter->next; + } +} + +void* gamealloc_malloc(GameAlloc* this, size_t size) { + GameAllocEntry* ptr = malloc(size + sizeof(GameAllocEntry)); + + 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; + } + + return NULL; +} + +void gamealloc_free(GameAlloc* this, void* ptr) { + if (ptr != NULL) { + GameAllocEntry* entry = (GameAllocEntry*)ptr - 1; + + entry->prev->next = entry->next; + entry->next->prev = entry->prev; + this->head = this->base.prev; + free(entry); + } +} + +void gamealloc_cleanup(GameAlloc* this) { + GameAllocEntry* iter = this->base.next; + + while (&this->base != iter) { + GameAllocEntry* cur = iter; + + iter = iter->next; + free(cur); + } + + 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; +} diff --git a/yamls/jp/code.yaml b/yamls/jp/code.yaml index e7f6f92c..30d1e174 100644 --- a/yamls/jp/code.yaml +++ b/yamls/jp/code.yaml @@ -84,7 +84,7 @@ - [0x6F6230, asm, code/audioMgr] - [0x6F68B0, asm, code/cfbinfo] - [0x6F6AA0, asm, code/game] - - [0x6F73C0, asm, code/gamealloc] + - [0x6F73C0, c, code/gamealloc] - [0x6F7500, asm, code/gfxalloc] - [0x6F7580, asm, code/graph] - [0x6F83B0, c, code/irqmgr]