summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPrakxo <87568477+Prakxo@users.noreply.github.com>2024-03-27 17:55:05 +0100
committerGitHub <noreply@github.com>2024-03-27 09:55:05 -0700
commit540648ef26d6dbdd4277bd4bdd8a38e32e8569ad (patch)
treeb708af79f4843a47daccc154aee8ff7057391e45 /src
parent4b081155a9f62ccc0bf46245cd2219cfb9324cd9 (diff)
listalloc OK (#158)
Diffstat (limited to 'src')
-rw-r--r--src/code/listalloc.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/code/listalloc.c b/src/code/listalloc.c
new file mode 100644
index 0000000..e6b62ee
--- /dev/null
+++ b/src/code/listalloc.c
@@ -0,0 +1,63 @@
+#include "listalloc.h"
+#include "libc64/malloc.h"
+
+ListAlloc* ListAlloc_Init(ListAlloc* this) {
+ this->prev = NULL;
+ this->next = NULL;
+ return this;
+}
+
+void* ListAlloc_Alloc(ListAlloc* this, size_t size) {
+ ListAlloc* ptr = malloc(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;
+ }
+
+ free(ptr);
+}
+
+void ListAlloc_FreeAll(ListAlloc* this) {
+ ListAlloc* iter = this->prev;
+
+ while (iter != NULL) {
+ ListAlloc_Free(this, (u8*)iter + sizeof(ListAlloc));
+ iter = this->prev;
+ }
+}