summaryrefslogtreecommitdiff
path: root/src/code/listalloc.c
diff options
context:
space:
mode:
authorJack Walker <7463599+Jack-Walker@users.noreply.github.com>2020-03-17 00:31:30 -0400
committerJack Walker <7463599+Jack-Walker@users.noreply.github.com>2020-03-17 00:31:30 -0400
commit087f561f7786a812c815b9198f24e6acd0477497 (patch)
tree8e8b1efeb798ff0b341d39de024d7b8554013c4a /src/code/listalloc.c
parentbe78236d36a5eb4ef80acef6188751f6b5d176ae (diff)
First proper commit.
Diffstat (limited to 'src/code/listalloc.c')
-rw-r--r--src/code/listalloc.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/code/listalloc.c b/src/code/listalloc.c
new file mode 100644
index 000000000..20cc88e1c
--- /dev/null
+++ b/src/code/listalloc.c
@@ -0,0 +1,65 @@
+#include <global.h>
+
+ListAlloc* ListAlloc_Init(ListAlloc* this)
+{
+ this->prev = NULL;
+ this->next = NULL;
+ return this;
+}
+
+void* ListAlloc_Alloc(ListAlloc* this, u32 size)
+{
+ ListAlloc* ptr;
+ ListAlloc* next;
+
+ ptr = SystemArena_MallocDebug(size + sizeof(ListAlloc), "../listalloc.c", 40);
+ if (!ptr)
+ return NULL;
+
+ next = this->next;
+
+ if (next)
+ next->next = ptr;
+
+ ptr->prev = next;
+ ptr->next = NULL;
+ this->next = ptr;
+
+ if (!this->prev)
+ this->prev = ptr;
+
+ return (u8*)ptr + sizeof(ListAlloc);
+}
+
+void ListAlloc_Free(ListAlloc* this, void* data)
+{
+ ListAlloc* ptr;
+
+ ptr = &((ListAlloc*)data)[-1];
+
+ if (ptr->prev)
+ ptr->prev->next = ptr->next;
+
+ if (ptr->next)
+ ptr->next->prev = ptr->prev;
+
+ if (this->prev == ptr)
+ this->prev = ptr->next;
+
+ if (this->next == ptr)
+ this->next = ptr->prev;
+
+ SystemArena_FreeDebug(ptr, "../listalloc.c", 72);
+}
+
+void ListAlloc_FreeAll(ListAlloc* this)
+{
+ ListAlloc* iter;
+
+ iter = this->prev;
+ while (iter)
+ {
+ ListAlloc_Free(this, (u8*)iter + sizeof(ListAlloc));
+ iter = this->prev;
+ }
+}