1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-06 07:30:28 +02:00

Refactor to how struct and smob finalization works

* libguile/finalizers.h:
* libguile/finalizers.c (scm_i_add_struct_finalizer):
(scm_i_add_smob_finalizer): New internal API.
* libguile/smob.c (scm_i_new_smob):
(scm_i_new_double_smob):
* libguile/struct.c (scm_i_alloc_struct): Use it.
This commit is contained in:
Andy Wingo 2025-05-02 16:16:57 +02:00
parent 6ffddf5dae
commit 532df66e07
4 changed files with 42 additions and 15 deletions

View file

@ -40,6 +40,8 @@
#include "gsubr.h" #include "gsubr.h"
#include "init.h" #include "init.h"
#include "numbers.h" #include "numbers.h"
#include "struct.h"
#include "smob.h"
#include "threads.h" #include "threads.h"
#include "version.h" #include "version.h"
@ -150,6 +152,38 @@ scm_i_add_finalizer (void *obj, scm_t_finalizer_proc proc, void *data)
shuffle_resuscitators_to_front (chained_data); shuffle_resuscitators_to_front (chained_data);
} }
static void
struct_finalizer_trampoline (void *ptr, void *data)
{
scm_i_finalize_struct (SCM_I_CURRENT_THREAD, PTR2SCM (ptr));
}
static void
smob_finalizer_trampoline (void *ptr, void *data)
{
scm_i_finalize_smob (SCM_I_CURRENT_THREAD, PTR2SCM (ptr));
}
SCM
scm_i_add_struct_finalizer (struct scm_thread *thread, SCM obj)
{
if (!SCM_STRUCTP (obj))
abort ();
scm_i_set_finalizer (SCM2PTR (obj), struct_finalizer_trampoline, NULL);
return SCM_UNSPECIFIED;
}
SCM
scm_i_add_smob_finalizer (struct scm_thread *thread, SCM obj)
{
if (!SCM_HAS_TYP7 (obj, scm_tc7_smob))
abort ();
scm_i_set_finalizer (SCM2PTR (obj), smob_finalizer_trampoline, NULL);
return SCM_UNSPECIFIED;
}
static void static void
invoke_finalizer (void *obj, void *data) invoke_finalizer (void *obj, void *data)
{ {

View file

@ -35,6 +35,11 @@ SCM_INTERNAL void scm_i_add_finalizer (void *obj, scm_t_finalizer_proc,
SCM_INTERNAL void scm_i_add_resuscitator (void *obj, scm_t_finalizer_proc, SCM_INTERNAL void scm_i_add_resuscitator (void *obj, scm_t_finalizer_proc,
void *data); void *data);
SCM_INTERNAL SCM scm_i_add_struct_finalizer (struct scm_thread *thread,
SCM obj);
SCM_INTERNAL SCM scm_i_add_smob_finalizer (struct scm_thread *thread,
SCM obj);
SCM_INTERNAL void scm_i_finalizer_pre_fork (void); SCM_INTERNAL void scm_i_finalizer_pre_fork (void);
/* Return true if THREAD is the finalizer thread. */ /* Return true if THREAD is the finalizer thread. */

View file

@ -403,12 +403,6 @@ scm_i_finalize_smob (struct scm_thread *thread, SCM smob)
free_smob (smob); free_smob (smob);
} }
static void
finalize_smob (void *ptr, void *data)
{
return scm_i_finalize_smob (SCM_I_CURRENT_THREAD, PTR2SCM (ptr));
}
/* Return a SMOB with typecode TC. The SMOB type corresponding to TC may /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
provide a custom mark procedure and it will be honored. */ provide a custom mark procedure and it will be honored. */
SCM SCM
@ -430,7 +424,7 @@ scm_i_new_smob (scm_t_bits tc, scm_t_bits data)
SCM_SET_CELL_WORD_0 (ret, tc); SCM_SET_CELL_WORD_0 (ret, tc);
if (scm_smobs[smobnum].free) if (scm_smobs[smobnum].free)
scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL); scm_i_add_smob_finalizer (SCM_I_CURRENT_THREAD, ret);
return ret; return ret;
} }
@ -457,7 +451,7 @@ scm_i_new_double_smob (scm_t_bits tc, scm_t_bits data1,
SCM_SET_CELL_WORD_0 (ret, tc); SCM_SET_CELL_WORD_0 (ret, tc);
if (scm_smobs[smobnum].free) if (scm_smobs[smobnum].free)
scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL); scm_i_add_smob_finalizer (SCM_I_CURRENT_THREAD, ret);
return ret; return ret;
} }

View file

@ -319,12 +319,6 @@ scm_i_finalize_struct (struct scm_thread *thread, SCM obj)
finalize (obj); finalize (obj);
} }
static void
struct_finalizer_trampoline (void *ptr, void *data)
{
scm_i_finalize_struct (SCM_I_CURRENT_THREAD, PTR2SCM (ptr));
}
/* A struct is a sequence of words preceded by a pointer to the struct's /* A struct is a sequence of words preceded by a pointer to the struct's
vtable. The vtable reference is tagged with the struct tc3. */ vtable. The vtable reference is tagged with the struct tc3. */
static SCM static SCM
@ -337,7 +331,7 @@ scm_i_alloc_struct (scm_t_bits vtable_bits, int n_words)
/* vtable_bits can be 0 when making a vtable vtable */ /* vtable_bits can be 0 when making a vtable vtable */
if (vtable_bits && SCM_VTABLE_INSTANCE_FINALIZER (SCM_PACK (vtable_bits))) if (vtable_bits && SCM_VTABLE_INSTANCE_FINALIZER (SCM_PACK (vtable_bits)))
/* Register a finalizer for the newly created instance. */ /* Register a finalizer for the newly created instance. */
scm_i_set_finalizer (SCM2PTR (ret), struct_finalizer_trampoline, NULL); scm_i_add_struct_finalizer (SCM_I_CURRENT_THREAD, ret);
return ret; return ret;
} }