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

commit fcf8fa3d10d8db84943dd206b461f9366b39e984
Author: Andy Wingo <wi...@pobox.com>
AuthorDate: Sun May 4 15:15:41 2025 +0200

    Reimplement scm_is_mutable_pair using gc_heap_contains
    
    * libguile/pairs.c (scm_to_ref): Copy this stub around again; we should
    fix this...
    (scm_is_mutable_pair): Use gc_heap_contains.
---
 libguile/pairs.c | 21 ++++++++++++++++++++-
 libguile/pairs.h | 26 ++------------------------
 2 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/libguile/pairs.c b/libguile/pairs.c
index 64222b424..f61bc4009 100644
--- a/libguile/pairs.c
+++ b/libguile/pairs.c
@@ -1,4 +1,4 @@
-/* Copyright 1995-1996,2000-2001,2004-2006,2008-2013,2017-2019
+/* Copyright 1995-1996,2000-2001,2004-2006,2008-2013,2017-2019,2025
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -26,6 +26,7 @@
 #include <verify.h>
 
 #include "boolean.h"
+#include "gc-internal.h"
 #include "gsubr.h"
 
 #include "pairs.h"
@@ -74,6 +75,24 @@ scm_cons2 (SCM w, SCM x, SCM y)
   return scm_cons (w, scm_cons (x, y));
 }
 
+static inline struct gc_ref scm_to_ref (SCM scm)
+{
+  return gc_ref (SCM_UNPACK (scm));
+}
+
+int
+scm_is_mutable_pair (SCM x)
+{
+  /* Guile embeds literal pairs into compiled object files.  It's not
+     valid Scheme to mutate literal values.  Two practical reasons to
+     enforce this restriction are to allow literals to share share
+     structure (pairs) with other literals in the compilation unit, and
+     to allow literals containing immediates to be allocated in the
+     read-only, shareable section of the file.  Attempting to mutate a
+     pair in the read-only section would cause a segmentation fault, so
+     to avoid that, we really do need to enforce the restriction.  */
+  return scm_is_pair (x) && gc_heap_contains (the_gc_heap, scm_to_ref (x));
+}
 
 SCM_DEFINE (scm_pair_p, "pair?", 1, 0, 0, 
             (SCM x),
diff --git a/libguile/pairs.h b/libguile/pairs.h
index 617b4c229..4bca40368 100644
--- a/libguile/pairs.h
+++ b/libguile/pairs.h
@@ -1,7 +1,7 @@
 #ifndef SCM_PAIRS_H
 #define SCM_PAIRS_H
 
-/* Copyright 1995-1996,2000-2001,2004,2006,2008-2010,2012,2018
+/* Copyright 1995-1996,2000-2001,2004,2006,2008-2010,2012,2018,2025
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -199,29 +199,7 @@ scm_cdr (SCM x)
 }
 #endif
 
-#ifdef BUILDING_LIBGUILE
-#ifndef HAVE_GC_IS_HEAP_PTR
-static int
-GC_is_heap_ptr (void *ptr)
-{
-  return GC_base (ptr) != NULL;
-}
-#endif
-
-static inline int
-scm_is_mutable_pair (SCM x)
-{
-  /* Guile embeds literal pairs into compiled object files.  It's not
-     valid Scheme to mutate literal values.  Two practical reasons to
-     enforce this restriction are to allow literals to share share
-     structure (pairs) with other literals in the compilation unit, and
-     to allow literals containing immediates to be allocated in the
-     read-only, shareable section of the file.  Attempting to mutate a
-     pair in the read-only section would cause a segmentation fault, so
-     to avoid that, we really do need to enforce the restriction.  */
-  return scm_is_pair (x) && GC_is_heap_ptr (SCM2PTR (x));
-}
-#endif /* BUILDING_LIBGUILE */
+SCM_INTERNAL int scm_is_mutable_pair (SCM x);
 
 SCM_API SCM scm_cons2 (SCM w, SCM x, SCM y);
 SCM_API SCM scm_pair_p (SCM x);

Reply via email to