wingo pushed a commit to branch wip-whippet
in repository guile.

commit 453fe41a26e1cd248de8bd1bd9e48c436e6ec12e
Author: Andy Wingo <wi...@pobox.com>
AuthorDate: Fri Jun 20 14:43:10 2025 +0200

    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.
---
 libguile/Makefile.am       |  1 +
 libguile/dynstack.c        |  2 +-
 libguile/fluids-internal.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 libguile/fluids.c          | 57 +++++++++++++++++++++----------------
 libguile/fluids.h          | 32 ++++-----------------
 libguile/init.c            |  2 +-
 libguile/intrinsics.c      |  2 +-
 libguile/modules.c         |  2 +-
 libguile/print.c           |  2 +-
 libguile/threads.c         |  2 +-
 10 files changed, 116 insertions(+), 57 deletions(-)

diff --git a/libguile/Makefile.am b/libguile/Makefile.am
index 141083f21..932fcd972 100644
--- a/libguile/Makefile.am
+++ b/libguile/Makefile.am
@@ -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                              \
diff --git a/libguile/dynstack.c b/libguile/dynstack.c
index d2d181d46..847925fe9 100644
--- a/libguile/dynstack.c
+++ b/libguile/dynstack.c
@@ -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"
diff --git a/libguile/fluids-internal.h b/libguile/fluids-internal.h
new file mode 100644
index 000000000..412aa1627
--- /dev/null
+++ b/libguile/fluids-internal.h
@@ -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 */
diff --git a/libguile/fluids.c b/libguile/fluids.c
index b95930782..54afc9195 100644
--- a/libguile/fluids.c
+++ b/libguile/fluids.c
@@ -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,
-                                             fluid, c_depth - 1,
-                                             SCM_I_FLUID_DEFAULT (fluid));
+    ret = scm_dynstack_find_old_fluid_value
+      (&SCM_I_CURRENT_THREAD->dynstack,
+       fluid, c_depth - 1,
+       fluid_default_value (scm_to_fluid (fluid)));
 
   if (SCM_UNBNDP (ret))
     scm_misc_error ("fluid-ref*", "unbound fluid: ~S", scm_list_1 (fluid));
diff --git a/libguile/fluids.h b/libguile/fluids.h
index 9153169d0..9bd2961ad 100644
--- a/libguile/fluids.h
+++ b/libguile/fluids.h
@@ -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 */
diff --git a/libguile/init.c b/libguile/init.c
index ac1d09f01..110a4d7df 100644
--- a/libguile/init.c
+++ b/libguile/init.c
@@ -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"
diff --git a/libguile/intrinsics.c b/libguile/intrinsics.c
index e3a6ba5ce..f250c7b9b 100644
--- a/libguile/intrinsics.c
+++ b/libguile/intrinsics.c
@@ -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"
diff --git a/libguile/modules.c b/libguile/modules.c
index 136004392..8872e1e20 100644
--- a/libguile/modules.c
+++ b/libguile/modules.c
@@ -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"
diff --git a/libguile/print.c b/libguile/print.c
index 93f928120..443a9e3f1 100644
--- a/libguile/print.c
+++ b/libguile/print.c
@@ -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"
diff --git a/libguile/threads.c b/libguile/threads.c
index 3b0af1abd..26e8da43a 100644
--- a/libguile/threads.c
+++ b/libguile/threads.c
@@ -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"

Reply via email to