Skip to content

Commit

Permalink
Remove COOLER_THAN and change HOTTER_THAN macro to a functional style
Browse files Browse the repository at this point in the history
(refactoring)

Issue #627 (bdwgc).

* include/private/gc_priv.h (COOLER_THAN): Remove.
* include/private/gc_priv.h (HOTTER_THAN): Change macro to have two
arguments; add comment.
* include/private/gc_priv.h (MAKE_HOTTER): Rename x,y arguments to p,d.
* include/private/gc_priv.h [!THREADS] (GC_is_static_root): Change type
of argument from void* to ptr_t.
* mark_rts.c [!THREADS] (GC_is_static_root): Likewise.
* ptr_chck.c [!THREADS] (GC_on_stack): Likewise.
* mark_rts.c [THREADS] (GC_push_all_stack_sections): Update condition
according to MAKE_HOTTER defintion change.
* mark_rts.c [!THREADS] (GC_push_all_stack_part_eager_sections):
Likewise.
* misc.c [!ALWAYS_SMALL_CLEAR_STACK && !STACK_NOT_SCANNED
&& !ASM_CLEAR_CODE && !THREADS] (GC_clear_stack_inner): Likewise.
* misc.c [!THREADS] (GC_init, GC_call_with_gc_active): Likewise.
* os_dep.c [!ECOS && !NOSYS && !SYMBIAN && EMSCRIPTEN && !AMIGA
&& !EMBOX && !HAIKU && !OS2 && !ANY_MSWIN && !GC_OPENBSD_THREADS
&& (!GC_SOLARIS_THREADS || _STRICT_STDC) && !CPPCHECK]
(GC_get_main_stack_base): Likewise.
* os_dep.c [GC_DARWIN_THREADS && !NO_PTHREAD_GET_STACKADDR_NP
|| GC_SOLARIS_THREADS && !_STRICT_STDC || !GET_MAIN_STACKBASE_SPECIAL]
(GC_get_stack_base): Likewise.
* os_dep.c [NEED_CALLINFO && SAVE_CALL_CHAIN && !(NARGS==0
&& NFRAMES%2==0 && GC_HAVE_BUILTIN_BACKTRACE) && (I386 || SPARC)]
(GC_save_callers): Likewise.
* pthread_support.c (GC_call_with_gc_active): Likewise.
* ptr_chck.c [!THREADS] (GC_on_stack): Likewise.
* win32_threads.c [!WOW64_THREAD_CONTEXT_WORKAROUND]
(GC_push_stack_for): Likewise.
  • Loading branch information
ivmai committed Mar 30, 2024
1 parent 55d3a91 commit e813016
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
19 changes: 9 additions & 10 deletions include/private/gc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,19 @@ typedef struct hblkhdr hdr;

#define GC_WORD_MAX (~(word)0)

/* Handy definitions to compare and adjust pointers in a stack. */
#ifdef STACK_GROWS_UP
# define COOLER_THAN <
# define HOTTER_THAN >
# define MAKE_COOLER(p,d) \
# define HOTTER_THAN(p,q) ((word)(p) > (word)(q))
# define MAKE_COOLER(p,d) \
(void)((p) -= (word)(p) >= (word)((d) * sizeof(*(p))) ? (d) : 0)
# define MAKE_HOTTER(x,y) (void)((x) += (y))
# define MAKE_HOTTER(p,d) (void)((p) += (d))
#else
# define COOLER_THAN >
# define HOTTER_THAN <
# define MAKE_COOLER(p,d) \
# define HOTTER_THAN(p,q) ((word)(p) < (word)(q))
# define MAKE_COOLER(p,d) \
(void)((p) += (word)(p) <= (word)(GC_WORD_MAX \
- (d) * sizeof(*(p))) ? (d) : 0)
# define MAKE_HOTTER(x,y) (void)((x) -= (y))
#endif
# define MAKE_HOTTER(p,d) (void)((p) -= (d))
#endif /* !STACK_GROWS_UP */

