Simplify realloc

Call to our free wrapper when size is 0.
This commit is contained in:
Saúl Ibarra Corretgé 2024-09-10 21:41:03 +02:00
parent 5a41aaa6a3
commit fb70e0994b
2 changed files with 9 additions and 11 deletions

12
qjs.c
View file

@ -288,14 +288,14 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL; return NULL;
return js_trace_malloc(s, size); return js_trace_malloc(s, size);
} }
old_size = js__malloc_usable_size(ptr);
if (size == 0) { if (unlikely(size == 0)) {
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr); js_trace_free(s, ptr);
s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD;
free(ptr);
return NULL; return NULL;
} }
old_size = js__malloc_usable_size(ptr);
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
if (s->malloc_size + size - old_size > s->malloc_limit - 1) if (s->malloc_size + size - old_size > s->malloc_limit - 1)
return NULL; return NULL;

View file

@ -1781,13 +1781,11 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL; return NULL;
return js_def_malloc(s, size); return js_def_malloc(s, size);
} }
old_size = js__malloc_usable_size(ptr); if (unlikely(size == 0)) {
if (size == 0) { js_def_free(s, ptr);
s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD;
free(ptr);
return NULL; return NULL;
} }
old_size = js__malloc_usable_size(ptr);
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
if (s->malloc_size + size - old_size > s->malloc_limit - 1) if (s->malloc_size + size - old_size > s->malloc_limit - 1)
return NULL; return NULL;