From c11aef28278160df8720a755417a2fb6f1c56278 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 27 Sep 2016 20:05:19 +0300 Subject: [PATCH] Fix GC_bytes_allocd incrementation in case of allocation failure * malloc.c (GC_generic_malloc_inner, GC_generic_malloc_inner_ignore_off_page, GC_generic_malloc): Increment GC_bytes_allocd only if the allocation successful (op != NULL). * mallocx.c (GC_generic_malloc_ignore_off_page): Likewise. --- malloc.c | 8 +++++--- mallocx.c | 42 ++++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/malloc.c b/malloc.c index 85e86ee..9b72114 100644 --- a/malloc.c +++ b/malloc.c @@ -150,7 +150,8 @@ GC_INNER void * GC_generic_malloc_inner(size_t lb, int k) GC_bytes_allocd += GRANULES_TO_BYTES(lg); } else { op = (ptr_t)GC_alloc_large_and_clear(ADD_SLOP(lb), k, 0); - GC_bytes_allocd += lb; + if (op != NULL) + GC_bytes_allocd += lb; } return op; @@ -171,7 +172,8 @@ GC_INNER void * GC_generic_malloc_inner(size_t lb, int k) GC_ASSERT(k < MAXOBJKINDS); lb_adjusted = ADD_SLOP(lb); op = GC_alloc_large_and_clear(lb_adjusted, k, IGNORE_OFF_PAGE); - GC_bytes_allocd += lb_adjusted; + if (op != NULL) + GC_bytes_allocd += lb_adjusted; return op; } #endif @@ -221,8 +223,8 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k) ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0; # endif } + GC_bytes_allocd += lb_rounded; } - GC_bytes_allocd += lb_rounded; UNLOCK(); if (init && !GC_debugging_started && 0 != result) { BZERO(result, n_blocks * HBLKSIZE); diff --git a/mallocx.c b/mallocx.c index 1035a7e..740650a 100644 --- a/mallocx.c +++ b/mallocx.c @@ -196,32 +196,30 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_DBG_COLLECT_AT_MALLOC(lb); LOCK(); result = (ptr_t)GC_alloc_large(ADD_SLOP(lb), k, IGNORE_OFF_PAGE); - if (0 != result) { - if (GC_debugging_started) { - BZERO(result, n_blocks * HBLKSIZE); - } else { -# ifdef THREADS - /* Clear any memory that might be used for GC descriptors */ - /* before we release the lock. */ - ((word *)result)[0] = 0; - ((word *)result)[1] = 0; - ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0; - ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0; -# endif - } - } - GC_bytes_allocd += lb_rounded; - if (0 == result) { + if (NULL == result) { GC_oom_func oom_fn = GC_oom_fn; UNLOCK(); - return((*oom_fn)(lb)); + return (*oom_fn)(lb); + } + + if (GC_debugging_started) { + BZERO(result, n_blocks * HBLKSIZE); } else { - UNLOCK(); - if (init && !GC_debugging_started) { - BZERO(result, n_blocks * HBLKSIZE); - } - return(result); +# ifdef THREADS + /* Clear any memory that might be used for GC descriptors */ + /* before we release the lock. */ + ((word *)result)[0] = 0; + ((word *)result)[1] = 0; + ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0; + ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0; +# endif + } + GC_bytes_allocd += lb_rounded; + UNLOCK(); + if (init && !GC_debugging_started) { + BZERO(result, n_blocks * HBLKSIZE); } + return(result); } GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_ignore_off_page(size_t lb) -- 2.7.4