From f99635f4f07e1bf0f7e5108c9c63de672fdcc9c9 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 28 Oct 2024 19:57:57 +0300 Subject: [PATCH] Define internal macro for a number-to-pointer conversion (refactoring) Issue #627 (bdwgc). Convert an unsigned value to a void pointer. Typically used to print a numeric value using "%p" format specifier. The pointer is not supposed to be dereferenced. * headers.c (GC_install_counts): Use NUMERIC_TO_VPTR(). * include/private/dbg_mlc.h [KEEP_BACK_PTRS || PRINT_BLACK_LIST] (MARKED_FOR_FINALIZATION, MARKED_FROM_REGISTER, NOT_MARKED): Likewise. * misc.c [E2K] (GC_call_with_stack_base): Likewise. * pthread_stop_world.c [NACL] (GC_suspend_all): Likewise. * pthread_support.c [PARALLEL_MARK && HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG] (set_marker_thread_name): Likewise. * pthread_support.c [GC_PTHREADS_PARAMARK] (GC_start_mark_threads_inner): Likewise. * pthread_support.c [E2K] (GC_get_my_stackbottom): Likewise. * reclaim.c (GC_start_reclaim): Likewise. * tests/gctest.c (INT_TO_SEXPR, reverse_test_inner, typed_test): Likewise. * thread_local_alloc.c (return_freelists, GC_init_thread_local): Likewise. * win32_threads.c [PARALLEL_MARK && !GC_PTHREADS_PARAMARK] (GC_start_mark_threads_inner): Likewise. * win32_threads.c (GC_win32_start_inner): Likewise. * win32_threads.c [GC_WINMAIN_REDIRECT] (GC_waitForSingleObjectInfinite): Likewise. * include/private/dbg_mlc.h [CPP_WORDSZ!=32] (GC_FREED_MEM_MARKER, START_FLAG, END_FLAG): Add outermost parentheses. * include/private/gc_priv.h (NUMERIC_TO_VPTR): New macro. * pthread_support.c [E2K] (GC_record_stack_base, GC_set_stackbottom): Remove redundant parentheses. * win32_threads.c [CPPCHECK] (GC_use_threads_discovery): Add extra parentheses. --- headers.c | 2 +- include/private/dbg_mlc.h | 12 ++++++------ include/private/gc_priv.h | 5 +++++ misc.c | 2 +- pthread_stop_world.c | 2 +- pthread_support.c | 10 +++++----- reclaim.c | 2 +- tests/gctest.c | 21 +++++++++++---------- thread_local_alloc.c | 6 +++--- win32_threads.c | 10 +++++----- 10 files changed, 39 insertions(+), 33 deletions(-) diff --git a/headers.c b/headers.c index ea4ded80c..c2a0793b3 100644 --- a/headers.c +++ b/headers.c @@ -306,7 +306,7 @@ GC_install_counts(struct hblk *h, size_t sz /* bytes */) for (hbp = h + 1; ADDR_LT((ptr_t)hbp, (ptr_t)h + sz); hbp++) { word i = (word)HBLK_PTR_DIFF(hbp, h); - SET_HDR(hbp, (hdr *)(i > MAX_JUMP ? MAX_JUMP : i)); + SET_HDR(hbp, (hdr *)NUMERIC_TO_VPTR(i > MAX_JUMP ? MAX_JUMP : i)); } return TRUE; } diff --git a/include/private/dbg_mlc.h b/include/private/dbg_mlc.h index 31a41a4b5..5eb52db24 100644 --- a/include/private/dbg_mlc.h +++ b/include/private/dbg_mlc.h @@ -36,7 +36,7 @@ EXTERN_C_BEGIN # if CPP_WORDSZ == 32 # define GC_FREED_MEM_MARKER (GC_uintptr_t)0xdeadbeef # else -# define GC_FREED_MEM_MARKER (GC_uintptr_t) GC_WORD_C(0xEFBEADDEdeadbeef) +# define GC_FREED_MEM_MARKER ((GC_uintptr_t)GC_WORD_C(0xEFBEADDEdeadbeef)) # endif #endif /* !GC_FREED_MEM_MARKER */ @@ -46,8 +46,8 @@ EXTERN_C_BEGIN # define START_FLAG (GC_uintptr_t)0xfedcedcb # define END_FLAG (GC_uintptr_t)0xbcdecdef #else -# define START_FLAG (GC_uintptr_t) GC_WORD_C(0xFEDCEDCBfedcedcb) -# define END_FLAG (GC_uintptr_t) GC_WORD_C(0xBCDECDEFbcdecdef) +# define START_FLAG ((GC_uintptr_t)GC_WORD_C(0xFEDCEDCBfedcedcb)) +# define END_FLAG ((GC_uintptr_t)GC_WORD_C(0xBCDECDEFbcdecdef)) #endif #if defined(KEEP_BACK_PTRS) || defined(PRINT_BLACK_LIST) @@ -56,13 +56,13 @@ EXTERN_C_BEGIN /* argument to some marking functions. */ /* Object was marked because it is finalizable. */ -# define MARKED_FOR_FINALIZATION ((ptr_t)(GC_uintptr_t)2) +# define MARKED_FOR_FINALIZATION ((ptr_t)NUMERIC_TO_VPTR(2)) /* Object was marked from a register. Hence the */ /* source of the reference doesn't have an address. */ -# define MARKED_FROM_REGISTER ((ptr_t)(GC_uintptr_t)4) +# define MARKED_FROM_REGISTER ((ptr_t)NUMERIC_TO_VPTR(4)) -# define NOT_MARKED ((ptr_t)(GC_uintptr_t)8) +# define NOT_MARKED ((ptr_t)NUMERIC_TO_VPTR(8)) #endif /* KEEP_BACK_PTRS || PRINT_BLACK_LIST */ /* Object debug header. The size of the structure is assumed */ diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index e21f1af8f..a15d70852 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -324,6 +324,11 @@ typedef struct hblkhdr hdr; #define CAST_AWAY_VOLATILE_PVOID(p) \ CAST_THRU_UINTPTR(/* no volatile */ void *, p) +/* Convert an unsigned value to a void pointer. Typically used to */ +/* print a numeric value using "%p" format specifier. The pointer */ +/* is not supposed to be dereferenced. */ +#define NUMERIC_TO_VPTR(v) ((void *)(word)(v)) + /* Create a ptr_t pointer from a number (of word type). */ #define MAKE_CPTR(w) ((ptr_t)(word)(w)) diff --git a/misc.c b/misc.c index 9aa839b71..edabc5c9b 100644 --- a/misc.c +++ b/misc.c @@ -2406,7 +2406,7 @@ GC_call_with_stack_base(GC_stack_base_func fn, void *arg) unsigned long long sz_ull; GET_PROCEDURE_STACK_SIZE_INNER(&sz_ull); - base.reg_base = (void *)(word)sz_ull; + base.reg_base = NUMERIC_TO_VPTR(sz_ull); } #endif result = (*(GC_stack_base_func volatile *)&fn)(&base, arg); diff --git a/pthread_stop_world.c b/pthread_stop_world.c index ff7d2b529..52ccc2a8f 100644 --- a/pthread_stop_world.c +++ b/pthread_stop_world.c @@ -1034,7 +1034,7 @@ GC_suspend_all(void) if (GC_nacl_thread_parked[i] == 1) { num_threads_parked++; if (GC_on_thread_event) - GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, (void *)(word)i); + GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, NUMERIC_TO_VPTR(i)); } } } diff --git a/pthread_support.c b/pthread_support.c index 4dd839bdb..e6e81d0c5 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -378,7 +378,7 @@ static void set_marker_thread_name(unsigned id) { int err = pthread_setname_np(pthread_self(), "GC-marker-%zu", - (void *)(size_t)id); + NUMERIC_TO_VPTR(id)); if (EXPECT(err != 0, FALSE)) WARN("pthread_setname_np failed, errno= %" WARN_PRIdPTR "\n", (signed_word)err); @@ -637,7 +637,7 @@ GC_start_mark_threads_inner(void) GC_marker_last_stack_min[i] = ADDR_LIMIT; # endif if (EXPECT(REAL_FUNC(pthread_create)(&new_thread, &attr, GC_mark_thread, - (void *)(word)i) + NUMERIC_TO_VPTR(i)) != 0, FALSE)) { WARN("Marker thread %" WARN_PRIdPTR " creation failed\n", @@ -1649,7 +1649,7 @@ GC_record_stack_base(GC_stack_context_t crtn, const struct GC_stack_base *sb) if ((crtn->stack_end = (ptr_t)sb->mem_base) == NULL) ABORT("Bad stack base in GC_register_my_thread"); # ifdef E2K - crtn->ps_ofs = (size_t)((GC_uintptr_t)sb->reg_base); + crtn->ps_ofs = (size_t)(GC_uintptr_t)sb->reg_base; # elif defined(IA64) crtn->backing_store_end = (ptr_t)sb->reg_base; # elif defined(I386) && defined(GC_WIN32_THREADS) @@ -2105,7 +2105,7 @@ GC_set_stackbottom(void *gc_thread_handle, const struct GC_stack_base *sb) crtn->stack_end = (ptr_t)sb->mem_base; # ifdef E2K - crtn->ps_ofs = (size_t)((GC_uintptr_t)sb->reg_base); + crtn->ps_ofs = (size_t)(GC_uintptr_t)sb->reg_base; # elif defined(IA64) crtn->backing_store_end = (ptr_t)sb->reg_base; # endif @@ -2128,7 +2128,7 @@ GC_get_my_stackbottom(struct GC_stack_base *sb) sb->mem_base = crtn->stack_end; # ifdef E2K /* Store the offset in the procedure stack, not address. */ - sb->reg_base = (void *)((GC_uintptr_t)crtn->ps_ofs); + sb->reg_base = NUMERIC_TO_VPTR(crtn->ps_ofs); # elif defined(IA64) sb->reg_base = crtn->backing_store_end; # endif diff --git a/reclaim.c b/reclaim.c index 6f72268f7..86b48b274 100644 --- a/reclaim.c +++ b/reclaim.c @@ -731,7 +731,7 @@ GC_start_reclaim(GC_bool report_if_found) /* Go through all heap blocks (in hblklist) and reclaim unmarked */ /* objects or enqueue the block for later processing. */ - GC_apply_to_all_blocks(GC_reclaim_block, (void *)(word)report_if_found); + GC_apply_to_all_blocks(GC_reclaim_block, NUMERIC_TO_VPTR(report_if_found)); #ifdef EAGER_SWEEP /* This is a very stupid thing to do. We make it possible anyway, */ diff --git a/tests/gctest.c b/tests/gctest.c index 24fa6f696..881b06b20 100644 --- a/tests/gctest.c +++ b/tests/gctest.c @@ -251,7 +251,7 @@ struct SEXPR { typedef struct SEXPR *sexpr; -#define INT_TO_SEXPR(v) ((sexpr)(GC_uintptr_t)(unsigned)(v)) +#define INT_TO_SEXPR(v) ((sexpr)NUMERIC_TO_VPTR(v)) #define SEXPR_TO_INT(p) ((int)(GC_word)(p)) #undef nil @@ -866,7 +866,7 @@ reverse_test_inner(void *data) if (data == 0) { /* This stack frame is not guaranteed to be scanned. */ - return GC_call_with_gc_active(reverse_test_inner, (void *)(GC_uintptr_t)1); + return GC_call_with_gc_active(reverse_test_inner, NUMERIC_TO_VPTR(1)); } #if defined(CPPCHECK) @@ -1101,7 +1101,7 @@ mktree(int n) #ifndef GC_NO_FINALIZATION if (!GC_get_find_leak()) { - GC_REGISTER_FINALIZER(result, finalizer, (void *)(GC_uintptr_t)n, + GC_REGISTER_FINALIZER(result, finalizer, NUMERIC_TO_VPTR(n), (GC_finalization_proc *)0, (void **)0); if (my_index >= MAX_FINALIZED) { GC_printf("live_indicators overflowed\n"); @@ -1413,25 +1413,25 @@ typed_test(void) GC_printf("Bad initialization by GC_malloc_explicitly_typed\n"); FAIL; } - newP[0] = (void *)(GC_uintptr_t)17; + newP[0] = NUMERIC_TO_VPTR(17); GC_PTR_STORE_AND_DIRTY(newP + 1, old); old = newP; AO_fetch_and_add1(&collectable_count); newP = (void **)GC_MALLOC_EXPLICITLY_TYPED(4 * sizeof(void *), d2); CHECK_OUT_OF_MEMORY(newP); - newP[0] = (void *)(GC_uintptr_t)17; + newP[0] = NUMERIC_TO_VPTR(17); GC_PTR_STORE_AND_DIRTY(newP + 1, old); old = newP; AO_fetch_and_add1(&collectable_count); newP = (void **)GC_MALLOC_EXPLICITLY_TYPED(33 * sizeof(void *), d3); CHECK_OUT_OF_MEMORY(newP); - newP[0] = (void *)(GC_uintptr_t)17; + newP[0] = NUMERIC_TO_VPTR(17); GC_PTR_STORE_AND_DIRTY(newP + 1, old); old = newP; AO_fetch_and_add1(&collectable_count); newP = (void **)GC_CALLOC_EXPLICITLY_TYPED(4, 2 * sizeof(void *), d1); CHECK_OUT_OF_MEMORY(newP); - newP[0] = (void *)(GC_uintptr_t)17; + newP[0] = NUMERIC_TO_VPTR(17); GC_PTR_STORE_AND_DIRTY(newP + 1, old); old = newP; AO_fetch_and_add1(&collectable_count); @@ -1449,7 +1449,7 @@ typed_test(void) } } CHECK_OUT_OF_MEMORY(newP); - newP[0] = (void *)(GC_uintptr_t)17; + newP[0] = NUMERIC_TO_VPTR(17); GC_PTR_STORE_AND_DIRTY(newP + 1, old); old = newP; } @@ -1511,8 +1511,9 @@ uniq(void *p, ...) int n = 0, i, j; q[n++] = p; va_start(a, p); - for (; (q[n] = va_arg(a, void *)) != NULL; n++) - ; + for (; (q[n] = va_arg(a, void *)) != NULL; n++) { + /* Empty. */ + } va_end(a); for (i = 0; i < n; i++) for (j = 0; j < i; j++) diff --git a/thread_local_alloc.c b/thread_local_alloc.c index 07f3166fb..0840fe6a7 100644 --- a/thread_local_alloc.c +++ b/thread_local_alloc.c @@ -66,7 +66,7 @@ return_freelists(void **fl, void **gfl) } /* Clear fl[i], since the thread structure may hang around. */ /* Do it in a way that is likely to trap if we access it. */ - fl[i] = (ptr_t)(GC_uintptr_t)HBLKSIZE; + fl[i] = (ptr_t)NUMERIC_TO_VPTR(HBLKSIZE); } /* The 0 granule free list really contains 1 granule objects. */ if (ADDR(fl[0]) >= HBLKSIZE @@ -116,10 +116,10 @@ GC_init_thread_local(GC_tlfs p) } for (j = 0; j < GC_TINY_FREELISTS; ++j) { for (k = 0; k < THREAD_FREELISTS_KINDS; ++k) { - p->_freelists[k][j] = (void *)(GC_uintptr_t)1; + p->_freelists[k][j] = NUMERIC_TO_VPTR(1); } # ifdef GC_GCJ_SUPPORT - p->gcj_freelists[j] = (void *)(GC_uintptr_t)1; + p->gcj_freelists[j] = NUMERIC_TO_VPTR(1); # endif } /* The zero-sized free list is handled like the regular free list, */ diff --git a/win32_threads.c b/win32_threads.c index 4ab81dcb1..2e443d3c2 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -109,7 +109,7 @@ GC_use_threads_discovery(void) # endif GC_init(); # ifdef CPPCHECK - GC_noop1((word)(GC_funcptr_uint)&GC_DllMain); + GC_noop1((word)(GC_funcptr_uint)(&GC_DllMain)); # endif # endif } @@ -1215,7 +1215,7 @@ GC_start_mark_threads_inner(void) /* There is no _beginthreadex() in WinCE. */ handle = CreateThread(NULL /* lpsa */, MARK_THREAD_STACK_SIZE /* ignored */, GC_mark_thread, - (LPVOID)(word)i, 0 /* fdwCreate */, &thread_id); + NUMERIC_TO_VPTR(i), 0 /* fdwCreate */, &thread_id); if (EXPECT(NULL == handle, FALSE)) { WARN("Marker thread %" WARN_PRIdPTR " creation failed\n", (signed_word)i); @@ -1231,7 +1231,7 @@ GC_start_mark_threads_inner(void) GC_marker_last_stack_min[i] = ADDR_LIMIT; handle = _beginthreadex(NULL /* security_attr */, MARK_THREAD_STACK_SIZE, - GC_mark_thread, (void *)(word)i, 0 /* flags */, + GC_mark_thread, NUMERIC_TO_VPTR(i), 0 /* flags */, &thread_id); if (EXPECT(!handle || handle == (GC_uintptr_t)-1L, FALSE)) { WARN("Marker thread %" WARN_PRIdPTR " creation failed\n", @@ -1429,7 +1429,7 @@ GC_win32_start_inner(struct GC_stack_base *sb, void *arg) __try # endif { - ret = (void *)(word)((*start_routine)(start_arg)); + ret = NUMERIC_TO_VPTR(start_routine(start_arg)); } # ifndef NO_SEH_AVAILABLE __finally @@ -1608,7 +1608,7 @@ main_thread_start(LPVOID arg) STATIC void *GC_CALLBACK GC_waitForSingleObjectInfinite(void *handle) { - return (void *)(word)WaitForSingleObject((HANDLE)handle, INFINITE); + return NUMERIC_TO_VPTR(WaitForSingleObject((HANDLE)handle, INFINITE)); } # ifndef WINMAIN_THREAD_STACK_SIZE