diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index 59f4660d..ac1b9bcd 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -328,4 +328,36 @@ Result kcalloc(usize nmemb, usize size) usize realsize = nmemb * size; void* ptr = TRY(kmalloc(realsize)); return memset(ptr, 0, realsize); +} + +void dump_heap_usage() +{ + Serial::println("-- Dumping usage stats for kernel heap:"); + if (!heap_start) + { + Serial::println("- Heap is not currently being used"); + return; + } + usize alloc_total = 0; + usize alloc_used = 0; + HeapBlock* block = heap_start; + while (block) + { + if (is_block_free(block)) + { + Serial::printf("- Available block, of size %zu\n", block->full_size); + alloc_total += block->full_size + sizeof(HeapBlock); + } + else + { + Serial::printf("- Used block, of size %zu, of which %zu bytes are being used\n", block->full_size, + block->req_size); + alloc_total += block->full_size + sizeof(HeapBlock); + alloc_used += block->req_size; + } + block = block->next; + } + + Serial::printf("-- Total memory allocated for heap: %zu bytes\n", alloc_total); + Serial::printf("-- Heap memory in use by the kernel: %zu bytes\n", alloc_used); } \ No newline at end of file diff --git a/kernel/src/memory/Heap.h b/kernel/src/memory/Heap.h index b7bf53e9..251e039c 100644 --- a/kernel/src/memory/Heap.h +++ b/kernel/src/memory/Heap.h @@ -17,4 +17,6 @@ template Result destroy(T* item) { item->~T(); return kfree(item); -} \ No newline at end of file +} + +void dump_heap_usage(); \ No newline at end of file