Skip to content

Commit fb70e09

Browse files
committed
Simplify realloc
Call to our free wrapper when size is 0.
1 parent 5a41aaa commit fb70e09

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

qjs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
288288
return NULL;
289289
return js_trace_malloc(s, size);
290290
}
291-
old_size = js__malloc_usable_size(ptr);
292-
if (size == 0) {
293-
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
294-
s->malloc_count--;
295-
s->malloc_size -= old_size + MALLOC_OVERHEAD;
296-
free(ptr);
291+
292+
if (unlikely(size == 0)) {
293+
js_trace_free(s, ptr);
297294
return NULL;
298295
}
296+
297+
old_size = js__malloc_usable_size(ptr);
298+
299299
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
300300
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
301301
return NULL;

quickjs.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,13 +1781,11 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
17811781
return NULL;
17821782
return js_def_malloc(s, size);
17831783
}
1784-
old_size = js__malloc_usable_size(ptr);
1785-
if (size == 0) {
1786-
s->malloc_count--;
1787-
s->malloc_size -= old_size + MALLOC_OVERHEAD;
1788-
free(ptr);
1784+
if (unlikely(size == 0)) {
1785+
js_def_free(s, ptr);
17891786
return NULL;
17901787
}
1788+
old_size = js__malloc_usable_size(ptr);
17911789
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
17921790
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
17931791
return NULL;

0 commit comments

Comments
 (0)