Debugging with the heap & changes to it

This commit is contained in:
vanten-s 2024-08-10 14:37:31 +02:00
parent 1376ad96d4
commit 0fd7b67bce
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
4 changed files with 42 additions and 6 deletions

View file

@ -2,7 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
char get_last_key_pressed();
void print_hex_digit(uint8_t digit); void print_hex_digit(uint8_t digit);
void print_hex_byte(uint8_t byte); void print_hex_byte(uint8_t byte);
void print_hex_bytes(void* bytes, size_t len); void print_hex_bytes(void* bytes, size_t len);
void print_heap();

View file

@ -2,6 +2,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
extern struct Heap_Metadata global_heap;
struct Heap_Metadata { struct Heap_Metadata {
struct Heap_Block* start; struct Heap_Block* start;
size_t size; size_t size;

View file

@ -2,6 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <strlib.h> #include <strlib.h>
#include <kernel/heap.h>
void print_hex_digit(uint8_t digit) 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]); 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(&current, 4);
terminal_writestring("\nSize: ");
print_hex_bytes(&current->size, 4);
terminal_writestring("\nUsed: ");
if (current->used) {
terminal_writestring("True\n");
} else {
terminal_writestring("False\n");
}
terminal_writestring("Data: ");
print_hex_bytes(&current->data, 4);
terminal_putchar('\n');
current = current->next;
}
}

View file

@ -5,7 +5,7 @@
#define HEAP_SIZE (256 * 4096) #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; struct Heap_Metadata global_heap;
void heap_init() void heap_init()
@ -51,15 +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;
while (current->data != ptr) { current = current->next; } while (current->data != ptr) {
current = current->next;
struct Heap_Block* start = current; }
current->used = false; 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; current = current->next;
} }
start->next = current; start->next = current;
start->size = (uint32_t)start->next - (uint32_t)start->data; start->size = (uint32_t)start->next - (uint32_t)start->data;
} }