1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-01 15:20:34 +02:00

Move fluids off of scm_cell

* libguile/fluids-internal.h: New file for internal definitions.
* libguile/dynstack.c:
* libguile/init.c:
* libguile/intrinsics.c:
* libguile/modules.c:
* libguile/print.c:
* libguile/threads.c: Include new internal file.
* libguile/fluids.c: Use "struct scm_fluid".
* libguile/Makefile.am: Add new file.
This commit is contained in:
Andy Wingo 2025-06-20 14:43:10 +02:00
parent 8141c35ec4
commit 453fe41a26
10 changed files with 116 additions and 57 deletions

View file

@ -517,6 +517,7 @@ noinst_HEADERS = custom-ports.h \
bytevectors-internal.h \
cache-internal.h \
continuations-internal.h \
fluids-internal.h \
gc-inline.h \
gc-internal.h \
gsubr-internal.h \

View file

@ -34,7 +34,7 @@
#include "control.h"
#include "eval.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "variable.h"
#include "threads.h"
#include "trace.h"

View file

@ -0,0 +1,71 @@
#ifndef SCM_FLUIDS_INTERNAL_H
#define SCM_FLUIDS_INTERNAL_H
/* Copyright 1996,2000-2001,2006,2008-2013,2018,2025
Free Software Foundation, Inc.
This file is part of Guile.
Guile is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guile is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
#include <libguile/fluids.h>
#include <libguile/gc.h>
#include <libguile/cache-internal.h>
struct scm_fluid
{
scm_t_bits tag_and_flags;
SCM default_value;
};
static inline struct scm_fluid*
scm_to_fluid (SCM x)
{
if (!scm_is_fluid (x))
abort ();
return (struct scm_fluid *) SCM_UNPACK_POINTER (x);
}
static inline SCM
scm_from_fluid (struct scm_fluid *fluid)
{
return SCM_PACK_POINTER (fluid);
}
struct scm_ephemeron_table;
struct scm_dynamic_state
{
SCM thread_local_values;
struct scm_ephemeron_table *values;
uint8_t has_aliased_values;
struct scm_cache cache;
};
SCM_INTERNAL SCM scm_i_fluid_ref (scm_thread *thread, SCM fluid);
SCM_INTERNAL void scm_swap_fluid (SCM fluid, SCM value_box,
scm_t_dynamic_state *dynamic_state);
SCM_INTERNAL SCM scm_dynamic_state_ref (SCM state, SCM fluid, SCM dflt);
SCM_INTERNAL SCM scm_i_make_initial_dynamic_state (void);
SCM_INTERNAL void scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate);
SCM_INTERNAL void scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate);
SCM_INTERNAL void scm_init_fluids (void);
#endif /* SCM_FLUIDS_INTERNAL_H */

View file

