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

commit b262df6ca72a2562e6815a860891776ec9c76d9b
Author: Andy Wingo <wi...@pobox.com>
AuthorDate: Fri Jun 20 15:57:37 2025 +0200

    Move keywords off of scm_cell
    
    * libguile/keywords-internal.h: New file.
    * libguile/Makefile.am (noinst_HEADERS): Add new file.
    * libguile/hash.c:
    * libguile/init.c:
    * libguile/keywords.c: Include new file.
    (scm_symbol_to_keyword): Allocate via scm_allocate_tagged.
---
 libguile/Makefile.am         |  1 +
 libguile/hash.c              |  2 +-
 libguile/init.c              |  2 +-
 libguile/keywords-internal.h | 56 ++++++++++++++++++++++++++++++++++++++++++++
 libguile/keywords.c          | 24 ++++++++-----------
 libguile/keywords.h          | 17 +++++++-------
 6 files changed, 77 insertions(+), 25 deletions(-)

diff --git a/libguile/Makefile.am b/libguile/Makefile.am
index 932fcd972..56c3a7a1f 100644
--- a/libguile/Makefile.am
+++ b/libguile/Makefile.am
@@ -521,6 +521,7 @@ noinst_HEADERS = custom-ports.h                             
        \
                  gc-inline.h                                   \
                  gc-internal.h                                 \
                  gsubr-internal.h                              \
+                 keywords-internal.h                           \
                  posix-w32.h                                   \
                 private-options.h                              \
                 programs.h                                     \
diff --git a/libguile/hash.c b/libguile/hash.c
index 17e978c6e..ddc0333a8 100644
--- a/libguile/hash.c
+++ b/libguile/hash.c
@@ -32,7 +32,7 @@
 #include "chars.h"
 #include "foreign.h"
 #include "gsubr.h"
-#include "keywords.h"
+#include "keywords-internal.h"
 #include "numbers.h"
 #include "pairs.h"
 #include "ports.h"
diff --git a/libguile/init.c b/libguile/init.c
index 110a4d7df..67010c41f 100644
--- a/libguile/init.c
+++ b/libguile/init.c
@@ -87,7 +87,7 @@
 #include "intrinsics.h"
 #include "ioext.h"
 #include "jit.h"
-#include "keywords.h"
+#include "keywords-internal.h"
 #include "list.h"
 #include "load.h"
 #include "loader.h"
diff --git a/libguile/keywords-internal.h b/libguile/keywords-internal.h
new file mode 100644
index 000000000..475643e9f
--- /dev/null
+++ b/libguile/keywords-internal.h
@@ -0,0 +1,56 @@
+#ifndef SCM_KEYWORDS_INTERNAL_H
+#define SCM_KEYWORDS_INTERNAL_H
+
+/* Copyright 1995-1996,1999-2001,2006,2008,2015,2018-2019,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/keywords.h>
+#include <libguile/scm.h>
+
+
+
+struct scm_keyword
+{
+  scm_t_bits tag;
+  SCM symbol;
+};
+
+static inline struct scm_keyword *
+scm_to_keyword (SCM x)
+{
+  return (struct scm_keyword *) SCM_UNPACK_POINTER (x);
+}
+
+static inline SCM
+scm_from_keyword (struct scm_keyword *keyword)
+{
+  return SCM_PACK_POINTER (keyword);
+}
+
+#define SCM_KEYWORDP(x) (scm_is_keyword (x))
+#define SCM_VALIDATE_KEYWORD(pos, v) \
+  SCM_MAKE_VALIDATE_MSG (pos, v, KEYWORDP, "keyword")
+
+#define SCM_I_KEYWORD_HASH(x) scm_i_symbol_hash (scm_to_keyword (x)->symbol)
+
+SCM_INTERNAL void scm_init_keywords (void);
+
+#endif  /* SCM_KEYWORDS_INTERNAL_H */
diff --git a/libguile/keywords.c b/libguile/keywords.c
index ffb4dbbbf..019641a02 100644
--- a/libguile/keywords.c
+++ b/libguile/keywords.c
@@ -35,23 +35,21 @@
 #include "ports.h"
 #include "strings.h"
 #include "symbols.h"
+#include "threads.h"
 
-#include "keywords.h"
+#include "keywords-internal.h"
 
 
 
 static SCM keyword_obarray;
 
