summaryrefslogtreecommitdiff
path: root/arm9/lib/src/OS_alloc.c
diff options
context:
space:
mode:
authorProjectRevoTPP <projectrevotpp@hotmail.com>2020-05-01 03:06:54 -0400
committerProjectRevoTPP <projectrevotpp@hotmail.com>2020-05-01 03:06:54 -0400
commitb950f6a92302af19769a76006a9a83c68b1a47c6 (patch)
tree05fa57262f1f8d90fcb443becd59d553b66d6f52 /arm9/lib/src/OS_alloc.c
parentc8a06f894bf98b9fa12759043cbd6316e30c59dc (diff)
migrate and match OS_alloc. Thanks Demki!
Diffstat (limited to 'arm9/lib/src/OS_alloc.c')
-rw-r--r--arm9/lib/src/OS_alloc.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/arm9/lib/src/OS_alloc.c b/arm9/lib/src/OS_alloc.c
new file mode 100644
index 00000000..242c5571
--- /dev/null
+++ b/arm9/lib/src/OS_alloc.c
@@ -0,0 +1,169 @@
+//
+// Created by mart on 4/23/20.
+//
+#include "function_target.h"
+#include "os_alloc.h"
+#include "consts.h"
+#include "os_system.h"
+
+void* OSiHeapInfo[OS_ARENA_MAX];
+
+ARM_FUNC Cell* DLAddFront(Cell* list, Cell* cell)
+{
+ cell->next = list;
+ cell->prev = NULL;
+
+ if (list != NULL)
+ list->prev = cell;
+ return cell;
+}
+
+ARM_FUNC Cell* DLExtract(Cell* list, Cell* cell)
+{
+ if (cell->next) {
+ cell->next->prev = cell->prev;
+ }
+ if (cell->prev == NULL) {
+ list = cell->next;
+ } else {
+ cell->prev->next = cell->next;
+ }
+ return list;
+}
+
+ARM_FUNC Cell *DLInsert(Cell *original, Cell *inserted)
+{
+ Cell *prevCell = NULL;
+ Cell *nextCell = original;
+
+
+ while (nextCell != NULL)
+ {
+ if (inserted <= nextCell)
+ break;
+ prevCell = nextCell;
+ nextCell = nextCell->next;
+ }
+
+ inserted->next = nextCell;
+ inserted->prev = prevCell;
+
+ if (nextCell != NULL)
+ {
+ nextCell->prev = inserted;
+ Cell * temp = (Cell *)((char *)inserted + inserted->size);
+ if (temp == nextCell)
+ {
+ inserted->size += nextCell->size;
+ nextCell = nextCell->next;
+ inserted->next = nextCell;
+ if (nextCell != NULL)
+ nextCell->prev = inserted;
+ }
+ }
+
+ if (prevCell != NULL)
+ {
+ prevCell->next = inserted;
+ Cell * temp = (Cell *)((char *)prevCell + prevCell->size);
+
+ if (temp != inserted)
+ return original;
+
+ prevCell->size += inserted->size;
+ prevCell->next = nextCell;
+ if (nextCell != NULL)
+ nextCell->prev = prevCell;
+
+ return original;
+ }
+
+ return inserted;
+}
+
+#define HEADERSIZE OSi_ROUND(sizeof(Cell), 32)
+#define MINOBJSIZE (HEADERSIZE+32)
+
+ARM_FUNC void* OS_AllocFromHeap(OSArenaId id, OSHeapHandle heap, u32 size) {
+ OSHeapInfo* heapInfo;
+ HeapDesc* hd;
+ Cell* cell;
+ Cell* newCell;
+ long leftoverSize;
+
+ OSIntrMode enabled = OS_DisableInterrupts();
+ heapInfo = OSiHeapInfo[id];
+ if (!heapInfo) {
+ OS_RestoreInterrupts(enabled);
+ return NULL;
+ }
+
+ if (heap < 0) {
+ heap = heapInfo->currentHeap;
+ }
+
+ hd = &heapInfo->heapArray[heap];
+
+ size += HEADERSIZE;
+ size = OSi_ROUND(size, 32);
+
+ for (cell = hd->free; cell != NULL; cell = cell->next) {
+ if ((long)size <= cell->size) {
+ break;
+ }
+ }
+
+ if (cell == NULL) {
+ OS_RestoreInterrupts(enabled);
+ return NULL;
+ }
+
+ leftoverSize = cell->size - (long)size;
+ if (leftoverSize < MINOBJSIZE) {
+ hd->free = DLExtract(hd->free, cell);
+ } else {
+ cell->size = (long)size;
+
+ newCell = (Cell *) ((char *)cell + size);
+ newCell->size = leftoverSize;
+
+ newCell->prev = cell->prev;
+ newCell->next = cell->next;
+
+ if (newCell->next != NULL) {
+ newCell->next->prev = newCell;
+ }
+
+ if (newCell->prev != NULL) {
+ newCell->prev->next = newCell;
+ } else {
+ hd->free = newCell;
+ }
+ }
+
+ hd->allocated = DLAddFront(hd->allocated, cell);
+
+ OS_RestoreInterrupts(enabled);
+ return (void *)((char *)cell + HEADERSIZE);
+}
+
+ARM_FUNC void OS_FreeToHeap(OSArenaId id, OSHeapHandle heap, void* ptr) {
+ OSHeapInfo *heapInfo;
+ HeapDesc *hd;
+ Cell *cell;
+
+ OSIntrMode enabled = OS_DisableInterrupts();
+ heapInfo = OSiHeapInfo[id];
+
+ if (heap < 0) {
+ heap = heapInfo->currentHeap;
+ }
+
+ cell = (Cell *) ((char *)ptr - HEADERSIZE);
+ hd = &heapInfo->heapArray[heap];
+
+ hd->allocated = DLExtract(hd->allocated, cell);
+ hd->free = DLInsert(hd->free, cell);
+
+ OS_RestoreInterrupts(enabled);
+}