diff options
author | Cleverking2003 <30466983+Cleverking2003@users.noreply.github.com> | 2020-05-01 10:38:55 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 10:38:55 +0300 |
commit | 90d38f053ff21a5d6cd967de29dc440cbba9aa29 (patch) | |
tree | 05fa57262f1f8d90fcb443becd59d553b66d6f52 | |
parent | c8a06f894bf98b9fa12759043cbd6316e30c59dc (diff) | |
parent | b950f6a92302af19769a76006a9a83c68b1a47c6 (diff) |
Merge pull request #49 from ProjectRevoTPP/master
migrate and match OS_alloc.c
-rw-r--r-- | arm9/asm/OS_alloc.s | 202 | ||||
-rw-r--r-- | arm9/lib/include/os_system.h | 15 | ||||
-rw-r--r-- | arm9/lib/src/OS_alloc.c | 169 | ||||
-rw-r--r-- | arm9/undefined_syms.txt | 2 | ||||
-rw-r--r-- | include/nitro/os_system.h | 15 | ||||
-rw-r--r-- | undefined_syms.txt | 2 |
6 files changed, 175 insertions, 230 deletions
diff --git a/arm9/asm/OS_alloc.s b/arm9/asm/OS_alloc.s deleted file mode 100644 index 3eaea88f..00000000 --- a/arm9/asm/OS_alloc.s +++ /dev/null @@ -1,202 +0,0 @@ - .include "asm/macros.inc" - .include "global.inc" - - .text - - arm_func_start OS_FreeToHeap -OS_FreeToHeap: ; 0x020CC6F8 - stmdb sp!, {r4-r7,lr} - sub sp, sp, #0x4 - mov r7, r0 - mov r5, r1 - mov r4, r2 - bl OS_DisableInterrupts - ldr r1, _020CC768 ; =OSi_HeapInfo - mov r6, r0 - ldr r0, [r1, r7, lsl #0x2] - cmp r5, #0x0 - ldrlt r5, [r0, #0x0] - ldr r1, [r0, #0x10] - mov r0, #0xc - mla r7, r5, r0, r1 - sub r4, r4, #0x20 - ldr r0, [r7, #0x8] - mov r1, r4 - bl DLExtract - str r0, [r7, #0x8] - ldr r0, [r7, #0x4] - mov r1, r4 - bl DLInsert - str r0, [r7, #0x4] - mov r0, r6 - bl OS_RestoreInterrupts - add sp, sp, #0x4 - ldmia sp!, {r4-r7,lr} - bx lr - .balign 4 -_020CC768: .word OSi_HeapInfo - - arm_func_start OS_AllocFromHeap -OS_AllocFromHeap: ; 0x020CC76C - stmdb sp!, {r4-r7,lr} - sub sp, sp, #0x4 - mov r6, r0 - mov r5, r1 - mov r7, r2 - bl OS_DisableInterrupts - ldr r1, _020CC894 ; =OSi_HeapInfo - mov r4, r0 - ldr r1, [r1, r6, lsl #0x2] - cmp r1, #0x0 - bne _020CC7AC - bl OS_RestoreInterrupts - add sp, sp, #0x4 - mov r0, #0x0 - ldmia sp!, {r4-r7,lr} - bx lr -_020CC7AC: - cmp r5, #0x0 - ldrlt r5, [r1, #0x0] - ldr r1, [r1, #0x10] - mov r0, #0xc - mla r6, r5, r0, r1 - ldr r0, [r6, #0x4] - add r1, r7, #0x20 - add r1, r1, #0x1f - mov r5, r0 - cmp r0, #0x0 - bic r7, r1, #0x1f - beq _020CC7F4 -_020CC7DC: - ldr r1, [r5, #0x8] - cmp r7, r1 - ble _020CC7F4 - ldr r5, [r5, #0x4] - cmp r5, #0x0 - bne _020CC7DC -_020CC7F4: - cmp r5, #0x0 - bne _020CC814 - mov r0, r4 - bl OS_RestoreInterrupts - add sp, sp, #0x4 - mov r0, #0x0 - ldmia sp!, {r4-r7,lr} - bx lr -_020CC814: - ldr r1, [r5, #0x8] - sub r1, r1, r7 - cmp r1, #0x40 - bhs _020CC834 - mov r1, r5 - bl DLExtract - str r0, [r6, #0x4] - b _020CC86C -_020CC834: - str r7, [r5, #0x8] - add r2, r5, r7 - str r1, [r2, #0x8] - ldr r0, [r5, #0x0] - str r0, [r5, r7] - ldr r0, [r5, #0x4] - str r0, [r2, #0x4] - ldr r0, [r2, #0x4] - cmp r0, #0x0 - strne r2, [r0, #0x0] - ldr r0, [r2, #0x0] - cmp r0, #0x0 - strne r2, [r0, #0x4] - streq r2, [r6, #0x4] -_020CC86C: - ldr r0, [r6, #0x8] - mov r1, r5 - bl DLAddFront - str r0, [r6, #0x8] - mov r0, r4 - bl OS_RestoreInterrupts - add r0, r5, #0x20 - add sp, sp, #0x4 - ldmia sp!, {r4-r7,lr} - bx lr - .balign 4 -_020CC894: .word OSi_HeapInfo - - arm_func_start DLInsert -DLInsert: ; 0x020CC898 - stmdb sp!, {lr} - sub sp, sp, #0x4 - mov r12, r0 - cmp r0, #0x0 - mov lr, #0x0 - beq _020CC8C8 -_020CC8B0: - cmp r1, r12 - bls _020CC8C8 - mov lr, r12 - ldr r12, [r12, #0x4] - cmp r12, #0x0 - bne _020CC8B0 -_020CC8C8: - str r12, [r1, #0x4] - str lr, [r1, #0x0] - cmp r12, #0x0 - beq _020CC908 - str r1, [r12, #0x0] - ldr r3, [r1, #0x8] - add r2, r1, r3 - cmp r2, r12 - bne _020CC908 - ldr r2, [r12, #0x8] - add r2, r3, r2 - str r2, [r1, #0x8] - ldr r12, [r12, #0x4] - str r12, [r1, #0x4] - cmp r12, #0x0 - strne r1, [r12, #0x0] -_020CC908: - cmp lr, #0x0 - beq _020CC950 - str r1, [lr, #0x4] - ldr r2, [lr, #0x8] - add r3, lr, r2 - cmp r3, r1 - addne sp, sp, #0x4 - ldmneia sp!, {lr} - bxne lr - ldr r1, [r1, #0x8] - add sp, sp, #0x4 - add r1, r2, r1 - str r1, [lr, #0x8] - str r12, [lr, #0x4] - cmp r12, #0x0 - strne lr, [r12, #0x0] - ldmia sp!, {lr} - bx lr -_020CC950: - mov r0, r1 - add sp, sp, #0x4 - ldmia sp!, {lr} - bx lr - - arm_func_start DLExtract -DLExtract: ; 0x020CC960 - ldr r3, [r1, #0x4] - cmp r3, #0x0 - ldrne r2, [r1, #0x0] - strne r2, [r3, #0x0] - ldr r2, [r1, #0x0] - cmp r2, #0x0 - ldreq r0, [r1, #0x4] - ldrne r1, [r1, #0x4] - strne r1, [r2, #0x4] - bx lr - - arm_func_start DLAddFront -DLAddFront: ; 0x020CC988 - str r0, [r1, #0x4] - mov r2, #0x0 - str r2, [r1, #0x0] - cmp r0, #0x0 - strne r1, [r0, #0x0] - mov r0, r1 - bx lr diff --git a/arm9/lib/include/os_system.h b/arm9/lib/include/os_system.h index fc6a2f81..c7f121bd 100644 --- a/arm9/lib/include/os_system.h +++ b/arm9/lib/include/os_system.h @@ -18,18 +18,8 @@ typedef enum { } OSProcMode; typedef enum { - OS_INTRMODE_IRQ_DISABLE = HW_PSR_IRQ_DISABLE, - OS_INTRMODE_IRQ_ENABLE = 0 -} OSIntrMode_Irq; - -typedef enum { - OS_INTRMODE_FIQ_DISABLE = HW_PSR_FIQ_DISABLE, - OS_INTRMODE_FIQ_ENABLE = 0 -} OSIntrMode_Fiq; - -typedef union { - OSIntrMode_Fiq mode_fiq; - OSIntrMode_Irq mode_irq; + OS_INTRMODE_DISABLE = HW_PSR_IRQ_DISABLE, + OS_INTRMODE_ENABLE = 0 } OSIntrMode; OSIntrMode OS_EnableInterrupts(); @@ -37,7 +27,6 @@ OSIntrMode OS_DisableInterrupts(); OSIntrMode OS_RestoreInterrupts(OSIntrMode state); OSIntrMode OS_DisableInterrupts_IrqAndFiq(); OSIntrMode OS_RestoreInterrupts_IrqAndFiq(OSIntrMode state); -OSIntrMode_Irq OS_GetCpsrIrq(); OSProcMode OS_GetProcMode(); 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);
+}
diff --git a/arm9/undefined_syms.txt b/arm9/undefined_syms.txt index 8abcab21..c8a8097a 100644 --- a/arm9/undefined_syms.txt +++ b/arm9/undefined_syms.txt @@ -35,7 +35,7 @@ OSi_LauncherThread = 0x021D3568; OSi_IdleThreadStack = 0x021D3628; OSi_Initialized = 0x021D36F0; OSi_MainExArenaEnabled = 0x021D36F4; -OSi_HeapInfo = 0x021D36F8; +OSiHeapInfo = 0x021D36F8; OSi_UserExceptionHandler = 0x021D371C; OSi_UserExceptionHandlerArg = 0x21D3720; OSi_DebuggerHandler = 0x021D3724; diff --git a/include/nitro/os_system.h b/include/nitro/os_system.h index fc6a2f81..c7f121bd 100644 --- a/include/nitro/os_system.h +++ b/include/nitro/os_system.h @@ -18,18 +18,8 @@ typedef enum { } OSProcMode; typedef enum { - OS_INTRMODE_IRQ_DISABLE = HW_PSR_IRQ_DISABLE, - OS_INTRMODE_IRQ_ENABLE = 0 -} OSIntrMode_Irq; - -typedef enum { - OS_INTRMODE_FIQ_DISABLE = HW_PSR_FIQ_DISABLE, - OS_INTRMODE_FIQ_ENABLE = 0 -} OSIntrMode_Fiq; - -typedef union { - OSIntrMode_Fiq mode_fiq; - OSIntrMode_Irq mode_irq; + OS_INTRMODE_DISABLE = HW_PSR_IRQ_DISABLE, + OS_INTRMODE_ENABLE = 0 } OSIntrMode; OSIntrMode OS_EnableInterrupts(); @@ -37,7 +27,6 @@ OSIntrMode OS_DisableInterrupts(); OSIntrMode OS_RestoreInterrupts(OSIntrMode state); OSIntrMode OS_DisableInterrupts_IrqAndFiq(); OSIntrMode OS_RestoreInterrupts_IrqAndFiq(OSIntrMode state); -OSIntrMode_Irq OS_GetCpsrIrq(); OSProcMode OS_GetProcMode(); diff --git a/undefined_syms.txt b/undefined_syms.txt index d3891d10..302fe8bf 100644 --- a/undefined_syms.txt +++ b/undefined_syms.txt @@ -32,7 +32,7 @@ OSi_LauncherThread = 0x021D3568; OSi_IdleThreadStack = 0x021D3628; OSi_Initialized = 0x021D36F0; OSi_MainExArenaEnabled = 0x021D36F4; -OSi_HeapInfo = 0x021D36F8; +OSiHeapInfo = 0x021D36F8; OSi_UserExceptionHandler = 0x021D371C; OSi_UserExceptionHandlerArg = 0x21D3720; OSi_DebuggerHandler = 0x021D3724; |