From 27bd13dd5b3f9da6528494e496f430c2b9f29352 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 2 Dec 2024 12:25:04 +0100 Subject: [PATCH] clean memory include --- include/memory.h | 61 ------------------- src/memory.c | 153 +++++++++++++++++++++++++++++++---------------- 2 files changed, 102 insertions(+), 112 deletions(-) diff --git a/include/memory.h b/include/memory.h index f6cb17d77..cb69542e2 100644 --- a/include/memory.h +++ b/include/memory.h @@ -1,61 +1,6 @@ #ifndef GUARD_MEMORY_H #define GUARD_MEMORY_H -#define HEAP_SIZE 0x24000 - -// size: 0x8 -struct HeapSettings -{ - /* 0x0 */ u8 *start; - /* 0x4 */ u32 size; -}; - -// size: 0xC -struct HeapMemoryBlock -{ - /* 0x0 */ u8 *start; - /* 0x4 */ s32 size; - /* 0x8 */ s32 allocatedSize; -}; - -// size: 0x8 -struct HeapMemoryBlock2 -{ - /* 0x0 */ struct HeapFreeListElement *start; - /* 0x4 */ s32 size; -}; - -// size: 0x10 -struct HeapFreeListElement -{ - /* 0x0 */ u32 unk_atb; - /* 0x4 */ u32 atb; - /* 0x8 */ u32 grp; - /* 0xC */ struct HeapMemoryBlock block; -}; - -// size: 0x1C -struct HeapDescriptor -{ - u32 unk0; - /* 0x4 */ struct HeapDescriptor *parentHeap; - /* 0x8 */ struct HeapFreeListElement *freeList; - /* 0xC */ s32 freeCount; - /* 0x10 */ s32 freeListLength; - /* 0x14 */ u8 *start; - /* 0x18 */ u32 size; -}; - -// size: 0x8 -struct unkMemoryStruct -{ - struct HeapDescriptor *unk0; - u32 end; -}; - -extern struct HeapDescriptor *gHeapDescriptorList[8]; -extern s32 gHeapCount; - void InitHeap(void); void MemoryClear8(u8 *dest, s32 size); @@ -71,13 +16,7 @@ void MemoryCopy8(u8 *dest, u8 *src, s32 size); void MemoryCopy16(u16 *dest, u16 *src, s32 size); void MemoryCopy32(u32 *dest, u32 *src, s32 size); -void InitHeapInternal(void); -void DoInitHeap(struct HeapDescriptor *, struct HeapSettings *, struct HeapFreeListElement *, u32); -void InitSubHeap(struct HeapDescriptor *, struct HeapMemoryBlock2 *, u32); void *MemoryAlloc(s32 size, s32 group); void MemoryFree(void *a); -struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b); -void xxx_unused_memory_free(struct HeapDescriptor *a1); -void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2); #endif // GUARD_MEMORY_H diff --git a/src/memory.c b/src/memory.c index 953587704..66dfa29ed 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,16 +1,84 @@ #include "global.h" -#include "cpu.h" #include "memory.h" +#include "cpu.h" -EWRAM_DATA struct HeapDescriptor *gHeapDescriptorList[8] = {0}; // 2000E88 -EWRAM_DATA s32 gHeapCount = {0}; // 2000EA8 +#define HEAP_SIZE 0x24000 + +// size: 0x8 +struct HeapSettings +{ + /* 0x0 */ u8 *start; + /* 0x4 */ u32 size; +}; + +// size: 0xC +struct HeapMemoryBlock +{ + /* 0x0 */ u8 *start; + /* 0x4 */ s32 size; + /* 0x8 */ s32 allocatedSize; +}; + +// size: 0x8 +struct HeapMemoryBlock2 +{ + /* 0x0 */ struct HeapFreeListElement *start; + /* 0x4 */ s32 size; +}; + +// size: 0x10 +struct HeapFreeListElement +{ + /* 0x0 */ u32 unk_atb; + /* 0x4 */ u32 atb; + /* 0x8 */ u32 grp; + /* 0xC */ struct HeapMemoryBlock block; +}; + +// size: 0x1C +struct HeapDescriptor +{ + u32 unk0; + /* 0x4 */ struct HeapDescriptor *parentHeap; + /* 0x8 */ struct HeapFreeListElement *freeList; + /* 0xC */ s32 freeCount; + /* 0x10 */ s32 freeListLength; + /* 0x14 */ u8 *start; + /* 0x18 */ u32 size; +}; + +// size: 0x8 +struct unkMemoryStruct +{ + struct HeapDescriptor *unk0; + u32 end; +}; + +static EWRAM_DATA struct HeapDescriptor *sHeapDescriptorList[8] = {0}; // 2000E88 +static EWRAM_DATA s32 sHeapCount = {0}; // 2000EA8 UNUSED static EWRAM_DATA u32 sUnused1 = {0}; // 2000EAC -EWRAM_DATA struct HeapDescriptor gMainHeapDescriptor = {0}; // 2000EB0 +static EWRAM_DATA struct HeapDescriptor sMainHeapDescriptor = {0}; // 2000EB0 UNUSED static EWRAM_DATA u32 sUnused2 = {0}; // 2000ECC extern struct HeapFreeListElement gMainHeapFreeList[32]; // 2000ED0 (CAPACITY OR STRUCT SIZE IS WRONG) extern u8 gMainHeap[HEAP_SIZE]; // 20011D0 -void InitHeapInternal(void); +// Todo: fix fatal error +void FatalError(const void *, const char *, ...) __attribute__((noreturn)); +extern const char *const gUnknown_80B7EB8; +extern const char *const gUnknown_80B7EFC; +extern const char gUnknown_80B7EC4[]; +extern u32 gUnknown_80B7F14; +extern u32 gUnknown_80B7F88; +extern const char gLocateSetErrorMessage[]; +extern const char gLocalCreateErrorMessage[]; + +static void DoFree(struct HeapDescriptor *, void *); +static void *DoAlloc(struct HeapDescriptor *, s32, u32); +static void DoInitHeap(struct HeapDescriptor *, struct HeapSettings *, struct HeapFreeListElement *, u32); +static void InitSubHeap(struct HeapDescriptor *, struct HeapMemoryBlock2 *, u32); +static struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b); +static void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2); +static void InitHeapInternal(void); void InitHeap(void) { @@ -94,23 +162,23 @@ void MemoryCopy32(u32 *dest, u32 *src, s32 size) } } -void InitHeapInternal(void) +static void InitHeapInternal(void) { struct HeapSettings settings; settings.start = gMainHeap; settings.size = HEAP_SIZE; - gHeapCount = 0; - DoInitHeap(&gMainHeapDescriptor, &settings, gMainHeapFreeList, sizeof(gMainHeapFreeList) / sizeof(struct HeapFreeListElement)); + sHeapCount = 0; + DoInitHeap(&sMainHeapDescriptor, &settings, gMainHeapFreeList, sizeof(gMainHeapFreeList) / sizeof(struct HeapFreeListElement)); } -void DoInitHeap(struct HeapDescriptor *descriptor, struct HeapSettings *settings, struct HeapFreeListElement *freeList, u32 freeListLength) +static void DoInitHeap(struct HeapDescriptor *descriptor, struct HeapSettings *settings, struct HeapFreeListElement *freeList, u32 freeListLength) { u32 aligned_size; aligned_size = settings->size & ~(3); - gHeapDescriptorList[gHeapCount++] = descriptor; + sHeapDescriptorList[sHeapCount++] = descriptor; descriptor->start = settings->start; descriptor->size = aligned_size; @@ -128,7 +196,7 @@ void DoInitHeap(struct HeapDescriptor *descriptor, struct HeapSettings *settings freeList->grp = 0; } -void InitSubHeap(struct HeapDescriptor *parentHeap, struct HeapMemoryBlock2 *block, u32 freeListMax) +static void InitSubHeap(struct HeapDescriptor *parentHeap, struct HeapMemoryBlock2 *block, u32 freeListMax) { u32 freeListSize; u32 aligned_size; @@ -146,7 +214,7 @@ void InitSubHeap(struct HeapDescriptor *parentHeap, struct HeapMemoryBlock2 *blo DoInitHeap(parentHeap, &settings, freeList, freeListMax); } -u32 xxx_memory_attr_related(u32 r0) +static u32 xxx_memory_attr_related(u32 r0) { u32 temp; u32 return_var; @@ -179,7 +247,7 @@ u32 xxx_memory_attr_related(u32 r0) return return_var; } -s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32 atb, s32 size) +static s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32 atb, s32 size) { s32 i; struct HeapFreeListElement *curr; @@ -223,7 +291,7 @@ s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32 atb, s32 size) return -1; } -s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32 atb, s32 size) +static s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32 atb, s32 size) { s32 i; struct HeapFreeListElement *curr; @@ -267,24 +335,7 @@ s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32 atb, s32 size) return -1; } -// Todo: fix fatal error -void FatalError(const void *, const char *, ...) __attribute__((noreturn)); -extern const char *const gUnknown_80B7EB8; -extern const char *const gUnknown_80B7EFC; -extern const char gUnknown_80B7EC4[]; -extern u32 gUnknown_80B7F14; -extern u32 gUnknown_80B7F88; -extern const char gLocateSetErrorMessage[]; -extern struct HeapDescriptor gMainHeapDescriptor; -extern const char gLocalCreateErrorMessage[]; - -extern s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32, s32); -extern s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32, s32); - -void DoFree(struct HeapDescriptor *, void *); -void *DoAlloc(struct HeapDescriptor *, s32, u32); - -struct HeapFreeListElement * _LocateSetFront(struct HeapDescriptor *heap, s32 index, s32 atb, s32 size, s32 group) +static struct HeapFreeListElement * _LocateSetFront(struct HeapDescriptor *heap, s32 index, s32 atb, s32 size, s32 group) { s32 i; struct HeapFreeListElement *curr; @@ -322,7 +373,7 @@ struct HeapFreeListElement * _LocateSetFront(struct HeapDescriptor *heap, s32 in return curr; } -struct HeapFreeListElement * _LocateSetBack(struct HeapDescriptor *heap, s32 index, s32 atb, s32 size, s32 group) +static struct HeapFreeListElement * _LocateSetBack(struct HeapDescriptor *heap, s32 index, s32 atb, s32 size, s32 group) { s32 i; struct HeapFreeListElement *curr; @@ -363,14 +414,14 @@ struct HeapFreeListElement * _LocateSetBack(struct HeapDescriptor *heap, s32 ind return curr; } -void * _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group) +static void * _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group) { s32 index; struct HeapFreeListElement * foundSet; s32 atb; if (heap == NULL) { - heap = &gMainHeapDescriptor; + heap = &sMainHeapDescriptor; } // Set some sort flag/attr? @@ -402,15 +453,15 @@ error: void *MemoryAlloc(s32 size, s32 group) { - DoAlloc(&gMainHeapDescriptor, size, group); + DoAlloc(&sMainHeapDescriptor, size, group); } void MemoryFree(void *a) { - DoFree(&gMainHeapDescriptor, a); + DoFree(&sMainHeapDescriptor, a); } -struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHeap,u32 size,u32 param_3,u32 group) +UNUSED static struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHeap,u32 size,u32 param_3,u32 group) { int index; struct HeapFreeListElement *foundSet; @@ -418,7 +469,7 @@ struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHea struct unkMemoryStruct local_1c; if (parentHeap == NULL) { - parentHeap = &gMainHeapDescriptor; + parentHeap = &sMainHeapDescriptor; } index = MemorySearchFromBack(parentHeap,9,size); @@ -435,7 +486,7 @@ struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHea return iVar3; } -struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) +static struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) { struct HeapMemoryBlock2 s2; struct HeapDescriptor *a1; @@ -449,7 +500,7 @@ struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) return a1; } -void xxx_unused_memory_free(struct HeapDescriptor *a1) +UNUSED static void xxx_unused_memory_free(struct HeapDescriptor *a1) { bool8 b; s32 i; @@ -465,11 +516,11 @@ void xxx_unused_memory_free(struct HeapDescriptor *a1) if (b) { temp = FALSE; i = 0; - for (; i < gHeapCount; i++) { - if (gHeapDescriptorList[i] == a1) { - gHeapCount--; - for (; i < gHeapCount; i++) { - gHeapDescriptorList[i] = gHeapDescriptorList[i + 1]; + for (; i < sHeapCount; i++) { + if (sHeapDescriptorList[i] == a1) { + sHeapCount--; + for (; i < sHeapCount; i++) { + sHeapDescriptorList[i] = sHeapDescriptorList[i + 1]; } temp = TRUE; break; @@ -480,12 +531,12 @@ void xxx_unused_memory_free(struct HeapDescriptor *a1) } } -void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2) +static void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2) { return _LocateSet(heap, size, a2 | 0x100); } -void DoFree(struct HeapDescriptor *heapDescriptior, void *ptrToFree) +static void DoFree(struct HeapDescriptor *heapDescriptior, void *ptrToFree) { struct HeapFreeListElement *curr; struct HeapFreeListElement *next; @@ -493,7 +544,7 @@ void DoFree(struct HeapDescriptor *heapDescriptior, void *ptrToFree) s32 i; if (heapDescriptior == NULL) - heapDescriptior = &gMainHeapDescriptor; + heapDescriptior = &sMainHeapDescriptor; if (ptrToFree == NULL) return; @@ -536,12 +587,12 @@ void DoFree(struct HeapDescriptor *heapDescriptior, void *ptrToFree) } } -void nullsub_141(void) +UNUSED static void nullsub_141(void) { } -void nullsub_142(void) +UNUSED static void nullsub_142(void) { }