#include "pso/THeap.h"
#include <stdlib.h>
#include <string.h>
#include <global_types.h>
#undef MATCHING
#define MATCHING
static const int heap_offset = 8;
THeap *obj_heap;
THeap *alt_heap;
void xfree(void *ptr) {
}
void *xmalloc(size_t size) {
return NULL;
}
void THeap::heap_free(void *ptr) {
#define heap_ptr &ptr_u8[-heap_offset]
#define ptr_heap ((THeap *)heap_ptr)
const u8 *ptr_u8 = (u8 *)ptr;
if (ptr == NULL) {
return;
} else {
THeap *parent = (THeap *)heap;
THeap *child;
for (; child = (THeap *)parent->heap, child < ptr_heap; parent = child);
if (((u8 *)&ptr_heap->heap + ptr_heap->alloc_size) == (u8 *)&child->heap) {
ptr_heap->heap = child->heap;
ptr_heap->alloc_size += child->alloc_size;
} else {
ptr_heap->heap = (u8 *)child;
}
if (((u8 *)&parent->heap + parent->alloc_size) == heap_ptr) {
parent->heap = ptr_heap->heap;
parent->alloc_size += ptr_heap->alloc_size;
} else {
parent->heap = (u8 *)heap_ptr;
}
}
#undef ptr_heap
#undef heap_ptr
}
void *THeap::heap_zalloc(size_t size) {
void *ptr = heap_alloc(size);
if (ptr != NULL) {
memset(ptr, 0, size);
}
return ptr;
}
#ifndef MATCHING
void *THeap::heap_alloc(size_t size) {
THeap *parent = (THeap *)heap;
THeap *child;
u32 size_align = (heap_offset - 1) + align + size;
u32 aligned_size = size_align & -align;
goto start;
cond:
if (child->alloc_size < aligned_size) {
goto loop_body;
}
top:
if (aligned_size == child->alloc_size) {
parent->heap = child->heap;
} else {
THeap *tmp = (THeap *)&child->heap[aligned_size];
tmp->heap = child->heap;
tmp->alloc_size = child->alloc_size - aligned_size;
child->alloc_size = aligned_size;
parent->heap = (u8 *)&tmp->heap;
}
return (void *)&child->align;
loop_body:
parent = child;
start:
if (child = (THeap *)parent->heap, child == NULL) {
return NULL;
}
goto cond;
}
#else
asm void *THeap::heap_alloc(size_t size) {
nofralloc
lwz r0, 8(r3)
lwz r3, 0(r3)
add r6, r0, r4
neg r0, r0
addi r6, r6, 0x7
and r6, r6, r0
b lbl_80141090
lbl_80141044:
lwz r0, 4(r4)
cmplw r0, r6
blt- lbl_8014108c
cmplw r6, r0
bne- lbl_80141064
lwz r0, 0(r4)
stw r0, 0(r3)
b lbl_80141084
lbl_80141064:
lwz r0, 0(r4)
add r5, r4, r6
stw r0, 0(r5)
lwz r0, 4(r4)
subf r0, r6, r0
stw r0, 4(r5)
stw r6, 4(r4)
stw r5, 0(r3)
lbl_80141084:
addi r3, r4, 0x8
blr
lbl_8014108c:
mr r3, r4
lbl_80141090:
lwz r4, 0(r3)
cmplwi r4, 0
bne+ lbl_80141044
li r3, 0x0
blr
}
#endif
THeap::~THeap() {
xfree(heap);
}
#ifndef MATCHING
THeap::THeap(size_t size, int alignment) {
heap_size = 0;
mbr_0x10 = 0;
align = alignment;
heap = (u8 *)xmalloc(heap_size);
if (heap != NULL) {
const u32 new_size = size - heap_offset;
THeap *tmp_heap;
memset(heap, 0, size);
tmp_heap = (THeap *)heap;
tmp_heap->heap = &heap[heap_offset];
((THeap *)heap)->alloc_size = 0;
tmp_heap->align = 0;
tmp_heap->heap_size = new_size;
}
}
#else
asm THeap::THeap(size_t size, int alignment) {
nofralloc
stwu r1, -0x10(r1)
mflr r0
stw r0, 0x14(r1)
li r0, 0x0
stmw r30, 8(r1)
mr r30, r3
mr r31, r4
stw r0, 0xc(r3)
mr r3, r31
stw r0, 0x10(r30)
stw r5, 8(r30)
bl xmalloc
stw r3, 0(r30)
lwz r3, 0(r30)
cmplwi r3, 0
beq- lbl_80141160
mr r5, r31
li r4, 0x0
bl memset
lwz r3, 0(r30)
li r4, 0x0
addi r0, r31, -0x8
addi r5, r3, 0x8
stw r5, 0(r3)
lwz r3, 0(r30)
stw r4, 4(r3)
stw r4, 0(r5)
stw r0, 4(r5)
lbl_80141160:
mr r3, r30
lmw r30, 8(r1)
lwz r0, 0x14(r1)
mtlr r0
addi r1, r1, 0x10
blr
}
#endif