From 75f1f20031264c226085b98352b229fd58d4b036 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 2 Jul 2025 21:33:12 +0200 Subject: [PATCH] Refactor relationship between gc_mutator_roots and threads * libguile/threads.c (guilify_self_1): * libguile/whippet-embedder.h (gc_trace_mutator_pinned_roots): Make the roots contain the thread. This way sizeof(*thread) is the total allocation size; if you pad the allocation with extra words, some of the self-checks in nofl get borked. --- libguile/threads.c | 14 +++----------- libguile/whippet-embedder.h | 4 ++-- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/libguile/threads.c b/libguile/threads.c index 85ccc7a57..ee3e6cc63 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -359,22 +359,15 @@ static int thread_count; static SCM default_dynamic_state; -struct scm_thread_and_roots -{ - struct scm_thread thread; - struct gc_mutator_roots roots; -}; - /* Perform first stage of thread initialisation, in non-guile mode. */ static void guilify_self_1 (struct gc_mutator *mut, struct gc_stack_addr base, int needs_unregister) { - struct scm_thread_and_roots *thread_and_roots = - gc_allocate (mut, sizeof (*thread_and_roots), GC_ALLOCATION_TAGGED); - scm_thread *t = &thread_and_roots->thread; - struct gc_mutator_roots *roots = &thread_and_roots->roots; + struct gc_mutator_roots *roots = + gc_allocate (mut, sizeof (*roots), GC_ALLOCATION_TAGGED); + scm_thread *t = &roots->thread; /* We'll be referring to this object from thread-locals and other places that are gnarly to relocate. */ @@ -410,7 +403,6 @@ guilify_self_1 (struct gc_mutator *mut, struct gc_stack_addr base, scm_i_current_thread = t; #endif - roots->thread = t; gc_mutator_set_roots (mut, roots); scm_i_pthread_mutex_lock (&thread_admin_mutex); diff --git a/libguile/whippet-embedder.h b/libguile/whippet-embedder.h index bd6c9f934..e09f017f5 100644 --- a/libguile/whippet-embedder.h +++ b/libguile/whippet-embedder.h @@ -39,7 +39,7 @@ struct scm_thread; struct gc_mutator_roots { - struct scm_thread *thread; + struct scm_thread thread; }; #define GC_EMBEDDER_EPHEMERON_HEADER uintptr_t tag; @@ -129,7 +129,7 @@ gc_trace_mutator_pinned_roots (struct gc_mutator_roots *roots, void *trace_data), struct gc_heap *heap, void *trace_data) { - scm_trace_thread_roots (roots->thread, trace_pinned, trace_ambiguous, + scm_trace_thread_roots (&roots->thread, trace_pinned, trace_ambiguous, heap, trace_data); }