Skip to content

Commit

Permalink
True match zMalloc (#659)
Browse files Browse the repository at this point in the history
* True match zMalloc

* Cleanup
  • Loading branch information
KEKW555 authored Dec 24, 2023
1 parent 50b8efb commit 1e8ced0
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,8 @@ void sub_0801D898(void* dest, void* src, u32 word, u32 size) {
}

void* zMalloc(u32 size) {

FORCE_REGISTER(u32 slotFound, r5);
u16* heapStartOffset;
u32 slotFound;
u8* allocatedEntryStartOffset;
u8* allocatedEntryEndOffset;
u8* candidateSlotEndOffset;
Expand All @@ -377,7 +376,8 @@ void* zMalloc(u32 size) {

if ((allocatedEntryStartOffset <= candidateSlotStartOffset &&
candidateSlotStartOffset <= allocatedEntryEndOffset)) {
goto other_search;
slotFound = FALSE;
break;
}

if ((allocatedEntryStartOffset <= candidateSlotEndOffset &&
Expand All @@ -390,17 +390,15 @@ void* zMalloc(u32 size) {
candidateSlotEndOffset <= allocatedEntryEndOffset) ||
(candidateSlotStartOffset <= allocatedEntryStartOffset &&
allocatedEntryEndOffset <= candidateSlotEndOffset)) {
goto other_search;
slotFound = FALSE;
break;
}
}

if (!slotFound) {
other_search:

index1 = 0;
// Start searching for candidate slot from the left side of the heap buffer.
do {

candidateSlotEndOffset = gzHeap + heapStartOffset[(index1 * 2) + 1];
candidateSlotStartOffset = candidateSlotEndOffset - size;
slotFound = FALSE;
Expand All @@ -417,7 +415,8 @@ void* zMalloc(u32 size) {

if ((allocatedEntryStartOffset <= candidateSlotStartOffset &&
candidateSlotStartOffset < allocatedEntryEndOffset)) {
goto iter_end;
slotFound = FALSE;
break;
}

if ((allocatedEntryStartOffset < candidateSlotEndOffset &&
Expand All @@ -430,17 +429,13 @@ void* zMalloc(u32 size) {
candidateSlotEndOffset <= allocatedEntryEndOffset) ||
(candidateSlotStartOffset <= allocatedEntryStartOffset &&
allocatedEntryEndOffset <= candidateSlotEndOffset)) {
goto iter_end;
slotFound = FALSE;
break;
}
}
if (slotFound) {
break;
} else {
continue;
}

iter_end:
slotFound = FALSE;
}
} while ((index1 = (u16)(index1 + 1)) < numEntries);
}
Expand Down

0 comments on commit 1e8ced0

Please sign in to comment.