From 26b74b87bf38b0a377c622d166caeee61d49de27 Mon Sep 17 00:00:00 2001
From: Daniel Micay <danielmicay@gmail.com>
Date: Wed, 12 May 2021 20:28:37 -0400
Subject: [PATCH] improve code reuse for malloc API entry points

---
 h_malloc.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/h_malloc.c b/h_malloc.c
index 17891f4..c4f5696 100644
--- a/h_malloc.c
+++ b/h_malloc.c
@@ -1321,13 +1321,13 @@ static size_t adjust_size_for_canary(size_t size) {
 static inline void *alloc(size_t size) {
     unsigned arena = init();
     thread_unseal_metadata();
-    size = adjust_size_for_canary(size);
     void *p = allocate(arena, size);
     thread_seal_metadata();
     return p;
 }
 
 EXPORT void *h_malloc(size_t size) {
+    size = adjust_size_for_canary(size);
     return alloc(size);
 }
 
@@ -1337,11 +1337,8 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
         errno = ENOMEM;
         return NULL;
     }
-    unsigned arena = init();
-    thread_unseal_metadata();
     total_size = adjust_size_for_canary(total_size);
-    void *p = allocate(arena, total_size);
-    thread_seal_metadata();
+    void *p = alloc(total_size);
     if (!ZERO_ON_FREE && likely(p != NULL) && total_size && total_size <= MAX_SLAB_SIZE_CLASS) {
         memset(p, 0, total_size - canary_size);
     }
@@ -1349,12 +1346,11 @@ EXPORT void *h_calloc(size_t nmemb, size_t size) {
 }
 
 EXPORT void *h_realloc(void *old, size_t size) {
+    size = adjust_size_for_canary(size);
     if (old == NULL) {
         return alloc(size);
     }
 
-    size = adjust_size_for_canary(size);
-
     if (size > MAX_SLAB_SIZE_CLASS) {
         size = get_large_size_class(size);
         if (unlikely(!size)) {