From b9ad46f61c766c1a34ab1dc613e33897a6559153 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Wed, 12 Jun 2024 12:55:07 +0300 Subject: [PATCH] Define GC_CAST_AWAY_CONST_PVOID() public macro Issue #627 (bdwgc). * dbg_mlc.c (GC_debug_ptr_store_and_dirty): Use GC_CAST_AWAY_CONST_PVOID() instead of explicit cast to word and then to void*. * include/gc/gc.h (GC_GENERAL_REGISTER_DISAPPEARING_LINK_SAFE, GC_REGISTER_LONG_LINK_SAFE): Likewise. * include/private/gc_priv.h (GC_base_C): Likewise. * specific.c (GC_key_create_inner): Likewise. * include/gc/gc.h (include/gc/gc.h): New public macro. --- dbg_mlc.c | 4 ++-- include/gc/gc.h | 7 ++++--- include/private/gc_priv.h | 2 +- specific.c | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/dbg_mlc.c b/dbg_mlc.c index 528b77cd9..5c4f8a55b 100644 --- a/dbg_mlc.c +++ b/dbg_mlc.c @@ -611,8 +611,8 @@ GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p) GC_API void GC_CALL GC_debug_ptr_store_and_dirty(void *p, const void *q) { - *(void **)GC_is_visible(p) = GC_is_valid_displacement( - (/* no const */ void *)(word)q); + *(void **)GC_is_visible(p) + = GC_is_valid_displacement(GC_CAST_AWAY_CONST_PVOID(q)); GC_debug_end_stubborn_change(p); REACHABLE_AFTER_DIRTY(q); } diff --git a/include/gc/gc.h b/include/gc/gc.h index 1743fba91..471c3b438 100644 --- a/include/gc/gc.h +++ b/include/gc/gc.h @@ -1098,15 +1098,16 @@ GC_API /* 'realloc' attr */ GC_ATTR_ALLOC_SIZE(2) void * GC_CALL GC_debug_realloc_replacement(void * /* object_addr */, size_t /* size_in_bytes */); +#define GC_CAST_AWAY_CONST_PVOID(p) ((/* no const */ void *)(GC_uintptr_t)(p)) + /* Convenient macros for disappearing links registration working both */ /* for debug and non-debug allocated objects, and accepting interior */ /* pointers to object. */ #define GC_GENERAL_REGISTER_DISAPPEARING_LINK_SAFE(link, obj) \ GC_general_register_disappearing_link(link, \ - GC_base((/* no const */ void *)(GC_word)(obj))) + GC_base(GC_CAST_AWAY_CONST_PVOID(obj))) #define GC_REGISTER_LONG_LINK_SAFE(link, obj) \ - GC_register_long_link(link, \ - GC_base((/* no const */ void *)(GC_word)(obj))) + GC_register_long_link(link, GC_base(GC_CAST_AWAY_CONST_PVOID(obj))) #ifdef GC_DEBUG_REPLACEMENT # define GC_MALLOC(sz) GC_debug_malloc_replacement(sz) diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index f182789dd..2a69e5a1f 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -2705,7 +2705,7 @@ GC_EXTERN GC_bool GC_print_back_height; #endif /* Same as GC_base but excepts and returns a pointer to const object. */ -#define GC_base_C(p) ((const void *)GC_base((/* no const */ void *)(word)(p))) +#define GC_base_C(p) ((const void *)GC_base(GC_CAST_AWAY_CONST_PVOID(p))) /* Debugging print routines: */ void GC_print_block_list(void); diff --git a/specific.c b/specific.c index 419c8a86a..2e344cd23 100644 --- a/specific.c +++ b/specific.c @@ -34,10 +34,10 @@ GC_INNER int GC_key_create_inner(tsd ** key_ptr) GC_ASSERT(ADDR(&invalid_tse.next) % sizeof(tse *) == 0); result = (tsd *)MALLOC_CLEAR(sizeof(tsd)); if (NULL == result) return ENOMEM; - ret = pthread_mutex_init(&result->lock, NULL); + ret = pthread_mutex_init(&(result -> lock), NULL); if (ret != 0) return ret; for (i = 0; i < TS_CACHE_SIZE; ++i) { - result -> cache[i] = (/* no const */ tse *)(word)(&invalid_tse); + result -> cache[i] = (tse *)GC_CAST_AWAY_CONST_PVOID(&invalid_tse); } # ifdef GC_ASSERTIONS for (i = 0; i < TS_HASH_SIZE; ++i) {