Shipwright/soh/src/code/listalloc.c
Baoulettes a5df9dddf0
Use Macro for __FILE__ & __LINE__ when possible (#559)
* First batch some overlay

* Almost all overlay

* effect & gamestate

* kaleido stuffs

* more overlay

* more left over from code folder

* remaining hardcoded line and file

* Open & Close _DISP __FILE__ & __LINE__ clean up

* Some if (1) {} remove

* LOG_xxxx __FILE__ , __LINE__ cleaned

* ASSERT macro __FILE__ __LINE__

* mtx without line/file in functions

* " if (1) {} " & "if (0) {}" and tab/white place

* LogUtils as macro

* GameState_, GameAlloc_, SystemArena_ & ZeldaArena_

* Revert "GameState_, GameAlloc_, SystemArena_ & ZeldaArena_"

This reverts commit 0d85caaf7e.

* Like last commit but as macro

* Fix matrix not using macros

* use function not macro

* DebugArena_* functions
GameAlloc_MallocDebug
BgCheck_PosErrorCheck as macros
removed issues with ; in macro file
2022-07-05 19:29:34 -04:00

63 lines
1.2 KiB
C

#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 = SYSTEM_ARENA_MALLOC_DEBUG(size + sizeof(ListAlloc));
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;
}
SYSTEM_ARENA_FREE_DEBUG(ptr);
}
void ListAlloc_FreeAll(ListAlloc* this) {
ListAlloc* iter = this->prev;
while (iter != NULL) {
ListAlloc_Free(this, (u8*)iter + sizeof(ListAlloc));
iter = this->prev;
}
}