From c68de6141d7f91ba7968fa25f6092fc9685980b3 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 7 Apr 2019 05:48:10 -0400 Subject: [PATCH] factor out duplicated code in malloc/realloc --- h_malloc.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/h_malloc.c b/h_malloc.c index fec0d5c..8cf70cb 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1182,13 +1182,6 @@ static void deallocate_large(void *p, const size_t *expected_size) { regions_quarantine_deallocate_pages(p, size, guard_size); } -static size_t adjust_size_for_canaries(size_t size) { - if (size > 0 && size <= max_slab_size_class) { - return size + canary_size; - } - return size; -} - static int alloc_aligned(void **memptr, size_t alignment, size_t size, size_t min_alignment) { if ((alignment - 1) & alignment || alignment < min_alignment) { return EINVAL; @@ -1240,7 +1233,14 @@ static void *alloc_aligned_simple(size_t alignment, size_t size) { return ptr; } -EXPORT void *h_malloc(size_t size) { +static size_t adjust_size_for_canaries(size_t size) { + if (size > 0 && size <= max_slab_size_class) { + return size + canary_size; + } + return size; +} + +static inline void *alloc(size_t size) { init(); thread_unseal_metadata(); size = adjust_size_for_canaries(size); @@ -1249,6 +1249,10 @@ EXPORT void *h_malloc(size_t size) { return p; } +EXPORT void *h_malloc(size_t size) { + return alloc(size); +} + EXPORT void *h_calloc(size_t nmemb, size_t size) { size_t total_size; if (unlikely(__builtin_mul_overflow(nmemb, size, &total_size))) { @@ -1268,12 +1272,7 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) { EXPORT void *h_realloc(void *old, size_t size) { if (old == NULL) { - init(); - thread_unseal_metadata(); - size = adjust_size_for_canaries(size); - void *p = allocate(size); - thread_seal_metadata(); - return p; + return alloc(size); } size = adjust_size_for_canaries(size);