Fix GC_bytes_allocd incrementation in case of allocation failure
authorIvan Maidanski <ivmai@mail.ru>
Tue, 27 Sep 2016 17:05:19 +0000 (20:05 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 27 Sep 2016 17:05:19 +0000 (20:05 +0300)
* 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
mallocx.c

index 85e86ee..9b72114 100644 (file)
--- 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);
index 1035a7e..740650a 100644 (file)
--- 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)