#if defined(AMIGA) && defined(__SASC)
# define GC_FAR __far
Expand Down Expand Up @@ -2218,7 +2217,7 @@ void GC_register_data_segments(void);
GC_INNER GC_bool GC_is_main_thread(void);
# endif
#else
GC_INNER GC_bool GC_is_static_root(void *p);
GC_INNER GC_bool GC_is_static_root(ptr_t p);
/* Is the address p in one of the registered static */
/* root sections? */
# ifdef TRACE_BUF
Expand Down
14 changes: 7 additions & 7 deletions mark_rts.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int GC_no_dls = 0; /* Register dynamic library data segments. */
#ifndef THREADS
/* Primarily for debugging support: */
/* Is the address p in one of the registered static root sections? */
GC_INNER GC_bool GC_is_static_root(void *p)
GC_INNER GC_bool GC_is_static_root(ptr_t p)
{
static int last_root_set = MAX_ROOT_SETS;
int i;
Expand Down Expand Up @@ -719,7 +719,7 @@ STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
{
GC_ASSERT(I_HOLD_LOCK());
while (traced_stack_sect != NULL) {
GC_ASSERT((word)lo HOTTER_THAN (word)traced_stack_sect);
GC_ASSERT(HOTTER_THAN(lo, (ptr_t)traced_stack_sect));
# ifdef STACK_GROWS_UP
GC_push_all_stack((ptr_t)traced_stack_sect, lo);
# else
Expand All @@ -729,7 +729,7 @@ STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
GC_ASSERT(lo != NULL);
traced_stack_sect = traced_stack_sect -> prev;
}
GC_ASSERT(!((word)hi HOTTER_THAN (word)lo));
GC_ASSERT(!HOTTER_THAN(hi, lo));
# ifdef STACK_GROWS_UP
/* We got them backwards! */
GC_push_all_stack(hi, lo);
Expand Down Expand Up @@ -790,11 +790,11 @@ STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
ptr_t lo /* top */, ptr_t hi /* bottom */, ptr_t cold_gc_frame,
struct GC_traced_stack_sect_s *traced_stack_sect)
{
GC_ASSERT(traced_stack_sect == NULL || cold_gc_frame == NULL ||
(word)cold_gc_frame HOTTER_THAN (word)traced_stack_sect);
GC_ASSERT(traced_stack_sect == NULL || cold_gc_frame == NULL
|| HOTTER_THAN(cold_gc_frame, (ptr_t)traced_stack_sect));

while (traced_stack_sect != NULL) {
GC_ASSERT((word)lo HOTTER_THAN (word)traced_stack_sect);
GC_ASSERT(HOTTER_THAN(lo, (ptr_t)traced_stack_sect));
# ifdef STACK_GROWS_UP
GC_push_all_stack_partially_eager((ptr_t)traced_stack_sect, lo,
cold_gc_frame);
Expand All @@ -808,7 +808,7 @@ STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
cold_gc_frame = NULL; /* Use at most once. */
}

GC_ASSERT(!((word)hi HOTTER_THAN (word)lo));
GC_ASSERT(!HOTTER_THAN(hi, lo));
# ifdef STACK_GROWS_UP
/* We got them backwards! */
GC_push_all_stack_partially_eager(hi, lo, cold_gc_frame);
Expand Down
13 changes: 6 additions & 7 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ STATIC void GC_init_size_map(void)
volatile word dummy[CLEAR_SIZE];

BZERO((/* no volatile */ word *)((word)dummy), sizeof(dummy));
if ((word)GC_approx_sp() COOLER_THAN (word)limit) {
if (HOTTER_THAN((/* no volatile */ ptr_t)limit, GC_approx_sp())) {
(void)GC_clear_stack_inner(arg, limit);
}
/* Make sure the recursive call is not a tail call, and the bzero */
Expand Down Expand Up @@ -393,15 +393,14 @@ STATIC void GC_init_size_map(void)
/* Adjust GC_high_water. */
GC_ASSERT(GC_high_water != NULL);
MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
if ((word)sp HOTTER_THAN (word)GC_high_water) {
if (HOTTER_THAN(sp, GC_high_water))
GC_high_water = sp;
}
MAKE_HOTTER(GC_high_water, GC_SLOP);
{
ptr_t limit = GC_min_sp;

MAKE_HOTTER(limit, SLOP);
if ((word)sp COOLER_THAN (word)limit) {
if (HOTTER_THAN(limit, sp)) {
limit = PTR_ALIGN_DOWN(limit, 0x10);
GC_min_sp = sp;
return GC_clear_stack_inner(arg, limit);
Expand All @@ -411,7 +410,7 @@ STATIC void GC_init_size_map(void)
/* Restart clearing process, but limit how much clearing we do. */
GC_min_sp = sp;
MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
if ((word)GC_min_sp HOTTER_THAN (word)GC_high_water)
if (HOTTER_THAN(GC_min_sp, GC_high_water))
GC_min_sp = GC_high_water;
GC_bytes_allocd_at_reset = GC_bytes_allocd;
}
Expand Down Expand Up @@ -1345,7 +1344,7 @@ GC_API void GC_CALL GC_init(void)
# endif
GC_STATIC_ASSERT(sizeof(struct hblk) == HBLKSIZE);
# ifndef THREADS
GC_ASSERT(!((word)GC_stackbottom HOTTER_THAN (word)GC_approx_sp()));
GC_ASSERT(!HOTTER_THAN(GC_stackbottom, GC_approx_sp()));
# endif
GC_init_headers();
# ifdef SEARCH_FOR_DATA_START
Expand Down Expand Up @@ -2323,7 +2322,7 @@ GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, void *client_data)
/* Adjust our stack bottom pointer (this could happen if */
/* GC_get_main_stack_base() is unimplemented or broken for */
/* the platform). */
if ((word)GC_stackbottom HOTTER_THAN (word)(&stacksect))
if (HOTTER_THAN(GC_stackbottom, (ptr_t)(&stacksect)))
GC_stackbottom = (ptr_t)COVERT_DATAFLOW(&stacksect);

if (GC_blocked_sp == NULL) {
Expand Down
16 changes: 8 additions & 8 deletions os_dep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,8 +1342,8 @@ GC_INNER void GC_setpagesize(void)
result = (ptr_t)GC_find_limit(sp, TRUE);
# endif
# if defined(HEURISTIC2_LIMIT) && !defined(CPPCHECK)
if ((word)result COOLER_THAN (word)HEURISTIC2_LIMIT
&& (word)sp HOTTER_THAN (word)HEURISTIC2_LIMIT)
if (HOTTER_THAN(HEURISTIC2_LIMIT, result)
&& HOTTER_THAN(sp, HEURISTIC2_LIMIT))
result = HEURISTIC2_LIMIT;
# endif
}
Expand All @@ -1358,7 +1358,7 @@ GC_INNER void GC_setpagesize(void)
# endif
# endif
# if !defined(CPPCHECK)
GC_ASSERT((word)GC_approx_sp() HOTTER_THAN (word)result);
GC_ASSERT(HOTTER_THAN(GC_approx_sp(), result));
# endif
return result;
}
Expand Down Expand Up @@ -1438,7 +1438,7 @@ GC_INNER void GC_setpagesize(void)
/* pthread_get_stackaddr_np() should return stack bottom (highest */
/* stack address plus 1). */
b -> mem_base = pthread_get_stackaddr_np(pthread_self());
GC_ASSERT((word)GC_approx_sp() HOTTER_THAN (word)b->mem_base);
GC_ASSERT(HOTTER_THAN(GC_approx_sp(), (ptr_t)(b -> mem_base)));
return GC_SUCCESS;
}
# define HAVE_GET_STACK_BASE
Expand Down Expand Up @@ -1495,7 +1495,7 @@ GC_INNER void GC_setpagesize(void)
ABORT("thr_stksegment failed");
}
/* s.ss_sp holds the pointer to the stack bottom. */
GC_ASSERT((word)GC_approx_sp() HOTTER_THAN (word)s.ss_sp);
GC_ASSERT(HOTTER_THAN(GC_approx_sp(), (ptr_t)s.ss_sp));

if (!stackbase_main_self && thr_main() != 0)
{
Expand Down Expand Up @@ -1572,7 +1572,7 @@ GC_INNER void GC_setpagesize(void)

if (GC_get_stack_base(&sb) != GC_SUCCESS)
ABORT("GC_get_stack_base failed");
GC_ASSERT((word)GC_approx_sp() HOTTER_THAN (word)sb.mem_base);
GC_ASSERT(HOTTER_THAN(GC_approx_sp(), (ptr_t)sb.mem_base));
return (ptr_t)sb.mem_base;
}
#endif /* !GET_MAIN_STACKBASE_SPECIAL */
Expand Down Expand Up @@ -5347,9 +5347,9 @@ GC_API int GC_CALL GC_get_pages_executable(void)
fp = (struct frame *)((long)(frame -> FR_SAVFP) + BIAS);
# endif

for (; !((word)fp HOTTER_THAN (word)frame)
for (; !HOTTER_THAN((ptr_t)fp, (ptr_t)frame)
# ifndef THREADS
&& !((word)GC_stackbottom HOTTER_THAN (word)fp)
&& !HOTTER_THAN(GC_stackbottom, (ptr_t)fp)
# elif defined(STACK_GROWS_UP)
&& fp != NULL
# endif
Expand Down
2 changes: 1 addition & 1 deletion pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, void *client_data)
/* GC_get_stack_base() was used which returned GC_SUCCESS). */
stack_end = crtn -> stack_end; /* read of a volatile field */
GC_ASSERT(stack_end != NULL);
if ((word)stack_end HOTTER_THAN (word)(&stacksect)) {
if (HOTTER_THAN(stack_end, (ptr_t)(&stacksect))) {
crtn -> stack_end = (ptr_t)(&stacksect);
# if defined(I386) && defined(GC_WIN32_THREADS)
crtn -> initial_stack_base = (ptr_t)(&stacksect);
Expand Down
11 changes: 5 additions & 6 deletions ptr_chck.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ GC_valid_ptr_print_proc_t GC_is_visible_print_proc =

#ifndef THREADS
/* Could p be a stack address? */
STATIC GC_bool GC_on_stack(void *p)
STATIC GC_bool GC_on_stack(ptr_t p)
{
return (word)p HOTTER_THAN (word)GC_stackbottom
&& !((word)p HOTTER_THAN (word)GC_approx_sp());
return HOTTER_THAN(p, GC_stackbottom) && !HOTTER_THAN(p, GC_approx_sp());
}
#endif /* !THREADS */

Expand All @@ -159,16 +158,16 @@ GC_API void * GC_CALL GC_is_visible(void *p)
}
# else
/* Check stack first: */
if (GC_on_stack(p)) return p;
if (GC_on_stack((ptr_t)p)) return p;

hhdr = HDR(p);
if (NULL == hhdr) {
if (GC_is_static_root(p)) return p;
if (GC_is_static_root((ptr_t)p)) return p;
/* Else do it again correctly: */
# if defined(DYNAMIC_LOADING) || defined(ANY_MSWIN) || defined(PCR)
if (!GC_no_dls) {
GC_register_dynamic_libraries();
if (GC_is_static_root(p)) return p;
if (GC_is_static_root((ptr_t)p)) return p;
}
# endif
goto fail;
Expand Down
2 changes: 1 addition & 1 deletion win32_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ STATIC word GC_push_stack_for(GC_thread thread, thread_id_t self_id,
GC_log_printf("TIB stack limit/base: %p .. %p\n",
(void *)tib->StackLimit, (void *)tib->StackBase);
# endif
GC_ASSERT(!((word)stack_end COOLER_THAN (word)tib->StackBase));
GC_ASSERT(!HOTTER_THAN((ptr_t)tib->StackBase, stack_end));
if (stack_end != crtn -> initial_stack_base
/* We are in a coroutine (old-style way of the support). */
&& ((word)stack_end <= (word)tib->StackLimit
Expand Down

0 comments on commit e813016

Please sign in to comment.