Skip to content

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.


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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants