Fixed a bug in the heap and nicer formatting for print_heap
This commit is contained in:
parent
69dfa17f1f
commit
3d1e43e8eb
|
@ -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");
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,19 +51,34 @@ 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) {
|
||||||
|
if (!current->used && !found_unused) {
|
||||||
|
first_unused = current;
|
||||||
|
found_unused = true;
|
||||||
|
} else {
|
||||||
|
found_unused = false;
|
||||||
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!found_unused) {
|
||||||
|
first_unused = current;
|
||||||
|
}
|
||||||
|
|
||||||
current->used = false;
|
current->used = false;
|
||||||
|
|
||||||
if (current->next == 0) {
|
if (current->next == 0) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue