diff options
Diffstat (limited to 'libc')
-rw-r--r-- | libc/inc/mem.h | 14 | ||||
-rw-r--r-- | libc/mem.c | 26 |
2 files changed, 29 insertions, 11 deletions
diff --git a/libc/inc/mem.h b/libc/inc/mem.h index 9075736..4750efb 100644 --- a/libc/inc/mem.h +++ b/libc/inc/mem.h @@ -5,15 +5,19 @@ #include <def.h> +void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp); +void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp); +#define malloc(size) malloc_debug(size, __FILE__, __LINE__, __func__, #size) +#define free(ptr) free_debug(ptr, __FILE__, __LINE__, __func__, #ptr) + // Huh #ifdef kernel void heap_init(u32 start); -void *malloc(u32 size); -void free(void *ptr); +/* void *malloc(u32 size); */ +/* void free(void *ptr); */ #elif defined(userspace) -#include <print.h> -void *malloc(u32 size); -void free(void *ptr); +/* void *malloc(u32 size); */ +/* void free(void *ptr); */ #else #error "No lib target specified. Please use -Dkernel or -Duserspace" #endif @@ -235,7 +235,7 @@ void heap_init(u32 start) heap.end = start + HEAP_INIT_SIZE; } -void *malloc(u32 size) +void *_malloc(u32 size) { u32 index = bin_index(size); struct h_bin *temp = (struct h_bin *)&heap.bins[index]; @@ -279,8 +279,11 @@ void *malloc(u32 size) return &found->next; } -void free(void *p) +void _free(void *p) { + if (!p) + return; + struct h_bin *list; struct h_footer *new_foot, *old_foot; @@ -330,16 +333,27 @@ void free(void *p) #define kmalloc(n) (void *)sys1(SYS_MALLOC, n) #define kfree(ptr) (void)(sys1(SYS_FREE, (int)ptr)) -static u32 total = 0; -void *malloc(u32 size) +void *_malloc(u32 size) { - total += size; return kmalloc(size); } -void free(void *ptr) +void _free(void *ptr) { kfree(ptr); } #endif + +void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp) +{ + printf("MALLOC:\t%s:%d: %s: %dB (%s)\n", file, line, func, size, inp); + return _malloc(size); +} + +void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp) +{ + printf("FREE:\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp); + if (ptr) + _free(ptr); +} |