]> granicus.if.org Git - gc/commitdiff
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, 1 Nov 2016 23:26:39 +0000 (02:26 +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 3831822b3c3e4ebaed67a0e17c38122ce31c22ac..1c2f019db2c25540630825ab5c285a735fac95d7 100644 (file)
--- a/malloc.c
+++ b/malloc.c
@@ -140,7 +140,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;
@@ -159,7 +160,8 @@ GC_INNER void * GC_generic_malloc_inner(size_t lb, int k)
         return(GC_generic_malloc_inner(lb, k));
     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
@@ -210,8 +212,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 a526d9e356d55561fc6eaf02b03225487fa2f966..9c339959f9adcb5102c522f4e037c15cf33f7547 100644 (file)
--- a/mallocx.c
+++ b/mallocx.c
@@ -186,32 +186,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)