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

doc: ArrayBuffer and Buffer documentation #256

Closed
wants to merge 2 commits into from

Conversation

kfarnung
Copy link
Contributor

@kfarnung kfarnung commented May 2, 2018

  • Also fixed a typo in the draft message

doc/buffer.md Outdated
valid for the lifetime of the Buffer object. Since the Buffer is subject to
garbage collection this overload is only suitable for data which is static and
never needs to be freed.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can someone fact-check this? I did some digging in the code and this appears to be true.

If so, it's a departure from NAN which would take ownership of the data and later free it. Something to keep in mind for the conversion script, if someone was depending on this behavior there would be a memory leak in the converted code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened #258 to discuss

@kfarnung kfarnung changed the title doc: Buffer documentation doc: ArrayBuffer and Buffer documentation May 2, 2018

Wraps the provided external data into a new `ArrayBuffer` object.

The `ArrayBuffer` object does not assume ownership for the data and expects it
Copy link
Member

Choose a reason for hiding this comment

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

It is not more accurate that it effectively does take ownership, taking responsibility to run the finalizer to provided to delete the data when the class is deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I toyed with the wording here a bit, but since the module code is still responsible for cleanup I feel like it's more accurate to say that ownership is never transferred. The caller is merely lending the data to the ArrayBuffer. Doing a bit more reading, it seems to align pretty well with the way V8 describes these "externalized" ArrayBuffer objects.

https://v8.paulfryzel.com/docs/master/classv8_1_1_array_buffer.html#a166848a34653afd4502e4cc33443815d

After ArrayBuffer had been externalized, it does no longer own the memory block. The caller should take steps to free memory when it is no longer needed.

Copy link
Member

Choose a reason for hiding this comment

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

@kfarnung did we close on what we wanted to do? It would be good to get this landed.

The `ArrayBuffer` object does not assume ownership for the data and expects it
to be valid for the lifetime of the object. Since the `ArrayBuffer` is subject
to garbage collection this overload is only suitable for data which is static
and never needs to be freed.
Copy link
Member

Choose a reason for hiding this comment

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

I like your description here.

I know we discussed doing a free, but thinking about it I don't like us trying to free an object that we don't know was allocated with malloc or new.

What would be good to figure out though is how to avoid conversion errors from NaN which result in leaks. I'd be tempted to remove or deprecate this one and force callers to indicate they don't need finalization by passing a null to one of the other methods. Since the wrapper is not part of the abi contract I think we could delete the method and provide instructions on what to use instead.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just deprecate as opposed to remove with the doc saying "don't use"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My only concern about "doc deprecated" is that it requires people to see the doc when converting their code.

@mhdawson
Copy link
Member

@kfarnung can you take a pass through to update to what you think we should do for now. Would be good to get an initial version out and then we can iterate.

@kfarnung
Copy link
Contributor Author

Will do, I was on vacation for a week and I'm just getting back up to speed.

doc/buffer.md Outdated

Wraps the provided external data into a new `Buffer` object.

The Bu`Buffer`ffer object does not assume ownership for the data and expects it
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a typo here.

doc/buffer.md Outdated

Allocates a new `Buffer` object and copies the provided external data into it.

The `Buffer` object does not assume ownership for the data and expects it to be
Copy link
Contributor

@gabrielschulhof gabrielschulhof Jun 21, 2018

Choose a reason for hiding this comment

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

I don't get it. If it copies the data, how does it not assume ownership of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Buffer doesn't assume ownership for the data you're providing to the Copy call, it copies it to memory that it does own. This was supposed to contrast with the other Buffer factory functions, but maybe it's not necessary?

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

mhdawson pushed a commit that referenced this pull request Jul 4, 2018
PR-URL: #256
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@mhdawson
Copy link
Member

mhdawson commented Jul 4, 2018

Landed as 11697fc

@mhdawson mhdawson closed this Jul 4, 2018
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
PR-URL: nodejs/node-addon-api#256
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
PR-URL: nodejs/node-addon-api#256
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
PR-URL: nodejs/node-addon-api#256
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#256
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
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.

3 participants