-#define SCM_KEYWORDP(x) (SCM_HAS_TYP7 (x, scm_tc7_keyword))
-#define SCM_KEYWORD_SYMBOL(x) (SCM_CELL_OBJECT_1 (x))
-
 SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0, 
             (SCM obj),
            "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
            "@code{#f}.")
 #define FUNC_NAME s_scm_keyword_p
 {
-  return scm_from_bool (SCM_KEYWORDP (obj));
+  return scm_from_bool (scm_is_keyword (obj));
 }
 #undef FUNC_NAME
 
@@ -66,12 +64,16 @@ SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 
0,
 
   scm_dynwind_begin (0);
   scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
-  /* Note: `scm_cell' and `scm_hashq_set_x' can raise an out-of-memory
+  /* Note: allocating and `scm_hashq_set_x' can raise an out-of-memory
      error.  */
   keyword = scm_hashq_ref (keyword_obarray, symbol, SCM_BOOL_F);
   if (scm_is_false (keyword))
     {
-      keyword = scm_cell (scm_tc7_keyword, SCM_UNPACK (symbol));
+      struct scm_keyword *kw = scm_allocate_tagged (SCM_I_CURRENT_THREAD,
+                                                    sizeof (*kw));
+      kw->tag = scm_tc7_keyword;
+      kw->symbol = symbol;
+      keyword = scm_from_keyword (kw);
       scm_hashq_set_x (keyword_obarray, symbol, keyword);
     }
   scm_dynwind_end ();
@@ -85,16 +87,10 @@ SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 
0,
 #define FUNC_NAME s_scm_keyword_to_symbol
 {
   SCM_VALIDATE_KEYWORD (1, keyword);
-  return SCM_KEYWORD_SYMBOL (keyword);
+  return scm_to_keyword (keyword)->symbol;
 }
 #undef FUNC_NAME
 
-int
-scm_is_keyword (SCM val)
-{
-  return SCM_KEYWORDP (val);
-}
-
 SCM
 scm_from_locale_keyword (const char *name)
 {
diff --git a/libguile/keywords.h b/libguile/keywords.h
index cb8598d8b..e7ef38771 100644
--- a/libguile/keywords.h
+++ b/libguile/keywords.h
@@ -1,7 +1,7 @@
 #ifndef SCM_KEYWORDS_H
 #define SCM_KEYWORDS_H
 
-/* Copyright 1995-1996,1999-2001,2006,2008,2015,2018-2019
+/* Copyright 1995-1996,1999-2001,2006,2008,2015,2018-2019,2025
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -24,6 +24,7 @@
 
 #include <libguile/error.h>
 #include <libguile/snarf.h>
+#include <libguile/scm.h>
 
 
 
@@ -31,15 +32,17 @@ SCM_API SCM scm_keyword_p (SCM obj);
 SCM_API SCM scm_symbol_to_keyword (SCM symbol);
 SCM_API SCM scm_keyword_to_symbol (SCM keyword);
 
-SCM_API int scm_is_keyword (SCM val);
+static inline int
+scm_is_keyword (SCM x)
+{
+  return SCM_HAS_TYP7 (x, scm_tc7_keyword);
+}
+
 SCM_API SCM scm_from_locale_keyword (const char *name);
 SCM_API SCM scm_from_locale_keywordn (const char *name, size_t len);
 SCM_API SCM scm_from_latin1_keyword (const char *name);
 SCM_API SCM scm_from_utf8_keyword (const char *name);
 
-#define SCM_VALIDATE_KEYWORD(pos, v) \
-  SCM_MAKE_VALIDATE_MSG (pos, v, KEYWORDP, "keyword")
-
 #define SCM_KEYWORD(c_name, scheme_name) \
 SCM_SNARF_HERE(static SCM c_name) \
 SCM_SNARF_INIT(c_name = scm_from_utf8_keyword (scheme_name))
@@ -60,8 +63,4 @@ SCM_API void
 scm_c_bind_keyword_arguments (const char *subr, SCM rest,
                               scm_t_keyword_arguments_flags flags, ...);
 
-#define SCM_I_KEYWORD_HASH(x) scm_i_symbol_hash (SCM_CELL_OBJECT_1 (x))
-
-SCM_INTERNAL void scm_init_keywords (void);
-
 #endif  /* SCM_KEYWORDS_H */

Reply via email to