Skip to content
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

Structured memory deallocation using blocks #38

Merged
merged 3 commits into from
Jun 6, 2020

Conversation

chrisseaton
Copy link
Contributor

Fiddle::Pointer.new(size, free) do |pointer|
  ...
end
  • Easy to use best-practice
  • Helps people write correct code for the most common cases
  • Protect against leaks (free if there's an exception, or even if someone uses a trace point to get hold of the object)
  • Protect against deallocation latency (free as soon as you're done)
  • Protect against non-locality (free while it's still in cache from your using it)

return obj;
if (rb_block_given_p()) {
if (f == NULL) {
rb_raise(rb_eFiddleError, "a free function must be supplied to Fiddle::Pointer.malloc when it is called with a block");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use rb_eArgError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -261,7 +271,14 @@ rb_fiddle_ptr_s_malloc(int argc, VALUE argv[], VALUE klass)
obj = rb_fiddle_ptr_malloc(s,f);
if (wrap) RPTR_DATA(obj)->wrap[1] = wrap;

return obj;
if (rb_block_given_p()) {
if (f == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (f == NULL) {
if (!f) {

Because we don't use == NULL in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


def test_malloc_block_no_free
assert_raise Fiddle::DLError do
Pointer.malloc(10) { |ptr| raise }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove needless raise? raise in assert_raise may confuse us.

Suggested change
Pointer.malloc(10) { |ptr| raise }
Pointer.malloc(10) { |ptr| }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@chrisseaton
Copy link
Contributor Author

Sorry I didn't use 'commit suggestion' I hadn't seen that before in GitHub and didn't realise it was an option.

@chrisseaton chrisseaton requested a review from kou June 4, 2020 14:28
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
No problem.

@kou kou merged commit 290dfac into ruby:master Jun 6, 2020
@chrisseaton chrisseaton deleted the malloc-block branch June 10, 2020 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants