diff --git a/malloc.c b/malloc.c index b930819..6be7be1 100644 --- a/malloc.c +++ b/malloc.c @@ -748,11 +748,8 @@ static void *allocate(size_t size) { return p; } -static void deallocate(void *p) { - if (p >= ro.slab_region_start && p < ro.slab_region_end) { - slab_free(p); - return; - } +static void deallocate_large(void *p) { + enforce_init(); mutex_lock(®ions_lock); struct region_info *region = regions_find(p); @@ -810,7 +807,6 @@ EXPORT void *h_realloc(void *old, size_t size) { return allocate(size); } - enforce_init(); size = adjust_size_for_canaries(size); size_t old_size; @@ -820,6 +816,8 @@ EXPORT void *h_realloc(void *old, size_t size) { return old; } } else { + enforce_init(); + mutex_lock(®ions_lock); struct region_info *region = regions_find(old); if (region == NULL) { @@ -892,7 +890,11 @@ EXPORT void *h_realloc(void *old, size_t size) { copy_size -= canary_size; } memcpy(new, old, copy_size); - deallocate(old); + if (old_size <= max_slab_size_class) { + slab_free(old); + } else { + deallocate_large(old); + } return new; } @@ -981,8 +983,12 @@ EXPORT void h_free(void *p) { return; } - enforce_init(); - deallocate(p); + if (p >= ro.slab_region_start && p < ro.slab_region_end) { + slab_free(p); + return; + } + + deallocate_large(p); } EXPORT void h_cfree(void *ptr) ALIAS(h_free); @@ -992,13 +998,13 @@ EXPORT size_t h_malloc_usable_size(void *p) { return 0; } - enforce_init(); - if (p >= ro.slab_region_start && p < ro.slab_region_end) { size_t size = slab_usable_size(p); return size ? size - canary_size : 0; } + enforce_init(); + mutex_lock(®ions_lock); struct region_info *region = regions_find(p); if (p == NULL) { @@ -1011,7 +1017,7 @@ EXPORT size_t h_malloc_usable_size(void *p) { } EXPORT size_t h_malloc_object_size(void *p) { - if (p == NULL || unlikely(!is_init())) { + if (p == NULL) { return 0; } @@ -1020,6 +1026,10 @@ EXPORT size_t h_malloc_object_size(void *p) { return size ? size - canary_size : 0; } + if (unlikely(!is_init())) { + return 0; + } + mutex_lock(®ions_lock); struct region_info *region = regions_find(p); size_t size = p == NULL ? SIZE_MAX : region->size; @@ -1029,7 +1039,7 @@ EXPORT size_t h_malloc_object_size(void *p) { } EXPORT size_t h_malloc_object_size_fast(void *p) { - if (p == NULL || unlikely(!is_init())) { + if (p == NULL) { return 0; } @@ -1038,6 +1048,10 @@ EXPORT size_t h_malloc_object_size_fast(void *p) { return size ? size - canary_size : 0; } + if (unlikely(!is_init())) { + return 0; + } + return SIZE_MAX; }