From 0044f9480cfa8d5cf0a3d84e84be25e32b588726 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 5 Feb 2011 08:35:05 -0800 Subject: Making malloc / free / realloc function pointers to be able to wrap them later for thread-safe reasons. --- libc/LIB.status | 2 +- libc/include/malloc.h | 14 +++++++++++--- libc/src/malloc.c | 10 +++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libc/LIB.status b/libc/LIB.status index 8827b2c..b2961a6 100644 --- a/libc/LIB.status +++ b/libc/LIB.status @@ -90,7 +90,7 @@ putchar - missing getdelim - missing, gnu replacement to gets getline - missing, gnu replacement to gets *puts - missing -ungetc - missing +ungetc - missing, won't implement, as we don't bufferize at all. fread - ok, inlined fwrite - ok, inlined fseek - missing diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 8dbfc05..ee877a1 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -5,9 +5,17 @@ #include #include -void * malloc(size_t size); -void free(void *ptr); -void * realloc(void *ptr, size_t size); +typedef void * (*malloc_t)(size_t size); +typedef void (*free_t)(void * ptr); +typedef void * (*realloc_t)(void * ptr, size_t size); + +void * base_malloc(size_t size); +void base_free(void * ptr); +void * base_realloc(void * ptr, size_t size); + +extern malloc_t malloc; +extern free_t free; +extern realloc_t realloc; static inline void * calloc(size_t nmemb, size_t size) { void * r = malloc(nmemb * size); diff --git a/libc/src/malloc.c b/libc/src/malloc.c index 2b8a806..fb6d954 100644 --- a/libc/src/malloc.c +++ b/libc/src/malloc.c @@ -30,7 +30,7 @@ static heap_t * find_fit(heap_t * head, size_t size) { return prev; } -void * malloc(size_t size) { +void * base_malloc(size_t size) { void * ptr = NULL, * heap_ptr; heap_t * new, * prev; @@ -103,7 +103,7 @@ void * malloc(size_t size) { return ptr; } -void * realloc(void * ptr, size_t size) { +void * base_realloc(void * ptr, size_t size) { heap_t * prev; void * new = NULL; @@ -155,7 +155,7 @@ void * realloc(void * ptr, size_t size) { return new; } -void free(void * ptr) { +void base_free(void * ptr) { heap_t * cur; void * top; size_t size; @@ -200,3 +200,7 @@ void free(void * ptr) { __attribute__((weak)) void * __builtin_new(size_t size) { return malloc(size); } __attribute__((weak)) void __builtin_delete(void * ptr) { free(ptr); } + +malloc_t malloc = base_malloc; +free_t free = base_free; +realloc_t realloc = base_realloc; -- cgit v1.2.3