From d35f62760631711348d01165d011389ca1f94762 Mon Sep 17 00:00:00 2001 From: AnonymousRandomPerson Date: Tue, 22 Oct 2024 22:01:32 -0400 Subject: [PATCH 1/3] Decomped DoCreateSubHeap Decomped by Kermalis --- asm/memory_1.s | 24 +----------------------- include/memory.h | 27 +++++++++++++++++++++++++-- src/memory.c | 4 ++-- src/memory_mid.c | 42 ++++++++++++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/asm/memory_1.s b/asm/memory_1.s index 78965232f..5af67bad3 100644 --- a/asm/memory_1.s +++ b/asm/memory_1.s @@ -5,28 +5,6 @@ .text - thumb_func_start DoCreateSubHeap -DoCreateSubHeap: - push {r4,lr} - sub sp, 0x8 - adds r2, r1, 0 - ldr r4, [r0] - adds r1, r4, 0 - adds r1, 0x1C - str r1, [sp] - ldr r0, [r0, 0x4] - subs r0, 0x1C - str r0, [sp, 0x4] - adds r0, r4, 0 - mov r1, sp - bl InitSubHeap - adds r0, r4, 0 - add sp, 0x8 - pop {r4} - pop {r1} - bx r1 - thumb_func_end DoCreateSubHeap - thumb_func_start xxx_unused_memory_free xxx_unused_memory_free: push {r4-r7,lr} @@ -77,7 +55,7 @@ _080034D2: blt _080034D2 b _080034F8 .align 2, 0 - .pool + .pool _080034E8: adds r2, 0x4 adds r3, 0x1 diff --git a/include/memory.h b/include/memory.h index 30969cc6e..2398d02b7 100644 --- a/include/memory.h +++ b/include/memory.h @@ -18,6 +18,13 @@ struct HeapMemoryBlock /* 0x8 */ s32 allocatedSize; }; +// size: 0x8 +struct HeapMemoryBlock2 +{ + /* 0x0 */ struct HeapFreeListElement *start; + /* 0x4 */ s32 size; +}; + // size: 0x10 struct HeapFreeListElement { @@ -39,6 +46,21 @@ struct HeapDescriptor /* 0x18 */ u32 size; }; +// size: 0x8 +struct unkMemoryStruct +{ + struct HeapDescriptor *unk0; + u32 end; +}; + +// size: 0x1C +struct unkMemoryStruct2 +{ + u8 fill[0xC]; + /* 0xC */ struct HeapDescriptor *unkC; + /* 0x10 */ u32 end; +}; + void InitHeap(void); void MemoryClear8(u8 *dest, s32 size); @@ -56,9 +78,10 @@ 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 HeapMemoryBlock *, u32); -s32 _LocateSet(struct HeapDescriptor *heap, s32 size, s32 param_3); +void InitSubHeap(struct HeapDescriptor *, struct HeapMemoryBlock2 *, u32); +struct HeapDescriptor * _LocateSet(struct HeapDescriptor *heap, s32 size, s32 param_3); void *MemoryAlloc(s32 size, s32 group); void MemoryFree(void *a); +struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b); #endif // GUARD_MEMORY_H diff --git a/src/memory.c b/src/memory.c index b6165ab05..5d148fb4e 100644 --- a/src/memory.c +++ b/src/memory.c @@ -127,7 +127,7 @@ void DoInitHeap(struct HeapDescriptor *descriptor, struct HeapSettings *settings freeList->grp = 0; } -void InitSubHeap(struct HeapDescriptor *parentHeap, struct HeapMemoryBlock *block, u32 freeListMax) +void InitSubHeap(struct HeapDescriptor *parentHeap, struct HeapMemoryBlock2 *block, u32 freeListMax) { u32 freeListSize; u32 aligned_size; @@ -171,7 +171,7 @@ u8 xxx_memory_attr_related(u32 r0) { return_var = 3; } - else + else { return_var = 5; } diff --git a/src/memory_mid.c b/src/memory_mid.c index 3bb1e5194..7d6e4ad51 100644 --- a/src/memory_mid.c +++ b/src/memory_mid.c @@ -8,21 +8,20 @@ extern struct HeapDescriptor gMainHeapDescriptor; extern const char gLocalCreateErrorMessage[]; extern s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32, s32); extern s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32, s32); -extern struct HeapDescriptor * _LocateSetBack(struct HeapDescriptor *, u32, u32, u32, u32); -extern struct HeapDescriptor * _LocateSetFront(struct HeapDescriptor *, u32, u32, u32, u32); +extern struct unkMemoryStruct2 * _LocateSetBack(struct HeapDescriptor *, u32, u32, u32, u32); +extern struct unkMemoryStruct2 * _LocateSetFront(struct HeapDescriptor *, u32, u32, u32, u32); void DoFree(struct HeapDescriptor *, void *); void *DoAlloc(struct HeapDescriptor *, s32, u32); -extern struct HeapDescriptor *DoCreateSubHeap(u32 *, u32); void FatalError(u32 *, const char *, ...) __attribute__((noreturn)); -s32 _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group) +struct HeapDescriptor* _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group) { s32 index; - struct HeapDescriptor * uVar2; + struct unkMemoryStruct2 * uVar2; s32 atb; - + if (heap == NULL) { heap = &gMainHeapDescriptor; } @@ -37,13 +36,13 @@ s32 _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group) index = MemorySearchFromFront(heap,atb,size); if (index < 0) goto error; uVar2 = _LocateSetFront(heap,index,atb,size,group); - return uVar2->freeCount; + return uVar2->unkC; } else { index = MemorySearchFromBack(heap,atb,size); if (index < 0) goto error; uVar2 = _LocateSetBack(heap,index,atb,size,group); - return uVar2->freeCount; + return uVar2->unkC; } error: @@ -67,10 +66,10 @@ void MemoryFree(void *a) struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHeap,u32 size,u32 param_3,u32 group) { int index; - struct HeapDescriptor *iVar2; + struct unkMemoryStruct2 *iVar2; struct HeapDescriptor *iVar3; - s32 local_1c[2]; - + struct unkMemoryStruct local_1c; + if (parentHeap == NULL) { parentHeap = &gMainHeapDescriptor; } @@ -81,10 +80,25 @@ struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHea FatalError(&gUnknown_80B7F88,gLocalCreateErrorMessage,size); iVar2 = _LocateSetBack(parentHeap,index,9,size,group); - local_1c[0] = iVar2->freeCount; - local_1c[1] = iVar2->freeListLength; + local_1c.unk0 = iVar2->unkC; + local_1c.end = iVar2->end; - iVar3 = DoCreateSubHeap(local_1c,param_3); + iVar3 = DoCreateSubHeap(&local_1c,param_3); iVar3->parentHeap = parentHeap; return iVar3; } + +// Note: This is called after _LocateSetBack which currently returns a HeapDescriptor* which is incorrect +struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) +{ + struct HeapMemoryBlock2 s2; + struct HeapDescriptor *a1; + u32 end; + + a1 = a->unk0; + s2.start = (struct HeapFreeListElement *)((u8*)a1 + sizeof(struct HeapDescriptor)); + end = a->end; + s2.size = end - sizeof(struct HeapDescriptor); + InitSubHeap(a1, &s2, b); + return a1; +} From dd8c864e382f12549a9bdf9767aee6947c824222 Mon Sep 17 00:00:00 2001 From: AnonymousRandomPerson Date: Tue, 22 Oct 2024 22:18:48 -0400 Subject: [PATCH 2/3] Decomped xxx_unused_memory_free Decomped by Kermalis --- asm/memory_1.s | 73 ------------------------------------------------ include/memory.h | 4 +++ src/memory.c | 2 +- src/memory_mid.c | 32 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 74 deletions(-) diff --git a/asm/memory_1.s b/asm/memory_1.s index 5af67bad3..325f933cc 100644 --- a/asm/memory_1.s +++ b/asm/memory_1.s @@ -5,79 +5,6 @@ .text - thumb_func_start xxx_unused_memory_free -xxx_unused_memory_free: - push {r4-r7,lr} - adds r4, r0, 0 - cmp r4, 0 - beq _08003504 - movs r1, 0 - ldr r0, [r4, 0xC] - cmp r0, 0x1 - bne _080034A2 - ldr r0, [r4, 0x8] - ldr r0, [r0, 0x4] - cmp r0, 0 - bne _080034A2 - movs r1, 0x1 -_080034A2: - cmp r1, 0 - beq _08003504 - movs r0, 0 - mov r12, r0 - movs r3, 0 - ldr r6, =gHeapCount - ldr r0, [r6] - cmp r12, r0 - bge _080034F2 - ldr r5, =gHeapDescriptorList - adds r1, r6, 0 - adds r7, r5, 0 - movs r2, 0 -_080034BC: - adds r0, r2, r5 - ldr r0, [r0] - cmp r0, r4 - bne _080034E8 - ldr r0, [r1] - subs r0, 0x1 - str r0, [r1] - cmp r3, r0 - bge _080034F8 - ldr r1, =gHeapCount - adds r2, r7 -_080034D2: - ldr r0, [r2, 0x4] - stm r2!, {r0} - adds r3, 0x1 - ldr r0, [r1] - cmp r3, r0 - blt _080034D2 - b _080034F8 - .align 2, 0 - .pool -_080034E8: - adds r2, 0x4 - adds r3, 0x1 - ldr r0, [r6] - cmp r3, r0 - blt _080034BC -_080034F2: - mov r0, r12 - cmp r0, 0 - beq _08003504 -_080034F8: - ldr r0, [r4, 0x4] - cmp r0, 0 - beq _08003504 - adds r1, r4, 0 - bl DoFree -_08003504: - pop {r4-r7} - pop {r0} - bx r0 - thumb_func_end xxx_unused_memory_free - thumb_func_start DoAlloc DoAlloc: push {lr} diff --git a/include/memory.h b/include/memory.h index 2398d02b7..980c20dba 100644 --- a/include/memory.h +++ b/include/memory.h @@ -61,6 +61,9 @@ struct unkMemoryStruct2 /* 0x10 */ u32 end; }; +extern struct HeapDescriptor *gHeapDescriptorList[8]; +extern s32 gHeapCount; + void InitHeap(void); void MemoryClear8(u8 *dest, s32 size); @@ -83,5 +86,6 @@ struct HeapDescriptor * _LocateSet(struct HeapDescriptor *heap, s32 size, s32 pa 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); #endif // GUARD_MEMORY_H diff --git a/src/memory.c b/src/memory.c index 5d148fb4e..0a63bfd33 100644 --- a/src/memory.c +++ b/src/memory.c @@ -3,7 +3,7 @@ #include "memory.h" EWRAM_DATA struct HeapDescriptor *gHeapDescriptorList[8] = {0}; // 2000E88 -EWRAM_DATA u32 gHeapCount = {0}; // 2000EA8 +EWRAM_DATA s32 gHeapCount = {0}; // 2000EA8 UNUSED static EWRAM_DATA u32 sUnused1 = {0}; // 2000EAC EWRAM_DATA struct HeapDescriptor gMainHeapDescriptor = {0}; // 2000EB0 UNUSED static EWRAM_DATA u32 sUnused2 = {0}; // 2000ECC diff --git a/src/memory_mid.c b/src/memory_mid.c index 7d6e4ad51..ba605254a 100644 --- a/src/memory_mid.c +++ b/src/memory_mid.c @@ -6,6 +6,7 @@ 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); extern struct unkMemoryStruct2 * _LocateSetBack(struct HeapDescriptor *, u32, u32, u32, u32); @@ -102,3 +103,34 @@ struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) InitSubHeap(a1, &s2, b); return a1; } + +void xxx_unused_memory_free(struct HeapDescriptor *a1) +{ + bool8 b; + s32 i; + bool8 temp; + + if (a1 == NULL) + return; + + b = FALSE; + if (a1->freeCount == 1 && a1->freeList->atb == 0) + b = TRUE; + + if (b) { + temp = FALSE; + i = 0; + for (; i < gHeapCount; i++) { + if (gHeapDescriptorList[i] == a1) { + gHeapCount--; + for (; i < gHeapCount; i++) { + gHeapDescriptorList[i] = gHeapDescriptorList[i + 1]; + } + temp = TRUE; + break; + } + } + if (temp && a1->parentHeap != NULL) + DoFree(a1->parentHeap, a1); + } +} From 67e8cd84005b624b82affc936b88d7f37a329d1a Mon Sep 17 00:00:00 2001 From: AnonymousRandomPerson Date: Tue, 22 Oct 2024 22:20:03 -0400 Subject: [PATCH 3/3] Decomped DoAlloc Decomped by Kermalis --- asm/memory_1.s | 12 ------------ include/memory.h | 1 + src/memory_mid.c | 6 +++++- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/asm/memory_1.s b/asm/memory_1.s index 325f933cc..6ece0344e 100644 --- a/asm/memory_1.s +++ b/asm/memory_1.s @@ -5,18 +5,6 @@ .text - thumb_func_start DoAlloc -DoAlloc: - push {lr} - movs r3, 0x80 - lsls r3, 1 - orrs r3, r2 - adds r2, r3, 0 - bl _LocateSet - pop {r1} - bx r1 - thumb_func_end DoAlloc - thumb_func_start DoFree DoFree: push {r4-r7,lr} diff --git a/include/memory.h b/include/memory.h index 980c20dba..48bba1793 100644 --- a/include/memory.h +++ b/include/memory.h @@ -87,5 +87,6 @@ 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_mid.c b/src/memory_mid.c index ba605254a..949bf099f 100644 --- a/src/memory_mid.c +++ b/src/memory_mid.c @@ -89,7 +89,6 @@ struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHea return iVar3; } -// Note: This is called after _LocateSetBack which currently returns a HeapDescriptor* which is incorrect struct HeapDescriptor *DoCreateSubHeap(struct unkMemoryStruct *a, u32 b) { struct HeapMemoryBlock2 s2; @@ -134,3 +133,8 @@ void xxx_unused_memory_free(struct HeapDescriptor *a1) DoFree(a1->parentHeap, a1); } } + +void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2) +{ + return _LocateSet(heap, size, a2 | 0x100); +}