summaryrefslogtreecommitdiff
path: root/weakmap.c
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2025-12-30 16:03:35 -0500
committerPeter Zhu <peter@peterzhu.ca>2026-01-04 09:02:40 -0500
commit6939f03f4cec6c50cbc1d214a38cdcdcf1d3f705 (patch)
treedf8e767282a0f6a9dc1feae4152249228ac1cb23 /weakmap.c
parent1b3382cbab83f0dbf21b0d3683d4ab5335f6c342 (diff)
Add field handle_weak_references to TypedData
This commit adds a field handle_weak_references to rb_data_type_struct for the callback when handling weak references. This avoids TypedData objects from needing to expose their rb_data_type_struct and weak references function.
Diffstat (limited to 'weakmap.c')
-rw-r--r--weakmap.c84
1 files changed, 42 insertions, 42 deletions
diff --git a/weakmap.c b/weakmap.c
index 7b6f27ce2b..b027604f5e 100644
--- a/weakmap.c
+++ b/weakmap.c
@@ -112,6 +112,26 @@ wmap_compact(void *ptr)
}
}
+static int
+rb_wmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
+{
+ if (rb_gc_handle_weak_references_alive_p(key) &&
+ rb_gc_handle_weak_references_alive_p(val)) {
+ return ST_CONTINUE;
+ }
+ else {
+ return ST_DELETE;
+ }
+}
+
+static void
+wmap_handle_weak_references(void *ptr)
+{
+ struct weakmap *w = ptr;
+
+ st_foreach(w->table, rb_wmap_handle_weak_references_i, (st_data_t)0);
+}
+
const rb_data_type_t rb_weakmap_type = {
"weakmap",
{
@@ -119,6 +139,7 @@ const rb_data_type_t rb_weakmap_type = {
wmap_free,
wmap_memsize,
wmap_compact,
+ wmap_handle_weak_references,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
};
@@ -140,27 +161,6 @@ static const struct st_hash_type wmap_hash_type = {
wmap_hash,
};
-static int
-rb_wmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
-{
- if (rb_gc_handle_weak_references_alive_p(key) &&
- rb_gc_handle_weak_references_alive_p(val)) {
- return ST_CONTINUE;
- }
- else {
- return ST_DELETE;
- }
-}
-
-void
-rb_wmap_handle_weak_references(VALUE self)
-{
- struct weakmap *w;
- TypedData_Get_Struct(self, struct weakmap, &rb_weakmap_type, w);
-
- st_foreach(w->table, rb_wmap_handle_weak_references_i, (st_data_t)0);
-}
-
static VALUE
wmap_allocate(VALUE klass)
{
@@ -588,13 +588,33 @@ wkmap_compact(void *ptr)
}
}
-const rb_data_type_t rb_weakkeymap_type = {
+static int
+rb_wkmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
+{
+ if (rb_gc_handle_weak_references_alive_p(key)) {
+ return ST_CONTINUE;
+ }
+ else {
+ return ST_DELETE;
+ }
+}
+
+static void
+wkmap_handle_weak_references(void *ptr)
+{
+ struct weakkeymap *w = ptr;
+
+ st_foreach(w->table, rb_wkmap_handle_weak_references_i, (st_data_t)0);
+}
+
+static const rb_data_type_t rb_weakkeymap_type = {
"weakkeymap",
{
wkmap_mark,
wkmap_free,
wkmap_memsize,
wkmap_compact,
+ wkmap_handle_weak_references,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
};
@@ -621,26 +641,6 @@ static const struct st_hash_type wkmap_hash_type = {
wkmap_hash,
};
-static int
-rb_wkmap_handle_weak_references_i(st_data_t key, st_data_t val, st_data_t arg)
-{
- if (rb_gc_handle_weak_references_alive_p(key)) {
- return ST_CONTINUE;
- }
- else {
- return ST_DELETE;
- }
-}
-
-void
-rb_wkmap_handle_weak_references(VALUE self)
-{
- struct weakkeymap *w;
- TypedData_Get_Struct(self, struct weakkeymap, &rb_weakkeymap_type, w);
-
- st_foreach(w->table, rb_wkmap_handle_weak_references_i, (st_data_t)0);
-}
-
static VALUE
wkmap_allocate(VALUE klass)
{