From e5e91421929033707c8e49a1a17230f9a4e06ad6 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Wed, 23 Aug 2023 21:59:06 -0300 Subject: [PATCH] Implement realloc accounting correctly (#51027) This was potentially making us getting wrong results. (cherry picked from commit ce8acdd4d9cd8321141c37ddafb63062191418f4) --- src/gc.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/gc.c b/src/gc.c index ad0c545c13e8d..0d285b7251589 100644 --- a/src/gc.c +++ b/src/gc.c @@ -3711,11 +3711,12 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size int64_t diff = sz - old; if (diff < 0) { + diff = -diff; uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc); if (free_acc + diff < 16*1024) - jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + (-diff)); + jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + diff); else { - jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + (-diff))); + jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + diff)); jl_atomic_store_relaxed(&ptls->gc_num.free_acc, 0); } } @@ -3847,11 +3848,12 @@ static void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t olds int64_t diff = allocsz - oldsz; if (diff < 0) { + diff = -diff; uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc); if (free_acc + diff < 16*1024) - jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + (-diff)); + jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + diff); else { - jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + (-diff))); + jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + diff)); jl_atomic_store_relaxed(&ptls->gc_num.free_acc, 0); } }