From db7cbefcdf746fbc34179f33c0930ebd07ee6f0a Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Thu, 19 Aug 2021 19:51:52 -0400 Subject: NNS_FND_expheap, 1 --- arm9/lib/libnns/include/NNS_FND_heapcommon.h | 64 +++++++++++++++++++ arm9/lib/libnns/src/NNS_FND_expheap.c | 92 ++++++++++++++++++++++++++++ arm9/lib/libnns/src/NNS_FND_heapcommon.c | 8 +-- 3 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 arm9/lib/libnns/src/NNS_FND_expheap.c (limited to 'arm9/lib/libnns') diff --git a/arm9/lib/libnns/include/NNS_FND_heapcommon.h b/arm9/lib/libnns/include/NNS_FND_heapcommon.h index e6fab69c..8128e74b 100644 --- a/arm9/lib/libnns/include/NNS_FND_heapcommon.h +++ b/arm9/lib/libnns/include/NNS_FND_heapcommon.h @@ -7,6 +7,18 @@ typedef struct NNSiFndHeapHead NNSiFndHeapHead; +typedef s32 NNSiIntPtr; +typedef u32 NNSiUIntPtr; + +#define NNSi_FndGetBitValue(data, st, bits) (((data) >>(st)) & ((1 <<(bits)) -1)) +#define NNSi_FndSetBitValue(data, st, bits, val) do { \ + u32 maskBits = (u32)((1 << (bits)) - 1); \ + u32 newVal = (val) & maskBits; \ + (void)(maskBits <<= st); \ + (data) &= ~maskBits; \ + (data) |= newVal << (st); \ +} while (FALSE); + struct NNSiFndHeapHead { u32 signature; @@ -23,4 +35,56 @@ struct NNSiFndHeapHead typedef NNSiFndHeapHead* NNSFndHeapHandle; // Type to represent heap handle +static inline NNSiUIntPtr NNSiGetUIntPtr(const void* ptr) +{ + return (NNSiUIntPtr)ptr; +} + +static inline u32 GetOffsetFromPtr(const void* start, const void* end) +{ + return NNSiGetUIntPtr(end) - NNSiGetUIntPtr(start); +} + +static inline void* AddU32ToPtr(void* ptr, u32 val) +{ + return (void*)( NNSiGetUIntPtr(ptr) + val ); +} + +static inline const void* AddU32ToCPtr(const void* ptr, u32 val) +{ + return (const void*)( NNSiGetUIntPtr(ptr) + val ); +} + +static inline void* SubU32ToPtr(void* ptr, u32 val) +{ + return (void*)(NNSiGetUIntPtr(ptr) - val); +} + +static inline const void* SubU32ToCPtr(const void* ptr, u32 val) +{ + return (const void*)(NNSiGetUIntPtr(ptr) - val); +} + +static inline int ComparePtr(const void* a, const void* b) +{ + const u8* wa = a; + const u8* wb = b; + + return wa - wb; +} + + +static inline u16 GetOptForHeap(const NNSiFndHeapHead* pHeapHd) +{ + return (u16)NNSi_FndGetBitValue(pHeapHd->attribute, 0, 8); +} + +static inline void SetOptForHeap( + NNSiFndHeapHead* pHeapHd, + u16 optFlag + ) +{ + NNSi_FndSetBitValue(pHeapHd->attribute, 0, 8, optFlag); +} + #endif //GUARD_NNS_FND_HEAPCOMMON_H diff --git a/arm9/lib/libnns/src/NNS_FND_expheap.c b/arm9/lib/libnns/src/NNS_FND_expheap.c new file mode 100644 index 00000000..70e3886a --- /dev/null +++ b/arm9/lib/libnns/src/NNS_FND_expheap.c @@ -0,0 +1,92 @@ +#include "global.h" +#include "NNS_FND_expheap.h" + +typedef struct NNSiMemRegion { + void* start; + void* end; +} NNSiMemRegion; + +static inline u16 GetAlignmentForMBlock(NNSiFndExpHeapMBlockHead* block) +{ + return NNSi_FndGetBitValue(block->attribute, 8, 7); +} + +static inline void* GetMemPtrForMBlock(NNSiFndExpHeapMBlockHead* block) +{ + return AddU32ToPtr(block, sizeof(NNSiFndExpHeapMBlockHead)); +} + +static inline void* GetMBlockEndAddr(NNSiFndExpHeapMBlockHead* block) +{ + return AddU32ToPtr(GetMemPtrForMBlock(block), block->blockSize); +} + +void GetRegionOfMBlock(NNSiMemRegion* region, NNSiFndExpHeapMBlockHead* block) +{ + region->start = SubU32ToPtr(block, GetAlignmentForMBlock(block)); + region->end = GetMBlockEndAddr(block); +} + +NNSiFndExpHeapMBlockHead* RemoveMBlock(NNSiFndExpMBlockList* list, NNSiFndExpHeapMBlockHead* block) +{ + NNSiFndExpHeapMBlockHead* const prev = block->pMBHeadPrev; + NNSiFndExpHeapMBlockHead* const next = block->pMBHeadNext; + + if (prev != NULL) + { + prev->pMBHeadNext = next; + } + else + { + list->head = next; + } + + if (next != NULL) + { + next->pMBHeadPrev = prev; + } + else + { + list->tail = prev; + } + + return prev; +} + +NNSiFndExpHeapMBlockHead* InsertMBlock(NNSiFndExpMBlockList* list, NNSiFndExpHeapMBlockHead* target, NNSiFndExpHeapMBlockHead* prev) +{ + NNSiFndExpHeapMBlockHead* next; + target->pMBHeadPrev = prev; + if (prev != NULL) + { + next = prev->pMBHeadNext; + prev->pMBHeadNext = target; + } + else + { + next = list->head; + list->head = target; + } + target->pMBHeadNext = next; + if (next != NULL) + { + next->pMBHeadPrev = target; + } + else + { + list->tail = target; + } + + return target; +} + +NNSiFndExpHeapMBlockHead* InitMBlock(const NNSiMemRegion* pRegion, u16 signature) +{ + NNSiFndExpHeapMBlockHead* block = pRegion->start; + block->signature = signature; + block->attribute = 0; + block->blockSize = GetOffsetFromPtr(GetMemPtrForMBlock(block), pRegion->end); + block->pMBHeadPrev = NULL; + block->pMBHeadNext = NULL; + return block; +} diff --git a/arm9/lib/libnns/src/NNS_FND_heapcommon.c b/arm9/lib/libnns/src/NNS_FND_heapcommon.c index 664007f1..07b8e9cf 100644 --- a/arm9/lib/libnns/src/NNS_FND_heapcommon.c +++ b/arm9/lib/libnns/src/NNS_FND_heapcommon.c @@ -5,12 +5,6 @@ BOOL sRootListInitialized; NNSFndList sRootList; -static inline void NNSi_FndHeapHeadSetOptionFlag(NNSiFndHeapHead *pHead, u8 option_flag) -{ - pHead->attribute &= ~0xFF; - pHead->attribute |= option_flag; -} - void *NNS_FndGetNextListObject(NNSFndList *, void *); static NNSiFndHeapHead* FindContainHeap(NNSFndList * pList, const void * memBlock) @@ -45,7 +39,7 @@ void NNSi_FndInitHeapHead(NNSiFndHeapHead *pHead, u32 signature, void* heapStart pHead->heapStart = heapStart; pHead->heapEnd = heapEnd; pHead->attribute = 0; - NNSi_FndHeapHeadSetOptionFlag(pHead, optionFlag); + SetOptForHeap(pHead, optionFlag); NNS_FndInitList(&pHead->childList, 4); if (!sRootListInitialized) { -- cgit v1.2.3