From 0fd7b67bceb81d869609bad11612fc1e21e952b4 Mon Sep 17 00:00:00 2001 From: vanten-s Date: Sat, 10 Aug 2024 14:37:31 +0200 Subject: [PATCH] Debugging with the heap & changes to it --- include/debugging.h | 2 +- include/kernel/heap.h | 2 ++ kernel/debugging.c | 27 +++++++++++++++++++++++++++ kernel/heap.c | 17 ++++++++++++----- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/include/debugging.h b/include/debugging.h index 4fd0bf0..068350c 100644 --- a/include/debugging.h +++ b/include/debugging.h @@ -2,7 +2,7 @@ #include #include -char get_last_key_pressed(); void print_hex_digit(uint8_t digit); void print_hex_byte(uint8_t byte); void print_hex_bytes(void* bytes, size_t len); +void print_heap(); diff --git a/include/kernel/heap.h b/include/kernel/heap.h index 3a3ae47..f13ffec 100644 --- a/include/kernel/heap.h +++ b/include/kernel/heap.h @@ -2,6 +2,8 @@ #include #include +extern struct Heap_Metadata global_heap; + struct Heap_Metadata { struct Heap_Block* start; size_t size; diff --git a/kernel/debugging.c b/kernel/debugging.c index 1d129f9..b6d02b5 100644 --- a/kernel/debugging.c +++ b/kernel/debugging.c @@ -2,6 +2,7 @@ #include #include #include +#include void print_hex_digit(uint8_t digit) { @@ -29,3 +30,29 @@ void print_hex_bytes(void* bytes, size_t len) print_hex_byte(value[i - 1]); } } + +void print_heap() +{ + struct Heap_Block* current = global_heap.start; + + while (current != 0) { + terminal_writestring("Block at: "); + print_hex_bytes(¤t, 4); + + terminal_writestring("\nSize: "); + print_hex_bytes(¤t->size, 4); + + terminal_writestring("\nUsed: "); + if (current->used) { + terminal_writestring("True\n"); + } else { + terminal_writestring("False\n"); + } + + terminal_writestring("Data: "); + print_hex_bytes(¤t->data, 4); + terminal_putchar('\n'); + + current = current->next; + } +} diff --git a/kernel/heap.c b/kernel/heap.c index 3ec7641..317b82e 100644 --- a/kernel/heap.c +++ b/kernel/heap.c @@ -5,7 +5,7 @@ #define HEAP_SIZE (256 * 4096) -uint8_t* global_heap_data[HEAP_SIZE]; +uint8_t* global_heap_data[HEAP_SIZE + sizeof(struct Heap_Block)]; struct Heap_Metadata global_heap; void heap_init() @@ -51,15 +51,22 @@ void* malloc(size_t size) void free(void *ptr) { struct Heap_Block* current = global_heap.start; - while (current->data != ptr) { current = current->next; } - - struct Heap_Block* start = current; + while (current->data != ptr) { + current = current->next; + } current->used = false; - while (current->used == false) { + if (current->next == 0) { + return; + } + + struct Heap_Block* start = current; + + while (current->used == false && current->next != 0) { current = current->next; } + start->next = current; start->size = (uint32_t)start->next - (uint32_t)start->data; }