summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2023-02-07 17:10:15 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2023-02-07 17:16:28 -0400
commit869b2933fa3f62ff4eb075772a246ee54b881fe1 (patch)
treeacb353146c776ee294775539a4a6ddf10d85e89f /src
parent149211e2a7e8044fbf4d3e1e2cec867ea30aacaa (diff)
THeap: Add, and make use of `heap_node` struct
Diffstat (limited to 'src')
-rw-r--r--src/pso/THeap.cpp73
1 files changed, 37 insertions, 36 deletions
diff --git a/src/pso/THeap.cpp b/src/pso/THeap.cpp
index d2da513..bf2a35a 100644
--- a/src/pso/THeap.cpp
+++ b/src/pso/THeap.cpp
@@ -29,28 +29,28 @@ void *xmalloc(size_t size) {
void THeap::heap_free(void *ptr) {
#define heap_ptr &ptr_u8[-heap_offset]
- #define ptr_heap ((THeap *)heap_ptr)
+ #define ptr_heap ((heap_node *)heap_ptr)
const u8 *ptr_u8 = (u8 *)ptr;
if (ptr == NULL) {
return;
} else {
- THeap *parent = (THeap *)heap;
- THeap *child;
+ heap_node *prev_node = heap_nodes;
+ heap_node *node;
- for (; child = (THeap *)parent->heap, child < ptr_heap; parent = child);
+ for (; node = prev_node->next, node < ptr_heap; prev_node = node);
- if (((u8 *)&ptr_heap->heap + ptr_heap->alloc_size) == (u8 *)&child->heap) {
- ptr_heap->heap = child->heap;
- ptr_heap->alloc_size += child->alloc_size;
+ if (((u8 *)&ptr_heap->next + ptr_heap->remaining_size) == (u8 *)&node->next) {
+ ptr_heap->next = node->next;
+ ptr_heap->remaining_size += node->remaining_size;
} else {
- ptr_heap->heap = (u8 *)child;
+ ptr_heap->next = node;
}
- if (((u8 *)&parent->heap + parent->alloc_size) == heap_ptr) {
- parent->heap = ptr_heap->heap;
- parent->alloc_size += ptr_heap->alloc_size;
+ if (((u8 *)&prev_node->next + prev_node->remaining_size) == heap_ptr) {
+ prev_node->next = ptr_heap->next;
+ prev_node->remaining_size += ptr_heap->remaining_size;
} else {
- parent->heap = (u8 *)heap_ptr;
+ prev_node->next = ptr_heap;
}
}
#undef ptr_heap
@@ -68,33 +68,34 @@ void *THeap::heap_zalloc(size_t size) {
// Getting closer, but register allocation issues.
#ifndef MATCHING
void *THeap::heap_alloc(size_t size) {
- THeap *parent = (THeap *)heap;
- THeap *child;
+ heap_node *prev_node = heap_nodes;
+ heap_node *node;
u32 size_align = (heap_offset - 1) + align + size;
u32 aligned_size = size_align & -align;
goto start;
cond:
- if (child->alloc_size < aligned_size) {
+ if (node->remaining_size < aligned_size) {
goto loop_body;
}
top:
- if (aligned_size == child->alloc_size) {
- parent->heap = child->heap;
+ if (aligned_size == node->remaining_size) {
+ prev_node->heap = node->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;
+ u8 *next_u8 = (u8 *)&node->next;
+ heap_node *tmp = &next_u8[aligned_size];
+ tmp->next = node->next;
+ tmp->remaining_size = node->remaining_size - aligned_size;
+ node->remaining_size = aligned_size;
+ prev_node->next = tmp;
}
- return (void *)&child->align;
+ return node + 1;
loop_body:
- parent = child;
+ prev_node = node;
start:
- if (child = (THeap *)parent->heap, child == NULL) {
+ if (node = prev_node->next, node == NULL) {
return NULL;
}
goto cond;
@@ -142,7 +143,7 @@ lbl_80141090:
#endif
THeap::~THeap() {
- xfree(heap);
+ xfree(heap_nodes);
}
// Getting closer, still not sure how to match though.
@@ -151,17 +152,17 @@ THeap::THeap(size_t size, int alignment) {
heap_size = 0;
mbr_0x10 = 0;
align = alignment;
- heap = (u8 *)xmalloc(heap_size);
- if (heap != NULL) {
+ heap_nodes = (heap_node *)xmalloc(size);
+ if (heap_nodes != 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;
+ heap_node *tmp_heap;
+ memset(heap_nodes, 0, size);
+
+ tmp_heap = heap_nodes;
+ tmp_heap->next = tmp_heap + 1;
+ heap_nodes->alloc_size = 0;
+ tmp_heap[1].align = 0;
+ tmp_heap[1].heap_size = new_size;
}
}
#else