2022-03-21 21:51:23 -04:00
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
ListAlloc* ListAlloc_Init(ListAlloc* this) {
|
|
|
|
this->prev = NULL;
|
|
|
|
this->next = NULL;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ListAlloc_Alloc(ListAlloc* this, size_t size) {
|
2022-07-05 19:29:34 -04:00
|
|
|
ListAlloc* ptr = SYSTEM_ARENA_MALLOC_DEBUG(size + sizeof(ListAlloc));
|
2022-03-21 21:51:23 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:29:34 -04:00
|
|
|
SYSTEM_ARENA_FREE_DEBUG(ptr);
|
2022-03-21 21:51:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListAlloc_FreeAll(ListAlloc* this) {
|
|
|
|
ListAlloc* iter = this->prev;
|
|
|
|
|
|
|
|
while (iter != NULL) {
|
|
|
|
ListAlloc_Free(this, (u8*)iter + sizeof(ListAlloc));
|
|
|
|
iter = this->prev;
|
|
|
|
}
|
|
|
|
}
|