From fb70e0994b1d9baac4024639abcb43e0f4b799e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 10 Sep 2024 21:41:03 +0200 Subject: [PATCH] Simplify realloc Call to our free wrapper when size is 0. --- qjs.c | 12 ++++++------ quickjs.c | 8 +++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/qjs.c b/qjs.c index 72230f3..fe500d5 100644 --- a/qjs.c +++ b/qjs.c @@ -288,14 +288,14 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size) return NULL; return js_trace_malloc(s, size); } - old_size = js__malloc_usable_size(ptr); - if (size == 0) { - js_trace_malloc_printf(s, "R %zd %p\n", size, ptr); - s->malloc_count--; - s->malloc_size -= old_size + MALLOC_OVERHEAD; - free(ptr); + + if (unlikely(size == 0)) { + js_trace_free(s, ptr); return NULL; } + + old_size = js__malloc_usable_size(ptr); + /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ if (s->malloc_size + size - old_size > s->malloc_limit - 1) return NULL; diff --git a/quickjs.c b/quickjs.c index 61dab42..143af0b 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1781,13 +1781,11 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size) return NULL; return js_def_malloc(s, size); } - old_size = js__malloc_usable_size(ptr); - if (size == 0) { - s->malloc_count--; - s->malloc_size -= old_size + MALLOC_OVERHEAD; - free(ptr); + if (unlikely(size == 0)) { + js_def_free(s, ptr); return NULL; } + old_size = js__malloc_usable_size(ptr); /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ if (s->malloc_size + size - old_size > s->malloc_limit - 1) return NULL;