Skip to content

Commit

Permalink
Define internal macro for a number-to-pointer conversion
Browse files Browse the repository at this point in the history
(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.
  • Loading branch information
ivmai committed Oct 29, 2024
1 parent edf607b commit f99635f
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions include/private/dbg_mlc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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)
Expand All @@ -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 */
Expand Down
5 changes: 5 additions & 0 deletions include/private/gc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion pthread_stop_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion reclaim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, */
Expand Down
21 changes: 11 additions & 10 deletions tests/gctest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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++)
Expand Down
6 changes: 3 additions & 3 deletions thread_local_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, */
Expand Down
10 changes: 5 additions & 5 deletions win32_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f99635f

Please sign in to comment.