From: Ivan Maidanski Date: Wed, 9 Aug 2017 08:10:38 +0000 (+0300) Subject: Do not call BCOPY and BZERO if size is zero X-Git-Tag: v8.0.0~613 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c4f8f000273f029e9aebc8eb305a21975c853786;p=gc Do not call BCOPY and BZERO if size is zero * 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. --- diff --git a/dbg_mlc.c b/dbg_mlc.c index 724d9cff..4edeeefd 100644 --- 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); diff --git a/finalize.c b/finalize.c index bd297a9c..ad7e03b5 100644 --- a/finalize.c +++ b/finalize.c @@ -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; } diff --git a/malloc.c b/malloc.c index 502b3e3b..dbffee27 100644 --- 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]); diff --git a/mallocx.c b/mallocx.c index f7a680a1..7b6f62cf 100644 --- 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 e833f37e..4ba09553 100644 --- 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; } } diff --git a/os_dep.c b/os_dep.c index 62e42040..9454f558 100644 --- 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; diff --git a/reclaim.c b/reclaim.c index 2361406e..2f491218 100644 --- 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) {