match _heap_realloc (#524)

* match _heap_realloc

* Minor code cleanup
This commit is contained in:
Lightning 2021-11-10 20:38:52 -08:00 committed by GitHub
parent be4bb17dee
commit 90c0d29bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 80 deletions

View File

@ -24,6 +24,7 @@ f32 signF(f32 val);
void* heap_malloc(s32 size);
void* _heap_malloc(HeapNode* head, u32 size);
u32 _heap_free(HeapNode* heapNodeList, void* addrToFree);
void* _heap_realloc(HeapNode* heapNodeList, void* addr, u32 newSize);
HeapNode* _heap_create(HeapNode* addr, u32 size);
s32 dma_copy(Addr romStart, Addr romEnd, void* vramDest);
void copy_matrix(Matrix4f src, Matrix4f dest);

View File

@ -278,7 +278,73 @@ u32 _heap_free(HeapNode* heapNodeList, void* addrToFree) {
return FALSE;
}
INCLUDE_ASM(s32, "43F0", _heap_realloc);
void* _heap_realloc(HeapNode* heapNodeList, void* addr, u32 newSize) {
u32 newSizeAligned;
HeapNode* nextNode;
HeapNode* curHeapAlloc;
HeapNode* newFreeBlock;
u32 newNodeLength;
HeapNode* nodeToUpdate;
curHeapAlloc = (HeapNode*)((u8*)addr - sizeof(HeapNode));
newSizeAligned = ALIGN16(newSize);
// check if the realloc is on an allocated node otherwise fail
if (!curHeapAlloc->allocated) {
return NULL;
}
nextNode = curHeapAlloc->next;
newNodeLength = curHeapAlloc->length;
// if we have a node after us and it isn't allocated then
// adjust the length and next node pointers to include the node after us
nodeToUpdate = nextNode;
if (nextNode && !nextNode->allocated) {
nextNode = nextNode->next;
newNodeLength += sizeof(HeapNode) + nodeToUpdate->length;
}
// check if the current block or current + next block (if free) are too small
nodeToUpdate = curHeapAlloc;
if (newNodeLength < newSizeAligned) {
// too small, allocatr a new node, copy data to it then free the current one
curHeapAlloc = _heap_malloc(heapNodeList, newSizeAligned);
if (curHeapAlloc == NULL) {
return NULL;
}
// minor interest note, copy the size of the newly allocated size
// instead of just how much data used to be stored, this results in copying
// excessive data
memcpy(curHeapAlloc, addr, newSizeAligned);
_heap_free(heapNodeList, addr);
return curHeapAlloc;
}
// see if there is room to add a new free block after us
if (newSizeAligned + sizeof(HeapNode) < newNodeLength) {
// room for a free block, create it
newFreeBlock = (HeapNode*)((u8*)addr + newSizeAligned);
// update current node
nodeToUpdate->next = newFreeBlock;
nodeToUpdate->length = newSizeAligned;
// create new node after the current one
nodeToUpdate = newFreeBlock;
nodeToUpdate->next = nextNode;
nodeToUpdate->length = (newNodeLength - newSizeAligned) - sizeof(HeapNode);
nodeToUpdate->allocated = FALSE;
} else {
// no room, update our next and length
nodeToUpdate->next = nextNode;
nodeToUpdate->length = newNodeLength;
}
// return the location we were at
return addr;
}
f32 cosine(s16 arg0) {
s16 temp360;

View File

@ -1,79 +0,0 @@
.set noat # allow manual use of $at
.set noreorder # don't insert nops after branches
glabel _heap_realloc
/* 46C4 800292C4 27BDFFD8 */ addiu $sp, $sp, -0x28
/* 46C8 800292C8 AFB3001C */ sw $s3, 0x1c($sp)
/* 46CC 800292CC 0080982D */ daddu $s3, $a0, $zero
/* 46D0 800292D0 AFB20018 */ sw $s2, 0x18($sp)
/* 46D4 800292D4 00A0902D */ daddu $s2, $a1, $zero
/* 46D8 800292D8 AFB10014 */ sw $s1, 0x14($sp)
/* 46DC 800292DC 2651FFF0 */ addiu $s1, $s2, -0x10
/* 46E0 800292E0 24C6000F */ addiu $a2, $a2, 0xf
/* 46E4 800292E4 2402FFF0 */ addiu $v0, $zero, -0x10
/* 46E8 800292E8 AFBF0020 */ sw $ra, 0x20($sp)
/* 46EC 800292EC AFB00010 */ sw $s0, 0x10($sp)
/* 46F0 800292F0 9643FFF8 */ lhu $v1, -8($s2)
/* 46F4 800292F4 10600016 */ beqz $v1, .L80029350
/* 46F8 800292F8 00C28024 */ and $s0, $a2, $v0
/* 46FC 800292FC 8E45FFF0 */ lw $a1, -0x10($s2)
/* 4700 80029300 8E44FFF4 */ lw $a0, -0xc($s2)
/* 4704 80029304 00A0182D */ daddu $v1, $a1, $zero
/* 4708 80029308 10600009 */ beqz $v1, .L80029330
/* 470C 8002930C 0090102B */ sltu $v0, $a0, $s0
/* 4710 80029310 94620008 */ lhu $v0, 8($v1)
/* 4714 80029314 14400006 */ bnez $v0, .L80029330
/* 4718 80029318 0090102B */ sltu $v0, $a0, $s0
/* 471C 8002931C 8C650000 */ lw $a1, ($v1)
/* 4720 80029320 8C630004 */ lw $v1, 4($v1)
/* 4724 80029324 24820010 */ addiu $v0, $a0, 0x10
/* 4728 80029328 00432021 */ addu $a0, $v0, $v1
/* 472C 8002932C 0090102B */ sltu $v0, $a0, $s0
.L80029330:
/* 4730 80029330 10400011 */ beqz $v0, .L80029378
/* 4734 80029334 0220182D */ daddu $v1, $s1, $zero
/* 4738 80029338 0260202D */ daddu $a0, $s3, $zero
/* 473C 8002933C 0C00A41B */ jal _heap_malloc
/* 4740 80029340 0200282D */ daddu $a1, $s0, $zero
/* 4744 80029344 0040882D */ daddu $s1, $v0, $zero
/* 4748 80029348 16200003 */ bnez $s1, .L80029358
/* 474C 8002934C 0220202D */ daddu $a0, $s1, $zero
.L80029350:
/* 4750 80029350 0800A4EE */ j .L800293B8
/* 4754 80029354 0000102D */ daddu $v0, $zero, $zero
.L80029358:
/* 4758 80029358 0240282D */ daddu $a1, $s2, $zero
/* 475C 8002935C 0C01929D */ jal memcpy
/* 4760 80029360 0200302D */ daddu $a2, $s0, $zero
/* 4764 80029364 0260202D */ daddu $a0, $s3, $zero
/* 4768 80029368 0C00A487 */ jal _heap_free
/* 476C 8002936C 0240282D */ daddu $a1, $s2, $zero
/* 4770 80029370 0800A4EE */ j .L800293B8
/* 4774 80029374 0220102D */ daddu $v0, $s1, $zero
.L80029378:
/* 4778 80029378 26020010 */ addiu $v0, $s0, 0x10
/* 477C 8002937C 0044102B */ sltu $v0, $v0, $a0
/* 4780 80029380 1040000A */ beqz $v0, .L800293AC
/* 4784 80029384 02501021 */ addu $v0, $s2, $s0
/* 4788 80029388 AC620000 */ sw $v0, ($v1)
/* 478C 8002938C AC700004 */ sw $s0, 4($v1)
/* 4790 80029390 0040182D */ daddu $v1, $v0, $zero
/* 4794 80029394 00901023 */ subu $v0, $a0, $s0
/* 4798 80029398 2442FFF0 */ addiu $v0, $v0, -0x10
/* 479C 8002939C AC650000 */ sw $a1, ($v1)
/* 47A0 800293A0 AC620004 */ sw $v0, 4($v1)
/* 47A4 800293A4 0800A4ED */ j .L800293B4
/* 47A8 800293A8 A4600008 */ sh $zero, 8($v1)
.L800293AC:
/* 47AC 800293AC AC650000 */ sw $a1, ($v1)
/* 47B0 800293B0 AC640004 */ sw $a0, 4($v1)
.L800293B4:
/* 47B4 800293B4 0240102D */ daddu $v0, $s2, $zero
.L800293B8:
/* 47B8 800293B8 8FBF0020 */ lw $ra, 0x20($sp)
/* 47BC 800293BC 8FB3001C */ lw $s3, 0x1c($sp)
/* 47C0 800293C0 8FB20018 */ lw $s2, 0x18($sp)
/* 47C4 800293C4 8FB10014 */ lw $s1, 0x14($sp)
/* 47C8 800293C8 8FB00010 */ lw $s0, 0x10($sp)
/* 47CC 800293CC 03E00008 */ jr $ra
/* 47D0 800293D0 27BD0028 */ addiu $sp, $sp, 0x28