Debugging with the heap & changes to it
This commit is contained in:
parent
1376ad96d4
commit
0fd7b67bce
|
@ -2,7 +2,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
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();
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern struct Heap_Metadata global_heap;
|
||||
|
||||
struct Heap_Metadata {
|
||||
struct Heap_Block* start;
|
||||
size_t size;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <strlib.h>
|
||||
#include <kernel/heap.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue