From 089667c19cde23eb5988778411ff28e186465d16 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Thu, 29 Dec 2011 14:04:58 +0400 Subject: [PATCH] Use EXPECT for checking various 'initialized' boolean variables * alloc.c (GC_try_to_collect_general, GC_expand_hp): Use "expect true" for GC_is_initialized value. * gcj_mlc.c (maybe_finalize): Likewise. * malloc.c (GC_alloc_large, GC_generic_malloc_inner, malloc): Likewise. * mallocx.c (GC_generic_malloc_many): Likewise. * mark_rts.c (GC_add_roots, GC_clear_roots): Likewise. * misc.c (GC_base, GC_init): Likewise. * ptr_chck.c (GC_same_obj, GC_is_valid_displacement, GC_is_visible): Likewise. * darwin_stop_world.c (GC_push_all_stacks): Use "expect true" for GC_thr_initialized value. * pthread_stop_world.c (GC_push_all_stacks): Likewise. * pthread_support.c (pthread_create): Likewise. * dyn_load.c (GC_register_dynamic_libraries_dl_iterate_phdr): Use "expect true" for excluded_segs value. * include/private/gc_priv.h (COND_DUMP): Use "expect false" for GC_dump_regularly value. * malloc.c (GC_generic_malloc): Use "expect false" for GC_have_errors value. * mallocx.c (GC_generic_malloc_ignore_off_page, GC_generic_malloc_many): Likewise. * malloc.c (calloc): Use "expect true" for lib_bounds_set value. * os_dep.c (GC_unix_mmap_get_mem): Use "expect true" for "initialized" value. * pthread_stop_world.c (GC_nacl_initialize_gc_thread): Use "expect true" for GC_nacl_thread_parking_inited value. * pthread_support.c (INIT_REAL_SYMS): Use "expect true" for GC_syms_initialized value; remove redundant trailing ';'. * pthread_support.c (GC_new_thread): Use "expect true" for first_thread_used value. * win32_threads.c (GC_new_thread): Likewise. * pthread_support.c (pthread_create): Use "expect true" for parallel_initialized value. * win32_threads.c (GC_CreateThread, GC_beginthreadex, GC_pthread_join, GC_pthread_create, GC_pthread_sigmask, GC_pthread_detach): Likewise. * pthread_support.c (pthread_create): Use "expect false" for "si" value to be NULL. * thread_local_alloc.c (GC_init_thread_local): Use "expect true" for keys_initialized value. * typd_mlc.c (GC_make_descriptor): Use "expect true" for GC_explicit_typing_initialized value. --- alloc.c | 4 ++-- darwin_stop_world.c | 2 +- dyn_load.c | 2 +- gcj_mlc.c | 4 ++-- include/private/gc_priv.h | 2 +- malloc.c | 11 ++++++----- mallocx.c | 8 +++++--- mark_rts.c | 4 ++-- misc.c | 4 ++-- os_dep.c | 2 +- pthread_stop_world.c | 5 +++-- pthread_support.c | 13 ++++++++----- ptr_chck.c | 6 +++--- thread_local_alloc.c | 2 +- typd_mlc.c | 4 +++- win32_threads.c | 20 +++++++++++++------- 16 files changed, 54 insertions(+), 39 deletions(-) diff --git a/alloc.c b/alloc.c index 609fae0..e182af2 100644 --- a/alloc.c +++ b/alloc.c @@ -936,7 +936,7 @@ STATIC GC_bool GC_try_to_collect_general(GC_stop_func stop_func, IF_CANCEL(int cancel_state;) DCL_LOCK_STATE; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); if (GC_debugging_started) GC_print_all_smashed(); GC_INVOKE_FINALIZERS(); LOCK(); @@ -1183,7 +1183,7 @@ GC_API int GC_CALL GC_expand_hp(size_t bytes) DCL_LOCK_STATE; LOCK(); - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes)); if (result) GC_requested_heapsize += bytes; UNLOCK(); diff --git a/darwin_stop_world.c b/darwin_stop_world.c index f05dc68..16e24a9 100644 --- a/darwin_stop_world.c +++ b/darwin_stop_world.c @@ -271,7 +271,7 @@ GC_INNER void GC_push_all_stacks(void) int nthreads = 0; word total_size = 0; mach_msg_type_number_t listcount = (mach_msg_type_number_t)THREAD_TABLE_SZ; - if (!GC_thr_initialized) + if (!EXPECT(GC_thr_initialized, TRUE)) GC_thr_init(); # ifndef DARWIN_DONT_PARSE_STACK diff --git a/dyn_load.c b/dyn_load.c index 8532d28..80dfbc8 100644 --- a/dyn_load.c +++ b/dyn_load.c @@ -532,7 +532,7 @@ STATIC GC_bool GC_register_dynamic_libraries_dl_iterate_phdr(void) { static GC_bool excluded_segs = FALSE; n_load_segs = 0; - if (!excluded_segs) { + if (!EXPECT(excluded_segs, TRUE)) { GC_exclude_static_roots_inner((ptr_t)load_segs, (ptr_t)load_segs + sizeof(load_segs)); excluded_segs = TRUE; diff --git a/gcj_mlc.c b/gcj_mlc.c index 7b189b5..a7fefd9 100644 --- a/gcj_mlc.c +++ b/gcj_mlc.c @@ -144,8 +144,8 @@ static void maybe_finalize(void) static word last_finalized_no = 0; DCL_LOCK_STATE; - if (GC_gc_no == last_finalized_no) return; - if (!GC_is_initialized) return; + if (GC_gc_no == last_finalized_no || + !EXPECT(GC_is_initialized, TRUE)) return; UNLOCK(); GC_INVOKE_FINALIZERS(); LOCK(); diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index 1770304..d28d807 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -1855,7 +1855,7 @@ GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */ #ifndef NO_DEBUGGING GC_EXTERN GC_bool GC_dump_regularly; /* Generate regular debugging dumps. */ -# define COND_DUMP if (GC_dump_regularly) GC_dump() +# define COND_DUMP if (EXPECT(GC_dump_regularly, FALSE)) GC_dump() #else # define COND_DUMP /* empty */ #endif diff --git a/malloc.c b/malloc.c index aeda693..75c9e0e 100644 --- a/malloc.c +++ b/malloc.c @@ -49,7 +49,7 @@ GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags) /* Round up to a multiple of a granule. */ lb = (lb + GRANULE_BYTES - 1) & ~(GRANULE_BYTES - 1); n_blocks = OBJ_SZ_TO_BLOCKS(lb); - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); /* Do our share of marking work */ if (GC_incremental && !GC_dont_gc) GC_collect_a_little_inner((int)n_blocks); @@ -111,7 +111,7 @@ GC_INNER void * GC_generic_malloc_inner(size_t lb, int k) if( (op = *opp) == 0 ) { if (GC_size_map[lb] == 0) { - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); if (GC_size_map[lb] == 0) GC_extend_size_map(lb); return(GC_generic_malloc_inner(lb, k)); } @@ -154,7 +154,8 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) void * result; DCL_LOCK_STATE; - if (GC_have_errors) GC_print_all_errors(); + if (EXPECT(GC_have_errors, FALSE)) + GC_print_all_errors(); GC_INVOKE_FINALIZERS(); if (SMALL_OBJ(lb)) { LOCK(); @@ -340,7 +341,7 @@ void * malloc(size_t lb) * The thread implementation may well call malloc at other * inopportune times. */ - if (!GC_is_initialized) return sbrk(lb); + if (!EXPECT(GC_is_initialized, TRUE)) return sbrk(lb); # endif /* I386 && GC_SOLARIS_THREADS */ return((void *)REDIRECT_MALLOC(lb)); } @@ -379,7 +380,7 @@ void * calloc(size_t n, size_t lb) ptr_t caller = (ptr_t)__builtin_return_address(0); /* This test does not need to ensure memory visibility, since */ /* the bounds will be set when/if we create another thread. */ - if (!lib_bounds_set) { + if (!EXPECT(lib_bounds_set, TRUE)) { GC_init_lib_bounds(); lib_bounds_set = TRUE; } diff --git a/mallocx.c b/mallocx.c index e592835..651cbad 100644 --- a/mallocx.c +++ b/mallocx.c @@ -184,7 +184,8 @@ GC_INNER void * GC_generic_malloc_ignore_off_page(size_t lb, int k) lb_rounded = GRANULES_TO_BYTES(lg); n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded); init = GC_obj_kinds[k].ok_init; - if (GC_have_errors) GC_print_all_errors(); + if (EXPECT(GC_have_errors, FALSE)) + GC_print_all_errors(); GC_INVOKE_FINALIZERS(); LOCK(); result = (ptr_t)GC_alloc_large(ADD_SLOP(lb), k, IGNORE_OFF_PAGE); @@ -286,10 +287,11 @@ GC_API void GC_CALL GC_generic_malloc_many(size_t lb, int k, void **result) } lw = BYTES_TO_WORDS(lb); lg = BYTES_TO_GRANULES(lb); - if (GC_have_errors) GC_print_all_errors(); + if (EXPECT(GC_have_errors, FALSE)) + GC_print_all_errors(); GC_INVOKE_FINALIZERS(); LOCK(); - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); /* Do our share of marking work */ if (GC_incremental && !GC_dont_gc) { ENTER_GC(); diff --git a/mark_rts.c b/mark_rts.c index 5afdab5..b7b8132 100644 --- a/mark_rts.c +++ b/mark_rts.c @@ -139,7 +139,7 @@ GC_API void GC_CALL GC_add_roots(void *b, void *e) { DCL_LOCK_STATE; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); LOCK(); GC_add_roots_inner((ptr_t)b, (ptr_t)e, FALSE); UNLOCK(); @@ -245,7 +245,7 @@ GC_API void GC_CALL GC_clear_roots(void) { DCL_LOCK_STATE; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); LOCK(); roots_were_cleared = TRUE; n_root_sets = 0; diff --git a/misc.c b/misc.c index 27f264a..18cc44d 100644 --- a/misc.c +++ b/misc.c @@ -361,7 +361,7 @@ GC_API void * GC_CALL GC_base(void * p) ptr_t limit; r = p; - if (!GC_is_initialized) return 0; + if (!EXPECT(GC_is_initialized, TRUE)) return 0; h = HBLKPTR(r); GET_BI(r, bi); candidate_hdr = HDR_FROM_BI(bi, r); @@ -659,7 +659,7 @@ GC_API void GC_CALL GC_init(void) word initial_heap_sz; IF_CANCEL(int cancel_state;) - if (GC_is_initialized) return; + if (EXPECT(GC_is_initialized, TRUE)) return; # ifdef REDIRECT_MALLOC { static GC_bool init_started = FALSE; diff --git a/os_dep.c b/os_dep.c index 97d84b0..a3f7800 100644 --- a/os_dep.c +++ b/os_dep.c @@ -2040,7 +2040,7 @@ STATIC ptr_t GC_unix_mmap_get_mem(word bytes) # ifndef USE_MMAP_ANON static GC_bool initialized = FALSE; - if (!initialized) { + if (!EXPECT(initialized, TRUE)) { zero_fd = open("/dev/zero", O_RDONLY); fcntl(zero_fd, F_SETFD, FD_CLOEXEC); initialized = TRUE; diff --git a/pthread_stop_world.c b/pthread_stop_world.c index a354a84..af7f9b1 100644 --- a/pthread_stop_world.c +++ b/pthread_stop_world.c @@ -312,7 +312,8 @@ GC_INNER void GC_push_all_stacks(void) pthread_t self = pthread_self(); word total_size = 0; - if (!GC_thr_initialized) GC_thr_init(); + if (!EXPECT(GC_thr_initialized, TRUE)) + GC_thr_init(); # ifdef DEBUG_THREADS GC_log_printf("Pushing stacks from thread 0x%x\n", (unsigned)self); # endif @@ -693,7 +694,7 @@ GC_INNER void GC_stop_world(void) { int i; pthread_mutex_lock(&GC_nacl_thread_alloc_lock); - if (!GC_nacl_thread_parking_inited) { + if (!EXPECT(GC_nacl_thread_parking_inited, TRUE)) { BZERO(GC_nacl_thread_parked, sizeof(GC_nacl_thread_parked)); BZERO(GC_nacl_thread_used, sizeof(GC_nacl_thread_used)); GC_nacl_thread_parking_inited = TRUE; diff --git a/pthread_support.c b/pthread_support.c index da53b5c..c7df374 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -275,7 +275,8 @@ GC_INNER unsigned long GC_lock_holder = NO_THREAD; GC_syms_initialized = TRUE; } -# define INIT_REAL_SYMS() if (!GC_syms_initialized) GC_init_real_syms(); +# define INIT_REAL_SYMS() if (!EXPECT(GC_syms_initialized, TRUE)) \ + GC_init_real_syms() #else # define INIT_REAL_SYMS() #endif @@ -475,7 +476,7 @@ STATIC GC_thread GC_new_thread(pthread_t id) static GC_bool first_thread_used = FALSE; GC_ASSERT(I_HOLD_LOCK()); - if (!first_thread_used) { + if (!EXPECT(first_thread_used, TRUE)) { result = &first_thread; first_thread_used = TRUE; } else { @@ -1511,8 +1512,9 @@ GC_API int WRAP_FUNC(pthread_create)(pthread_t *new_thread, si = (struct start_info *)GC_INTERNAL_MALLOC(sizeof(struct start_info), NORMAL); UNLOCK(); - if (!parallel_initialized) GC_init_parallel(); - if (0 == si && + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); + if (EXPECT(0 == si, FALSE) && (si = (struct start_info *) (*GC_get_oom_fn())(sizeof(struct start_info))) == 0) return(ENOMEM); @@ -1522,7 +1524,8 @@ GC_API int WRAP_FUNC(pthread_create)(pthread_t *new_thread, si -> start_routine = start_routine; si -> arg = arg; LOCK(); - if (!GC_thr_initialized) GC_thr_init(); + if (!EXPECT(GC_thr_initialized, TRUE)) + GC_thr_init(); # ifdef GC_ASSERTIONS { size_t stack_size = 0; diff --git a/ptr_chck.c b/ptr_chck.c index 5b3cc0a..a476633 100644 --- a/ptr_chck.c +++ b/ptr_chck.c @@ -43,7 +43,7 @@ GC_API void * GC_CALL GC_same_obj(void *p, void *q) ptr_t base, limit; word sz; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); hhdr = HDR((word)p); if (hhdr == 0) { if (divHBLKSZ((word)p) != divHBLKSZ((word)q) @@ -122,7 +122,7 @@ GC_API void * GC_CALL GC_is_valid_displacement(void *p) struct hblk *h; word sz; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); hhdr = HDR((word)p); if (hhdr == 0) return(p); h = HBLKPTR(p); @@ -189,7 +189,7 @@ GC_API void * GC_CALL GC_is_visible(void *p) hdr *hhdr; if ((word)p & (ALIGNMENT - 1)) goto fail; - if (!GC_is_initialized) GC_init(); + if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); # ifdef THREADS hhdr = HDR((word)p); if (hhdr != 0 && GC_base(p) == 0) { diff --git a/thread_local_alloc.c b/thread_local_alloc.c index 8321167..82523cd 100644 --- a/thread_local_alloc.c +++ b/thread_local_alloc.c @@ -89,7 +89,7 @@ GC_INNER void GC_init_thread_local(GC_tlfs p) int i; GC_ASSERT(I_HOLD_LOCK()); - if (!keys_initialized) { + if (!EXPECT(keys_initialized, TRUE)) { if (0 != GC_key_create(&GC_thread_key, 0)) { ABORT("Failed to create key for local allocator"); } diff --git a/typd_mlc.c b/typd_mlc.c index 1cf9f28..e94171b 100644 --- a/typd_mlc.c +++ b/typd_mlc.c @@ -539,7 +539,9 @@ GC_API GC_descr GC_CALL GC_make_descriptor(GC_bitmap bm, size_t len) signed_word i; # define HIGH_BIT (((word)1) << (WORDSZ - 1)) - if (!GC_explicit_typing_initialized) GC_init_explicit_typing(); + if (!EXPECT(GC_explicit_typing_initialized, TRUE)) + GC_init_explicit_typing(); + while (last_set_bit >= 0 && !GC_get_bit(bm, last_set_bit)) last_set_bit--; if (last_set_bit < 0) return(0 /* no pointers */); diff --git a/win32_threads.c b/win32_threads.c index 414dcc5..859db67 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -330,7 +330,7 @@ STATIC GC_thread GC_new_thread(DWORD id) GC_thread result; GC_ASSERT(I_HOLD_LOCK()); - if (!first_thread_used) { + if (!EXPECT(first_thread_used, TRUE)) { result = &first_thread; first_thread_used = TRUE; } else { @@ -2061,7 +2061,8 @@ GC_INNER void GC_get_next_stack(char *start, char *limit, HANDLE thread_h; thread_args *args; - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); /* make sure GC is initialized (i.e. main thread is */ /* attached, tls initialized). */ @@ -2109,7 +2110,8 @@ GC_INNER void GC_get_next_stack(char *start, char *limit, GC_uintptr_t thread_h; thread_args *args; - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); /* make sure GC is initialized (i.e. main thread is */ /* attached, tls initialized). */ # ifdef DEBUG_THREADS @@ -2365,7 +2367,8 @@ GC_INNER void GC_thr_init(void) (long)GetCurrentThreadId(), GC_PTHREAD_PTRVAL(pthread_id)); # endif - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); /* Thread being joined might not have registered itself yet. */ /* After the join,thread id may have been recycled. */ @@ -2405,7 +2408,8 @@ GC_INNER void GC_thr_init(void) GC_PTHREAD_CREATE_CONST pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); /* make sure GC is initialized (i.e. main thread is attached) */ if (GC_win32_dll_threads) { return pthread_create(new_thread, attr, start_routine, arg); @@ -2525,7 +2529,8 @@ GC_INNER void GC_thr_init(void) GC_API int GC_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); return pthread_sigmask(how, set, oset); } # endif /* !GC_NO_PTHREAD_SIGMASK */ @@ -2536,7 +2541,8 @@ GC_INNER void GC_thr_init(void) GC_thread t; DCL_LOCK_STATE; - if (!parallel_initialized) GC_init_parallel(); + if (!EXPECT(parallel_initialized, TRUE)) + GC_init_parallel(); LOCK(); t = GC_lookup_pthread(thread); UNLOCK(); -- 2.7.4