-
Notifications
You must be signed in to change notification settings - Fork 45
Add a "pinning" reference #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #include <fiddle.h> | ||
|
|
||
| VALUE rb_cPinned; | ||
| VALUE rb_eFiddleClearedReferenceError; | ||
|
|
||
| struct pinned_data { | ||
| VALUE ptr; | ||
| }; | ||
|
|
||
| static void | ||
| pinned_mark(void *ptr) | ||
| { | ||
| struct pinned_data *data = (struct pinned_data*)ptr; | ||
| /* Ensure reference is pinned */ | ||
| if (data->ptr) { | ||
| rb_gc_mark(data->ptr); | ||
| } | ||
| } | ||
|
|
||
| static size_t | ||
| pinned_memsize(const void *ptr) | ||
| { | ||
| return sizeof(struct pinned_data); | ||
| } | ||
|
|
||
| static const rb_data_type_t pinned_data_type = { | ||
| "fiddle/pinned", | ||
| {pinned_mark, xfree, pinned_memsize, }, | ||
| 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | ||
| }; | ||
|
|
||
| static VALUE | ||
| allocate(VALUE klass) | ||
| { | ||
| struct pinned_data *data; | ||
| VALUE obj = TypedData_Make_Struct(klass, struct pinned_data, &pinned_data_type, data); | ||
| data->ptr = 0; | ||
| return obj; | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: | ||
| * Fiddle::Pinned.new(object) => pinned_object | ||
| * | ||
| * Create a new pinned object reference. The Fiddle::Pinned instance will | ||
| * prevent the GC from moving +object+. | ||
| */ | ||
| static VALUE | ||
| initialize(VALUE self, VALUE ref) | ||
| { | ||
| struct pinned_data *data; | ||
| TypedData_Get_Struct(self, struct pinned_data, &pinned_data_type, data); | ||
| RB_OBJ_WRITE(self, &data->ptr, ref); | ||
| return self; | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: ref | ||
| * | ||
| * Return the object that this pinned instance references. | ||
| */ | ||
| static VALUE | ||
| ref(VALUE self) | ||
| { | ||
| struct pinned_data *data; | ||
| TypedData_Get_Struct(self, struct pinned_data, &pinned_data_type, data); | ||
| if (data->ptr) { | ||
| return data->ptr; | ||
| } else { | ||
| rb_raise(rb_eFiddleClearedReferenceError, "`ref` called on a cleared object"); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: clear | ||
| * | ||
| * Clear the reference to the object this is pinning. | ||
| */ | ||
| static VALUE | ||
| clear(VALUE self) | ||
| { | ||
| struct pinned_data *data; | ||
| TypedData_Get_Struct(self, struct pinned_data, &pinned_data_type, data); | ||
| data->ptr = 0; | ||
| return self; | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: cleared? | ||
| * | ||
| * Returns true if the reference has been cleared, otherwise returns false. | ||
| */ | ||
| static VALUE | ||
| cleared_p(VALUE self) | ||
| { | ||
| struct pinned_data *data; | ||
| TypedData_Get_Struct(self, struct pinned_data, &pinned_data_type, data); | ||
| if (data->ptr) { | ||
| return Qfalse; | ||
| } else { | ||
| return Qtrue; | ||
| } | ||
| } | ||
|
|
||
| extern VALUE rb_eFiddleError; | ||
|
|
||
| void | ||
| Init_fiddle_pinned(void) | ||
| { | ||
| rb_cPinned = rb_define_class_under(mFiddle, "Pinned", rb_cObject); | ||
| rb_define_alloc_func(rb_cPinned, allocate); | ||
| rb_define_method(rb_cPinned, "initialize", initialize, 1); | ||
| rb_define_method(rb_cPinned, "ref", ref, 0); | ||
| rb_define_method(rb_cPinned, "clear", clear, 0); | ||
| rb_define_method(rb_cPinned, "cleared?", cleared_p, 0); | ||
|
|
||
| /* | ||
| * Document-class: Fiddle::ClearedReferenceError | ||
| * | ||
| * Cleared reference exception | ||
| */ | ||
| rb_eFiddleClearedReferenceError = rb_define_class_under(mFiddle, "ClearedReferenceError", rb_eFiddleError); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # frozen_string_literal: true | ||
| begin | ||
| require_relative 'helper' | ||
| rescue LoadError | ||
| end | ||
|
|
||
| module Fiddle | ||
| class TestPinned < Fiddle::TestCase | ||
| def test_pin_object | ||
| x = Object.new | ||
| pinner = Pinned.new x | ||
| assert_same x, pinner.ref | ||
| end | ||
|
|
||
| def test_clear | ||
| pinner = Pinned.new Object.new | ||
| refute pinner.cleared? | ||
| pinner.clear | ||
| assert pinner.cleared? | ||
| ex = assert_raise(Fiddle::ClearedReferenceError) do | ||
| pinner.ref | ||
| end | ||
| assert_match "called on", ex.message | ||
| end | ||
| end | ||
| end | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we want a more explicit API in MRI to pin? You and I understand why this would pin the object, not sure everyone would.