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

commit c7fe77de0e34336cf577485b85e2c65bc2a0d133
Author: Andy Wingo <wi...@igalia.com>
AuthorDate: Sun May 4 12:10:03 2025 +0200

    Add gc_heap_contains API
---
 api/gc-api.h | 2 ++
 src/bdw.c    | 5 +++++
 src/mmc.c    | 7 +++++++
 src/pcc.c    | 8 ++++++++
 src/semi.c   | 7 +++++++
 5 files changed, 29 insertions(+)

diff --git a/api/gc-api.h b/api/gc-api.h
index b040b7d0a..e34e0d57e 100644
--- a/api/gc-api.h
+++ b/api/gc-api.h
@@ -59,6 +59,8 @@ GC_API_ void* gc_call_without_gc(struct gc_mutator *mut, 
void* (*f)(void*),
 GC_API_ void gc_collect(struct gc_mutator *mut,
                         enum gc_collection_kind requested_kind);
 
+GC_API_ int gc_heap_contains(struct gc_heap *heap, struct gc_ref ref);
+
 static inline void gc_update_alloc_table(struct gc_ref obj, size_t size,
                                          enum gc_allocation_kind kind) 
GC_ALWAYS_INLINE;
 static inline void gc_update_alloc_table(struct gc_ref obj, size_t size,
diff --git a/src/bdw.c b/src/bdw.c
index f991437fd..1403d64f2 100644
--- a/src/bdw.c
+++ b/src/bdw.c
@@ -187,6 +187,11 @@ void gc_collect(struct gc_mutator *mut,
   }
 }
 
+int gc_heap_contains(struct gc_heap *heap, struct gc_ref ref) {
+  GC_ASSERT(gc_ref_is_heap_object(ref));
+  return GC_base(gc_ref_heap_object(ref)) != 0;
+}
+
 int gc_object_is_old_generation_slow(struct gc_mutator *mut,
                                      struct gc_ref obj) {
   return 0;
diff --git a/src/mmc.c b/src/mmc.c
index 6a5f13bc2..6055905be 100644
--- a/src/mmc.c
+++ b/src/mmc.c
@@ -844,6 +844,13 @@ gc_collect(struct gc_mutator *mut, enum gc_collection_kind 
kind) {
   trigger_collection(mut, kind);
 }
 
+int
+gc_heap_contains(struct gc_heap *heap, struct gc_ref ref) {
+  GC_ASSERT(gc_ref_is_heap_object(ref));
+  return nofl_space_contains(heap_nofl_space(heap), ref)
+    || large_object_space_contains(heap_large_object_space(heap), ref);
+}
+
 int*
 gc_safepoint_flag_loc(struct gc_mutator *mut) {
   return &mutator_heap(mut)->collecting;
diff --git a/src/pcc.c b/src/pcc.c
index 19d0a1ef3..4419f519f 100644
--- a/src/pcc.c
+++ b/src/pcc.c
@@ -965,6 +965,14 @@ void gc_collect(struct gc_mutator *mut, enum 
gc_collection_kind kind) {
   trigger_collection(mut, kind);
 }
 
+int gc_heap_contains(struct gc_heap *heap, struct gc_ref ref) {
+  GC_ASSERT(gc_ref_is_heap_object(ref));
+  return (GC_GENERATIONAL
+          ? (new_space_contains(heap, ref) || old_space_contains(heap, ref))
+          : copy_space_contains(heap_mono_space(heap), ref))
+    || large_object_space_contains(heap_large_object_space(heap), ref);
+}
+
 static void* allocate_large(struct gc_mutator *mut, size_t size) {
   struct gc_heap *heap = mutator_heap(mut);
   struct large_object_space *space = heap_large_object_space(heap);
diff --git a/src/semi.c b/src/semi.c
index 27037df66..57bb762c7 100644
--- a/src/semi.c
+++ b/src/semi.c
@@ -468,6 +468,13 @@ void gc_collect(struct gc_mutator *mut,
   collect(mut);
 }
 
+int
+gc_heap_contains(struct gc_heap *heap, struct gc_ref ref) {
+  GC_ASSERT(gc_ref_is_heap_object(ref));
+  return semi_space_contains(heap_semi_space(heap), ref)
+    || large_object_space_contains(heap_large_object_space(heap), ref);
+}
+
 int gc_object_is_old_generation_slow(struct gc_mutator *mut,
                                      struct gc_ref obj) {
   return 0;

Reply via email to