Skip to content

Commit

Permalink
Merge pull request #236 from AnonymousRandomPerson/master
Browse files Browse the repository at this point in the history
Added decomped memory functions from Kermalis
  • Loading branch information
AnonymousRandomPerson authored Oct 24, 2024
2 parents e3e15a3 + 67e8cd8 commit 428839a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 126 deletions.
107 changes: 0 additions & 107 deletions asm/memory_1.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,113 +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}
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}
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}
Expand Down
32 changes: 30 additions & 2 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ struct HeapMemoryBlock
/* 0x8 */ s32 allocatedSize;
};

// size: 0x8
struct HeapMemoryBlock2
{
/* 0x0 */ struct HeapFreeListElement *start;
/* 0x4 */ s32 size;
};

// size: 0x10
struct HeapFreeListElement
{
Expand All @@ -39,6 +46,24 @@ 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;
};

extern struct HeapDescriptor *gHeapDescriptorList[8];
extern s32 gHeapCount;

void InitHeap(void);

void MemoryClear8(u8 *dest, s32 size);
Expand All @@ -56,9 +81,12 @@ 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);
void xxx_unused_memory_free(struct HeapDescriptor *a1);
void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2);

#endif // GUARD_MEMORY_H
6 changes: 3 additions & 3 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +171,7 @@ u8 xxx_memory_attr_related(u32 r0)
{
return_var = 3;
}
else
else
{
return_var = 5;
}
Expand Down
78 changes: 64 additions & 14 deletions src/memory_mid.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ 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 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;
}
Expand All @@ -37,13 +37,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:
Expand All @@ -67,10 +67,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;
}
Expand All @@ -81,10 +81,60 @@ 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;
}

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;
}

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);
}
}

void *DoAlloc(struct HeapDescriptor *heap, s32 size, u32 a2)
{
return _LocateSet(heap, size, a2 | 0x100);
}

0 comments on commit 428839a

Please sign in to comment.