summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorMatt Valentine-House <matt@eightbitraptor.com>2023-03-16 22:23:17 +0000
committerMatt Valentine-House <matt@eightbitraptor.com>2023-03-17 19:20:40 +0000
commit7142328a94c85cc5f23630396f248b32133f87ab (patch)
treeffb4e22f34ada823f09da78be54d2faa5abc2756 /include
parent9fd94d6a0cc7626c8abfc9ba05d6f156927ee8f7 (diff)
downloadruby-7142328a94c85cc5f23630396f248b32133f87ab.tar.gz
[Feature #19406] Allow declarative definition of references
When using rb_data_type_struct to wrap a C struct, that C struct can contain VALUE references to other Ruby objects. If this is the case then one must also define dmark and optionally dcompact callbacks in order to allow these objects to be correctly handled by the GC. This is suboptimal as it requires GC related logic to be implemented by extension developers. This can be a cause of subtle bugs when references are not marked of updated correctly inside these callbacks. This commit provides an alternative approach, useful in the simple case where the C struct contains VALUE members (ie. there isn't any conditional logic, or data structure manipulation required to traverse these references). In this case references can be defined using a declarative syntax as a list of edges (or, pointers to references). A flag can be set on the rb_data_type_struct to notify the GC that declarative references are being used, and a list of those references can be assigned to the dmark pointer instead of a function callback, on the rb_data_type_struct. Macros are also provided for simple declaration of the reference list, and building edges. To avoid having to also find space in the struct to define a length for the references list, I've chosed to always terminate the references list with RUBY_REF_END - defined as UINTPTR_MAX. My assumption is that no single struct will ever be large enough that UINTPTR_MAX is actually a valid reference.
Diffstat (limited to 'include')
-rw-r--r--include/ruby/internal/core/rtypeddata.h9
-rw-r--r--include/ruby/internal/gc.h6
2 files changed, 14 insertions, 1 deletions
diff --git a/include/ruby/internal/core/rtypeddata.h b/include/ruby/internal/core/rtypeddata.h
index bbf208867d..9d91a3e3f1 100644
--- a/include/ruby/internal/core/rtypeddata.h
+++ b/include/ruby/internal/core/rtypeddata.h
@@ -176,7 +176,14 @@ rbimpl_typeddata_flags {
* This flag is mysterious. It seems nobody is currently using it. The
* intention of this flag is also unclear. We need further investigations.
*/
- RUBY_TYPED_PROMOTED1 = RUBY_FL_PROMOTED1 /* THIS FLAG DEPENDS ON Ruby version */
+ RUBY_TYPED_PROMOTED1 = RUBY_FL_PROMOTED1, /* THIS FLAG DEPENDS ON Ruby version */
+
+ /**
+ * This flag determines whether marking and compaction should be carried out
+ * using the dmark/dcompact callback functions or whether we should mark
+ * declaratively using a list of references defined inside the data struct we're wrapping
+ */
+ RUBY_TYPED_DECL_MARKING = RUBY_FL_USER2
};
/**
diff --git a/include/ruby/internal/gc.h b/include/ruby/internal/gc.h
index fb14b34211..bc81883e0e 100644
--- a/include/ruby/internal/gc.h
+++ b/include/ruby/internal/gc.h
@@ -44,6 +44,12 @@
RBIMPL_SYMBOL_EXPORT_BEGIN()
+#define REF_EDGE(s, p) (offsetof(struct s, p))
+#define REFS_LIST_PTR(l) ((RUBY_DATA_FUNC)l)
+#define RUBY_REF_END SIZE_MAX
+#define RUBY_REFERENCES_START(t) static size_t t[] = {
+#define RUBY_REFERENCES_END RUBY_REF_END, };
+
/* gc.c */
RBIMPL_ATTR_COLD()