@ -31,6 +31,7 @@
#include "dynwind.h"
#include "ephemerons.h"
#include "eval.h"
#include "fluids-internal.h"
#include "gsubr.h"
#include "hashtab.h"
#include "list.h"
@ -40,8 +41,6 @@
#include "threads.h"
#include "variable.h"
#include "fluids.h"
/* A dynamic state associates fluids with values. There are two
representations of a dynamic state in Guile: the active
@ -98,8 +97,18 @@
hash trie or something, but we don't have such a data structure. */
#define FLUID_F_THREAD_LOCAL 0x100
#define SCM_I_FLUID_THREAD_LOCAL_P(x) \
(SCM_CELL_WORD_0 (x) & FLUID_F_THREAD_LOCAL)
static int
fluid_is_thread_local (struct scm_fluid *fluid)
{
return (fluid->tag_and_flags & FLUID_F_THREAD_LOCAL) != 0;
}
static SCM
fluid_default_value (struct scm_fluid *fluid)
{
return fluid->default_value;
}
static inline int
is_dynamic_state (SCM x)
@ -148,7 +157,7 @@ save_dynamic_state (scm_t_dynamic_state *state)
if (!entry->key)
continue;
if (SCM_I_FLUID_THREAD_LOCAL_P (key))
if (fluid_is_thread_local (scm_to_fluid (key)))
{
/* Because we don't include unflushed thread-local fluids in
the result, we need to flush them to the table so that
@ -187,7 +196,7 @@ saved_dynamic_state_ref (SCM saved, SCM fluid, SCM dflt)
void
scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
{
if (SCM_I_FLUID_THREAD_LOCAL_P (exp))
if (fluid_is_thread_local (scm_to_fluid (exp)))
scm_puts ("#<thread-local-fluid ", port);
else
scm_puts ("#<fluid ", port);
@ -206,12 +215,15 @@ scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED
#define SCM_I_FLUID_DEFAULT(x) (SCM_CELL_OBJECT_1 (x))
static SCM
new_fluid (SCM init, scm_t_bits flags)
{
return scm_cell (scm_tc7_fluid | flags, SCM_UNPACK (init));
struct scm_fluid *fluid = scm_allocate_tagged (SCM_I_CURRENT_THREAD,
sizeof (*fluid));
fluid->tag_and_flags = scm_tc7_fluid | flags;
fluid->default_value = init;
return scm_from_fluid (fluid);
}
SCM
@ -277,16 +289,10 @@ SCM_DEFINE (scm_fluid_thread_local_p, "fluid-thread-local?", 1, 0, 0,
#define FUNC_NAME s_scm_fluid_thread_local_p
{
SCM_VALIDATE_FLUID (1, fluid);
return scm_from_bool (SCM_I_FLUID_THREAD_LOCAL_P (fluid));
return scm_from_bool (fluid_is_thread_local (scm_to_fluid (fluid)));
}
#undef FUNC_NAME
int
scm_is_fluid (SCM obj)
{
return SCM_FLUID_P (obj);
}
static void
fluid_set_x (scm_t_dynamic_state *dynamic_state, SCM fluid, SCM value)
{
@ -307,7 +313,7 @@ fluid_set_x (scm_t_dynamic_state *dynamic_state, SCM fluid, SCM value)
fluid = SCM_PACK (evicted.key);
value = SCM_PACK (evicted.value);
if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
if (fluid_is_thread_local (scm_to_fluid (fluid)))
{
scm_hashq_set_x (dynamic_state->thread_local_values, fluid, value);
return;
@ -340,12 +346,12 @@ fluid_ref (scm_t_dynamic_state *dynamic_state, SCM fluid)
if (scm_is_eq (SCM_PACK (entry->key), fluid))
return SCM_PACK (entry->value);
if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
if (fluid_is_thread_local (scm_to_fluid (fluid)))
val = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
SCM_I_FLUID_DEFAULT (fluid));
fluid_default_value (scm_to_fluid (fluid)));
else
val = scm_c_ephemeron_hash_table_refq (dynamic_state->values, fluid,
SCM_I_FLUID_DEFAULT (fluid));
fluid_default_value (scm_to_fluid (fluid)));
/* Cache this lookup. */
fluid_set_x (dynamic_state, fluid, val);
@ -404,7 +410,7 @@ SCM_DEFINE (scm_fluid_ref_star, "fluid-ref*", 2, 0, 0,
ret = SCM_PACK (entry->value);
else
{
if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
if (fluid_is_thread_local (scm_to_fluid (fluid)))
ret = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
SCM_UNDEFINED);
else
@ -412,15 +418,16 @@ SCM_DEFINE (scm_fluid_ref_star, "fluid-ref*", 2, 0, 0,
SCM_UNDEFINED);
if (SCM_UNBNDP (ret))
ret = SCM_I_FLUID_DEFAULT (fluid);
ret = fluid_default_value (scm_to_fluid (fluid));
/* Don't cache the lookup. */
}
}
else
ret = scm_dynstack_find_old_fluid_value (&SCM_I_CURRENT_THREAD->dynstack,
ret = scm_dynstack_find_old_fluid_value
(&SCM_I_CURRENT_THREAD->dynstack,
fluid, c_depth - 1,
SCM_I_FLUID_DEFAULT (fluid));
fluid_default_value (scm_to_fluid (fluid)));
if (SCM_UNBNDP (ret))
scm_misc_error ("fluid-ref*", "unbound fluid: ~S", scm_list_1 (fluid));

