Skip to content

Revise TARPL's description for allocating 0 bytes #27434

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 1 commit into from
Aug 6, 2015
Merged

Revise TARPL's description for allocating 0 bytes #27434

merged 1 commit into from
Aug 6, 2015

Conversation

jeehoonkang
Copy link
Contributor

In Section 3.2, TARPL says that "standard allocators (including jemalloc, the one used by default in Rust) generally consider passing in 0 for the size of an allocation as Undefined Behaviour."
However, the C standard and jemalloc manual says allocating zero bytes
should succeed:

  • C11 7.22.3 paragraph 1: "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
  • jemalloc manual: "The malloc and calloc functions return a pointer to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM."
    • Note that the description for allocm says "Behavior is undefined if size is 0," but it is an experimental API.

r? @gankro

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @gankro (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@Gankra
Copy link
Contributor

Gankra commented Jul 31, 2015

Ah, thanks for the info (I can't even remember where I got that info... possibly just inferred it from existing patterns/comments)! I think I'd still explicitly call out allocating as an annoying detail. In particular if we used uniform allocating code then 0 allocations might give back null and we'd abort with OOM.

@jeehoonkang
Copy link
Contributor Author

  • I totally agree 0 allocations are a kind of headache..
  • But I am not sure how do you mean by "OOM." Is it out-of-memory? Then 0 allocation is not particularly related to OOM, I think.

@Gankra
Copy link
Contributor

Gankra commented Jul 31, 2015

@jeehoonkang "general" code checks for the allocator yielding null to identify an out-of-memory condition. If the allocator can return null on zero-sized allocations (which logically always succeed) we'll spuriously abort the program in that case. Regardless we don't want to bother the allocator for something we know the answer for.

@jeehoonkang
Copy link
Contributor Author

Ah, I see! thank you.

@bors
Copy link
Collaborator

bors commented Aug 4, 2015

☔ The latest upstream changes (presumably #27444) made this pull request unmergeable. Please resolve the merge conflicts.

@Gankra
Copy link
Contributor

Gankra commented Aug 4, 2015

@jeehoonkang we moved everything to src/doc/nomicon -- if you git pull --rebase it should Just Work because git understands this.

@jeehoonkang
Copy link
Contributor Author

@gankro Thank you for noticing me!

I agree with your comment "I think I'd still explicitly call out allocating as an annoying detail.", so I revised my PR. But I think I cannot fully grasp what you intended to write in this place. Please review :-)

@@ -85,8 +85,8 @@ support values.
Safe code need not worry about ZSTs, but *unsafe* code must be careful about the
consequence of types with no size. In particular, pointer offsets are no-ops,
and standard allocators (including jemalloc, the one used by default in Rust)
generally consider passing in `0` for the size of an allocation as Undefined
Behaviour.
generally return `nullptr` when a zero-sized allocation is requested, which is
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd replace "generally" with "may" here based on the documentation you linked.

In Section 3.2, TARPL says that "standard allocators (including jemalloc, the one used by default in Rust) generally consider passing in 0 for the size of an allocation as Undefined Behaviour."
However, the C standard and jemalloc manual says allocating zero bytes
should succeed:

- C11 7.22.3 paragraph 1: "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
- [jemalloc manual](http://www.freebsd.org/cgi/man.cgi?query=jemalloc&sektion=3): "The malloc and calloc functions return a	pointer	to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM."
    + Note that the description for `allocm` says "Behavior	is undefined if	size is 0," but it is an experimental API.
@jeehoonkang
Copy link
Contributor Author

Revised :-)

@Gankra
Copy link
Contributor

Gankra commented Aug 6, 2015

@bors r+ rollup

Thanks!

@bors
Copy link
Collaborator

bors commented Aug 6, 2015

📌 Commit 9bfb8d3 has been approved by Gankro

@bors
Copy link
Collaborator

bors commented Aug 6, 2015

⌛ Testing commit 9bfb8d3 with merge 83f2667...

bors added a commit that referenced this pull request Aug 6, 2015
In Section 3.2, TARPL says that "standard allocators (including jemalloc, the one used by default in Rust) generally consider passing in 0 for the size of an allocation as Undefined Behaviour."
However, the C standard and jemalloc manual says allocating zero bytes
should succeed:

- C11 7.22.3 paragraph 1: "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
- [jemalloc manual](http://www.freebsd.org/cgi/man.cgi?query=jemalloc&sektion=3): "The malloc and calloc functions return a	pointer	to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM."
    + Note that the description for `allocm` says "Behavior	is undefined if	size is 0," but it is an experimental API.

r? @gankro
@bors bors merged commit 9bfb8d3 into rust-lang:master Aug 6, 2015
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.

4 participants