Fixed a bug in the heap and nicer formatting for print_heap

This commit is contained in:
vanten-s 2024-08-12 16:27:53 +02:00
parent 69dfa17f1f
commit 3d1e43e8eb
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
2 changed files with 20 additions and 3 deletions

View file

@ -33,6 +33,7 @@ void print_hex_bytes(void* bytes, size_t len)
void print_heap() void print_heap()
{ {
terminal_writestring("--START HEAP--\n");
struct Heap_Block* current = global_heap.start; struct Heap_Block* current = global_heap.start;
while (current != 0) { while (current != 0) {
@ -55,4 +56,5 @@ void print_heap()
current = current->next; current = current->next;
} }
terminal_writestring("--END HEAP--\n");
} }

View file

@ -51,8 +51,22 @@ void* malloc(size_t size)
void free(void *ptr) void free(void *ptr)
{ {
struct Heap_Block* current = global_heap.start; struct Heap_Block* current = global_heap.start;
struct Heap_Block* first_unused;
bool found_unused = false;
while (current->data != ptr) { while (current->data != ptr) {
current = current->next; if (!current->used && !found_unused) {
first_unused = current;
found_unused = true;
} else {
found_unused = false;
}
current = current->next;
}
if (!found_unused) {
first_unused = current;
} }
current->used = false; current->used = false;
@ -61,9 +75,10 @@ void free(void *ptr)
return; return;
} }
struct Heap_Block* start = current; struct Heap_Block* start = first_unused;
current = start;
while (current->used == false && current != 0) { while (current->used == false && current->next != 0) {
current = current->next; current = current->next;
} }