diff options
author | Aaron Patterson <[email protected]> | 2020-08-02 14:26:40 -0700 |
---|---|---|
committer | Sutou Kouhei <[email protected]> | 2020-11-18 09:05:13 +0900 |
commit | 307388ea19f5c9d1c8c417d1979c40d970b420a2 (patch) | |
tree | fbf713aa94ddc4314fbe755c6bdc7541d10a6925 /ext/fiddle/fiddle.c | |
parent | e2dfc0c26b1f3d3517002ca2645d1b67847fe518 (diff) |
[ruby/fiddle] Add a "pinning" reference (#44)
* Add a "pinning" reference
A `Fiddle::Pinned` objects will prevent the objects they point to from
moving. This is useful in the case where you need to pass a reference
to a C extension that keeps the address in a global and needs the
address to be stable.
For example:
```ruby
class Foo
A = "hi" # this is an embedded string
some_c_function A # A might move!
end
```
If `A` moves, then the underlying string buffer may also move.
`Fiddle::Pinned` will prevent the object from moving:
```ruby
class Foo
A = "hi" # this is an embedded string
A_pinner = Fiddle::Pinned.new(A) # :nodoc:
some_c_function A # A can't move because of `Fiddle::Pinned`
end
```
This is a similar strategy to what Graal uses:
https://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/PinnedObject.html#getObject--
* rename global to match exception name
* Introduce generic Fiddle::Error and rearrange error classes
Fiddle::Error is the generic exception base class for Fiddle exceptions.
This commit introduces the class and rearranges Fiddle exceptions to
inherit from it.
https://github.com/ruby/fiddle/commit/ac52d00223
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3780
Diffstat (limited to 'ext/fiddle/fiddle.c')
-rw-r--r-- | ext/fiddle/fiddle.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/ext/fiddle/fiddle.c b/ext/fiddle/fiddle.c index e6435ba3f3..5f2bd5ae20 100644 --- a/ext/fiddle/fiddle.c +++ b/ext/fiddle/fiddle.c @@ -1,9 +1,11 @@ #include <fiddle.h> VALUE mFiddle; +VALUE rb_eFiddleDLError; VALUE rb_eFiddleError; void Init_fiddle_pointer(void); +void Init_fiddle_pinned(void); /* * call-seq: Fiddle.malloc(size) @@ -133,11 +135,18 @@ Init_fiddle(void) mFiddle = rb_define_module("Fiddle"); /* + * Document-class: Fiddle::Error + * + * Generic error class for Fiddle + */ + rb_eFiddleError = rb_define_class_under(mFiddle, "Error", rb_eStandardError); + + /* * Document-class: Fiddle::DLError * * standard dynamic load exception */ - rb_eFiddleError = rb_define_class_under(mFiddle, "DLError", rb_eStandardError); + rb_eFiddleDLError = rb_define_class_under(mFiddle, "DLError", rb_eFiddleError); /* Document-const: TYPE_VOID * @@ -439,5 +448,6 @@ Init_fiddle(void) Init_fiddle_closure(); Init_fiddle_handle(); Init_fiddle_pointer(); + Init_fiddle_pinned(); } /* vim: set noet sws=4 sw=4: */ |