2022-03-21 21:51:23 -04:00
|
|
|
#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) {
|
2022-07-05 19:29:34 -04:00
|
|
|
GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry), __FILE__, __LINE__);
|
2022-03-21 21:51:23 -04:00
|
|
|
|
|
|
|
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) {
|
2022-07-05 19:29:34 -04:00
|
|
|
GameAllocEntry* ptr = SYSTEM_ARENA_MALLOC_DEBUG(size + sizeof(GameAllocEntry));
|
2022-03-21 21:51:23 -04:00
|
|
|
|
|
|
|
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];
|
2022-07-05 19:29:34 -04:00
|
|
|
LOG_CHECK_NULL_POINTER("ptr->prev", ptr->prev);
|
|
|
|
LOG_CHECK_NULL_POINTER("ptr->next", ptr->next);
|
2022-03-21 21:51:23 -04:00
|
|
|
ptr->prev->next = ptr->next;
|
|
|
|
ptr->next->prev = ptr->prev;
|
|
|
|
this->head = this->base.prev;
|
2022-07-05 19:29:34 -04:00
|
|
|
SYSTEM_ARENA_FREE_DEBUG(ptr);
|
2022-03-21 21:51:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameAlloc_Cleanup(GameAlloc* this) {
|
|
|
|
GameAllocEntry* next = this->base.next;
|
|
|
|
GameAllocEntry* cur;
|
|
|
|
|
|
|
|
while (&this->base != next) {
|
|
|
|
cur = next;
|
|
|
|
next = next->next;
|
2022-07-05 19:29:34 -04:00
|
|
|
SYSTEM_ARENA_FREE_DEBUG(cur);
|
2022-03-21 21:51:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|