Do not call BCOPY and BZERO if size is zero
authorIvan Maidanski <ivmai@mail.ru>
Wed, 9 Aug 2017 08:10:38 +0000 (11:10 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 9 Aug 2017 08:26:14 +0000 (11:26 +0300)
* dbg_mlc.c (GC_debug_strndup): Do not call BCOPY() if elements count
is zero.
* dbg_mlc.c (GC_debug_realloc): Likewise.
* finalize.c [!GC_TOGGLE_REFS_NOT_NEEDED] (ensure_toggleref_capacity):
Likewise.
* malloc.c [REDIRECT_MALLOC && !strndup] (strndup): Likewise.
* mallocx.c (GC_strndup): Likewise.
* misc.c [!GC_GET_HEAP_USAGE_NOT_NEEDED] (GC_get_prof_stats): Likewise.
* os_dep.c [SAVE_CALL_CHAIN && GC_HAVE_BUILTIN_BACKTRACE]
(GC_save_callers): Likewise.
* reclaim.c (GC_print_all_errors): Likewise.
* malloc.c (GC_free): Do not call BZERO() if elements count is zero.
* malloc.c [THREADS] (GC_free_inner): Likewise.
* reclaim.c (GC_print_all_errors): Likewise.
* misc.c [!GC_GET_HEAP_USAGE_NOT_NEEDED && THREADS]
(GC_get_prof_stats_unsafe): Do not call fill_prof_stats() and BCOPY()
if stats_sz is zero.

dbg_mlc.c
finalize.c
malloc.c
mallocx.c
misc.c
os_dep.c
reclaim.c

index 724d9cf..4edeeef 100644 (file)
--- a/dbg_mlc.c
+++ b/dbg_mlc.c
@@ -760,7 +760,8 @@ GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
 #   endif
     return NULL;
   }
-  BCOPY(str, copy, len);
+  if (len > 0)
+    BCOPY(str, copy, len);
   copy[len] = '\0';
   return copy;
 }
@@ -973,7 +974,8 @@ GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
 #     else
         old_sz = ((oh *)base) -> oh_sz;
 #     endif
-      BCOPY(p, result, old_sz < lb ? old_sz : lb);
+      if (old_sz > 0)
+        BCOPY(p, result, old_sz < lb ? old_sz : lb);
       GC_debug_free(p);
     }
     return(result);
index bd297a9..ad7e03b 100644 (file)
@@ -413,8 +413,9 @@ GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
                         NORMAL);
       if (NULL == new_array)
         return FALSE;
-      BCOPY(GC_toggleref_arr, new_array,
-            GC_toggleref_array_size * sizeof(GCToggleRef));
+      if (EXPECT(GC_toggleref_array_size > 0, TRUE))
+        BCOPY(GC_toggleref_arr, new_array,
+              GC_toggleref_array_size * sizeof(GCToggleRef));
       GC_INTERNAL_FREE(GC_toggleref_arr);
       GC_toggleref_arr = new_array;
     }
index 502b3e3..dbffee2 100644 (file)
--- a/malloc.c
+++ b/malloc.c
@@ -490,7 +490,8 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_uncollectable(size_t lb)
         errno = ENOMEM;
         return NULL;
       }
-      BCOPY(str, copy, len);
+      if (EXPECT(len > 0, TRUE))
+        BCOPY(str, copy, len);
       copy[len] = '\0';
       return copy;
     }
@@ -545,7 +546,7 @@ GC_API void GC_CALL GC_free(void * p)
                 /* Its unnecessary to clear the mark bit.  If the       */
                 /* object is reallocated, it doesn't matter.  O.w. the  */
                 /* collector will do it, since it's on a free list.     */
-        if (ok -> ok_init) {
+        if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
             BZERO((word *)p + 1, sz-sizeof(word));
         }
         flh = &(ok -> ok_freelist[ngranules]);
@@ -590,7 +591,7 @@ GC_API void GC_CALL GC_free(void * p)
 
         GC_bytes_freed += sz;
         if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
-        if (ok -> ok_init) {
+        if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
             BZERO((word *)p + 1, sz-sizeof(word));
         }
         flh = &(ok -> ok_freelist[ngranules]);
index f7a680a..7b6f62c 100644 (file)
--- a/mallocx.c
+++ b/mallocx.c
@@ -542,7 +542,8 @@ GC_API GC_ATTR_MALLOC char * GC_CALL GC_strndup(const char *str, size_t size)
 #   endif
     return NULL;
   }
-  BCOPY(str, copy, len);
+  if (EXPECT(len > 0, TRUE))
+    BCOPY(str, copy, len);
   copy[len] = '\0';
   return copy;
 }
diff --git a/misc.c b/misc.c
index e833f37..4ba0955 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -584,7 +584,8 @@ GC_API void GC_CALL GC_get_heap_usage_safe(GC_word *pheap_size,
       memset((char *)pstats + sizeof(stats), 0xff, stats_sz - sizeof(stats));
       return sizeof(stats);
     } else {
-      BCOPY(&stats, pstats, stats_sz);
+      if (EXPECT(stats_sz > 0, TRUE))
+        BCOPY(&stats, pstats, stats_sz);
       return stats_sz;
     }
   }
@@ -604,8 +605,10 @@ GC_API void GC_CALL GC_get_heap_usage_safe(GC_word *pheap_size,
                  stats_sz - sizeof(stats));
         return sizeof(stats);
       } else {
-        fill_prof_stats(&stats);
-        BCOPY(&stats, pstats, stats_sz);
+        if (EXPECT(stats_sz > 0, TRUE)) {
+          fill_prof_stats(&stats);
+          BCOPY(&stats, pstats, stats_sz);
+        }
         return stats_sz;
       }
     }
index 62e4204..9454f55 100644 (file)
--- a/os_dep.c
+++ b/os_dep.c
@@ -4517,7 +4517,9 @@ GC_INNER void GC_save_callers(struct callinfo info[NFRAMES])
 # endif
   GC_STATIC_ASSERT(sizeof(struct callinfo) == sizeof(void *));
   npcs = backtrace((void **)tmp_info, NFRAMES + IGNORE_FRAMES);
-  BCOPY(tmp_info+IGNORE_FRAMES, info, (npcs - IGNORE_FRAMES) * sizeof(void *));
+  if (npcs > IGNORE_FRAMES)
+    BCOPY(&tmp_info[IGNORE_FRAMES], info,
+          (npcs - IGNORE_FRAMES) * sizeof(void *));
   for (i = npcs - IGNORE_FRAMES; i < NFRAMES; ++i) info[i].ci_pc = 0;
 # ifdef REDIRECT_MALLOC
     GC_in_save_callers = FALSE;
index 2361406..2f49121 100644 (file)
--- a/reclaim.c
+++ b/reclaim.c
@@ -82,10 +82,12 @@ GC_INNER void GC_print_all_errors(void)
     have_errors = GC_have_errors;
     printing_errors = TRUE;
     n_leaked = GC_n_leaked;
-    GC_ASSERT(n_leaked <= MAX_LEAKED);
-    BCOPY(GC_leaked, leaked, n_leaked * sizeof(ptr_t));
-    GC_n_leaked = 0;
-    BZERO(GC_leaked, n_leaked * sizeof(ptr_t));
+    if (n_leaked > 0) {
+      GC_ASSERT(n_leaked <= MAX_LEAKED);
+      BCOPY(GC_leaked, leaked, n_leaked * sizeof(ptr_t));
+      GC_n_leaked = 0;
+      BZERO(GC_leaked, n_leaked * sizeof(ptr_t));
+    }
     UNLOCK();
 
     if (GC_debugging_started) {