From 3dbb260e5f277a350da7251e01dd3284f973f36f Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Fri, 20 Jan 2023 09:37:48 +0300 Subject: Simplify code of GC_allochblk_nth (refactoring) Issue #510 (bdwgc). * allchblk.c (GC_hblk_fl_from_blocks): Change type of blocks argument from word to size_t. * allchblk.c (setup_header): Reformat comment. * allchblk.c (GC_get_first_part): Change type of total_size local variable from word to size_t; add assertion that bytes argument is multiple of HBLKSIZE. * allchblk.c (GC_get_first_part): Specify that result of GC_install_header() is unlikely to be NULL. * alloc.c (GC_add_to_heap): Likewise. * allchblk.c (AVOID_SPLIT_REMAPPED): Define only if USE_MUNMAP. * allchblk.c (GC_allochblk): Remove comment; change blocks*HBLKSIZE<0 to blocks>=GC_SIZE_MAX/(2*HBLKSIZE) and expect its result unlikely to be true. * allchblk.c (GC_allochblk): Vhange type of blocks local variable from word to size_t. * malloc.c (GC_alloc_large): Likewise. * allchblk.c (GC_drop_blacklisted_count): New STATIC varible (replaces\ count static one in GC_allochblk_nth). * allchblk.c (next_hblk_fits_better, find_nonbl_hblk, drop_hblk_in_chunks): New static function (move code GC_allochblk_nth). * allchblk.c (GC_allochblk_nth): Refine comment; add assertion that sz is non-zero; change type of size_needed and size_avail from signed_word to word; add and refine comments; call find_nonbl_hblk, next_hblk_fits_better and drop_hblk_in_chunks; simplify code in the loop; rename lasthbp to last_hbp local variable; rename thishdr to last_hdr local variable; remove thishbp local variable; add assertion that hhdr->hb_sz>=size_needed. * include/private/gc_priv.h (GC_allochblk): Move comment from allchblk.c. * include/private/gc_priv.h (GC_alloc_large) Move comment from malloc.c. * malloc.c (GC_alloc_large): Remove total_bytes local variable. * malloc.c (GC_alloc_large, GC_alloc_large_and_clear, GC_generic_malloc): Specify that result (or h) is unlikely to be NULL. --- malloc.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'malloc.c') diff --git a/malloc.c b/malloc.c index dc72a62d..e0643790 100644 --- a/malloc.c +++ b/malloc.c @@ -33,14 +33,11 @@ STATIC GC_bool GC_alloc_reclaim_list(struct obj_kind *kind) return TRUE; } -/* Allocate a large block of size lb bytes. The block is not cleared. */ -/* flags argument should be 0 or IGNORE_OFF_PAGE. EXTRA_BYTES value */ -/* was already added to lb. */ GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags) { struct hblk * h; - word n_blocks; - ptr_t result; + size_t n_blocks; + ptr_t result = NULL; GC_bool retry = FALSE; GC_ASSERT(I_HOLD_LOCK()); @@ -52,15 +49,16 @@ GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags) GC_init(); LOCK(); } - /* Do our share of marking work */ - if (GC_incremental && !GC_dont_gc) { + /* Do our share of marking work. */ + if (GC_incremental && !GC_dont_gc) { ENTER_GC(); GC_collect_a_little_inner((int)n_blocks); EXIT_GC(); - } + } + h = GC_allochblk(lb, k, flags); # ifdef USE_MUNMAP - if (0 == h) { + if (NULL == h) { GC_merge_unmapped(); h = GC_allochblk(lb, k, flags); } @@ -69,12 +67,9 @@ GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags) h = GC_allochblk(lb, k, flags); retry = TRUE; } - if (h == 0) { - result = 0; - } else { - size_t total_bytes = n_blocks * HBLKSIZE; + if (EXPECT(h != NULL, TRUE)) { if (n_blocks > 1) { - GC_large_allocd_bytes += total_bytes; + GC_large_allocd_bytes += HBLKSIZE * n_blocks; if (GC_large_allocd_bytes > GC_max_large_allocd_bytes) GC_max_large_allocd_bytes = GC_large_allocd_bytes; } @@ -92,7 +87,7 @@ STATIC ptr_t GC_alloc_large_and_clear(size_t lb, int k, unsigned flags) GC_ASSERT(I_HOLD_LOCK()); result = GC_alloc_large(lb, k, flags); - if (result != NULL + if (EXPECT(result != NULL, TRUE) && (GC_debugging_started || GC_obj_kinds[k].ok_init)) { /* Clear the whole block, in case of GC_realloc call. */ BZERO(result, HBLKSIZE * OBJ_SZ_TO_BLOCKS(lb)); @@ -263,7 +258,7 @@ GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k) init = GC_obj_kinds[k].ok_init; LOCK(); result = (ptr_t)GC_alloc_large(lb_rounded, k, 0); - if (0 != result) { + if (EXPECT(result != NULL, TRUE)) { if (GC_debugging_started) { BZERO(result, HBLKSIZE * OBJ_SZ_TO_BLOCKS(lb_rounded)); } else { -- cgit v1.2.1