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

docs: Changed docs for size_of to describe size as a stride offset #33335

Merged
merged 1 commit into from
May 3, 2016

Conversation

cramertj
Copy link
Member

@cramertj cramertj commented May 2, 2016

Current documentation for std::mem::size_of is ambiguous, and the documentation for std::intrinsics::size_of incorrectly defines size.

This fix re-defines size as the offset in bytes between successive instances of a type, as described in LLVM's getTypeAllocSize.

Fixes: #33266

Current description of `std::mem::size_of` is ambiguous, and the
`std::intrinsics::size_of` description incorrectly defines size
as the number of bytes necessary to exactly overwrite a value,
not including the padding between elements necessary in a vector
or structure.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (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. Due to 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.

@durka
Copy link
Contributor

durka commented May 2, 2016

If I only have one T, what does "between successive values" mean? Does Box::new(T) allocate mem::size_of::<T>() bytes or fewer?

@cramertj
Copy link
Member Author

cramertj commented May 2, 2016

@durka I can't give you a definitive answer on that-- perhaps @pnkfelix can shed some light.

I would speculate, however, that it would allocate exactly LLVM's getTypeAllocSize, seeing as this seems to be literally the allocation size of the type. This would guarantee alignment for future allocations of the type, although I'd imagine the actual heap-alignment is more heavily dependent upon the allocator in question. This would also fall in line with @pnkfelix's comment that the behavior of std::mem::size_of resembles that of sizeof(T) in C/C++, where it is common to see expressions such as malloc(sizeof(T)).

Note, too, that LLVM also provides a getTypeStoreSize specifying the number of bytes overwritten by the object in question, a more traditional/intuitive understanding of size_of.

@alexcrichton
Copy link
Member

@bors: r+ 38c8836

Thanks!

Does Box::new(T) allocate mem::size_of::() bytes or fewer?

Box allocates size_of::<T>, so it'll allocate the padding bytes as well

@durka
Copy link
Contributor

durka commented May 2, 2016

@alexcrichton I think the answer to my question should go into the size_of docs as well

@alexcrichton
Copy link
Member

Sounds like something that'd be more appropriate for Box rather than the core mem::size_of as it's an implementation detail of that particular pointer type.

Manishearth added a commit to Manishearth/rust that referenced this pull request May 2, 2016
docs: Changed docs for `size_of` to describe size as a stride offset

Current documentation for `std::mem::size_of` is ambiguous, and the documentation for `std::intrinsics::size_of` incorrectly defines size.

This fix re-defines size as the offset in bytes between successive instances of a type, as described in LLVM's [getTypeAllocSize](http://llvm.org/docs/doxygen/html/classllvm_1_1DataLayout.html#a1d6fcc02e91ba24510aba42660c90e29).

Fixes: rust-lang#33266
bors added a commit that referenced this pull request May 2, 2016
Rollup of 14 pull requests

- Successful merges: #32756, #33129, #33225, #33260, #33309, #33320, #33323, #33324, #33325, #33330, #33332, #33334, #33335, #33346
- Failed merges:
Manishearth added a commit to Manishearth/rust that referenced this pull request May 3, 2016
docs: Changed docs for `size_of` to describe size as a stride offset

Current documentation for `std::mem::size_of` is ambiguous, and the documentation for `std::intrinsics::size_of` incorrectly defines size.

This fix re-defines size as the offset in bytes between successive instances of a type, as described in LLVM's [getTypeAllocSize](http://llvm.org/docs/doxygen/html/classllvm_1_1DataLayout.html#a1d6fcc02e91ba24510aba42660c90e29).

Fixes: rust-lang#33266
bors added a commit that referenced this pull request May 3, 2016
Rollup of 14 pull requests

- Successful merges: #32756, #33129, #33225, #33260, #33309, #33320, #33323, #33324, #33325, #33330, #33332, #33334, #33335, #33346
- Failed merges:
@bors bors merged commit 38c8836 into rust-lang:master May 3, 2016
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.

docs: size_of is implemented as stride, but claims otherwise
5 participants