View file

@ -38,27 +38,19 @@
#define SCM_FLUID_P(x) (SCM_HAS_TYP7 (x, scm_tc7_fluid))
static inline int
scm_is_fluid (SCM x)
{
return SCM_FLUID_P (x);
}
#define SCM_VALIDATE_FLUID(pos, fluid) \
SCM_I_MAKE_VALIDATE_MSG2 (pos, fluid, scm_is_fluid, "fluid")
#ifdef BUILDING_LIBGUILE
# include <libguile/cache-internal.h>
struct scm_ephemeron_table;
struct scm_dynamic_state
{
SCM thread_local_values;
struct scm_ephemeron_table *values;
uint8_t has_aliased_values;
struct scm_cache cache;
};
#endif
SCM_API SCM scm_make_fluid (void);
SCM_API SCM scm_make_fluid_with_default (SCM dflt);
SCM_API SCM scm_make_unbound_fluid (void);
SCM_API SCM scm_make_thread_local_fluid (SCM dflt);
SCM_API int scm_is_fluid (SCM obj);
SCM_API SCM scm_fluid_p (SCM fl);
SCM_API SCM scm_fluid_thread_local_p (SCM fluid);
SCM_API SCM scm_fluid_ref (SCM fluid);
@ -67,11 +59,6 @@ SCM_API SCM scm_fluid_set_x (SCM fluid, SCM value);
SCM_API SCM scm_fluid_unset_x (SCM fluid);
SCM_API SCM scm_fluid_bound_p (SCM fluid);
SCM_INTERNAL SCM scm_i_fluid_ref (scm_thread *thread, SCM fluid);
SCM_INTERNAL void scm_swap_fluid (SCM fluid, SCM value_box,
scm_t_dynamic_state *dynamic_state);
SCM_API SCM scm_c_with_fluids (SCM fluids, SCM vals,
SCM (*cproc)(void *), void *cdata);
SCM_API SCM scm_c_with_fluid (SCM fluid, SCM val,
@ -89,12 +76,5 @@ SCM_API void scm_dynwind_current_dynamic_state (SCM state);
SCM_API void *scm_c_with_dynamic_state (SCM state,
void *(*func)(void *), void *data);
SCM_API SCM scm_with_dynamic_state (SCM state, SCM proc);
SCM_INTERNAL SCM scm_dynamic_state_ref (SCM state, SCM fluid, SCM dflt);
SCM_INTERNAL SCM scm_i_make_initial_dynamic_state (void);
SCM_INTERNAL void scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate);
SCM_INTERNAL void scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate);
SCM_INTERNAL void scm_init_fluids (void);
#endif /* SCM_FLUIDS_H */

View file

@ -69,7 +69,7 @@
#include "feature.h"
#include "filesys.h"
#include "finalizers.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "foreign-object.h"
#include "foreign.h"
#include "fports.h"

View file

@ -29,7 +29,7 @@
#include "bytevectors.h"
#include "cache-internal.h"
#include "extensions.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "frames.h"
#include "gc-inline.h"
#include "goops.h"

View file

@ -28,7 +28,7 @@
#include "boolean.h"
#include "deprecation.h"
#include "eval.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "gsubr.h"
#include "hashtab.h"
#include "keywords.h"

View file

@ -45,7 +45,7 @@
#include "extensions.h"
#include "finalizers.h"
#include "filesys.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "foreign.h"
#include "frames.h"
#include "goops.h"

View file

@ -47,7 +47,7 @@
#include "eval.h"
#include "extensions.h"
#include "finalizers.h"
#include "fluids.h"
#include "fluids-internal.h"
#include "gc-internal.h"
#include "gc.h"
#include "gsubr.h"