From 7542e55cb21f9590bcae75b34a8762485a6af296 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 10 Nov 2024 14:03:25 +0200 Subject: [PATCH] LibC: Add option to debug userspace malloc family calls --- userspace/libraries/LibC/malloc.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/userspace/libraries/LibC/malloc.cpp b/userspace/libraries/LibC/malloc.cpp index 147eaece..92e5d0b3 100644 --- a/userspace/libraries/LibC/malloc.cpp +++ b/userspace/libraries/LibC/malloc.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -6,6 +8,8 @@ #include #include +#define DEBUG_MALLOC 0 + static consteval size_t log_size_t(size_t value, size_t base) { size_t result = 0; @@ -174,6 +178,8 @@ static malloc_pool_t& pool_from_node(malloc_node_t* node) void* malloc(size_t size) { + dprintln_if(DEBUG_MALLOC, "malloc({})", size); + // align size to s_malloc_default_align boundary if (size_t ret = size % s_malloc_default_align) size += s_malloc_default_align - ret; @@ -207,6 +213,8 @@ void* malloc(size_t size) void* realloc(void* ptr, size_t size) { + dprintln_if(DEBUG_MALLOC, "realloc({}, {})", ptr, size); + if (ptr == nullptr) return malloc(size); @@ -237,6 +245,8 @@ void* realloc(void* ptr, size_t size) void free(void* ptr) { + dprintln_if(DEBUG_MALLOC, "free({})", ptr); + if (ptr == nullptr) return; @@ -265,6 +275,8 @@ void free(void* ptr) void* calloc(size_t nmemb, size_t size) { + dprintln_if(DEBUG_MALLOC, "calloc({}, {})", nmemb, size); + size_t total = nmemb * size; if (size != 0 && total / size != nmemb) {