From f336936309e2803ca6b82612240d36ba45b017cf Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Fri, 5 Aug 2016 12:10:19 -0700 Subject: [PATCH] Improve overall handling of realloc --- crt/memory.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/crt/memory.c b/crt/memory.c index 056df20..6cd0b5b 100644 --- a/crt/memory.c +++ b/crt/memory.c @@ -20,15 +20,30 @@ __wrap_free(void *ptr) { } } +size_t +__wrap_malloc_usable_size(void *ptr) { + return MEMGetSizeForMBlockExpHeap(ptr); +} + void * __wrap_realloc(void *ptr, size_t size) { + if (!ptr) { + return __wrap_malloc(size); + } + + if (__wrap_malloc_usable_size(ptr) >= size) { + return ptr; + } + void *realloc_ptr = __wrap_malloc(size); - if(realloc_ptr) { - memcpy(realloc_ptr, ptr, size); - __wrap_free(ptr); + if(!realloc_ptr) { + return NULL; } + memcpy(realloc_ptr, ptr, __wrap_malloc_usable_size(ptr)); + __wrap_free(ptr); + return realloc_ptr; } @@ -43,11 +58,6 @@ __wrap_calloc(size_t num, size_t size) { return ptr; } -size_t -__wrap_malloc_usable_size(void *ptr) { - return MEMGetSizeForMBlockExpHeap(ptr); -} - void * __wrap_valloc(size_t size) { return __wrap_memalign(64, size);