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

Add integration with zerocopy crate #253

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sivadeilra
Copy link

The zerocopy crate provides traits and functions for safely transmuting types to/from bytes. It is an important building block for high-performance serialization in many designs. It is also a very well-maintained crate with high standards for quality and its maintainers are actively engaged with the Rust Project on advancing the goals of the Safe Transmute working group.

The zerocopy crate defines the FromZeroes trait, which specifies that a type can be safely constructed from a buffer containing an all-zeroes bit pattern.

This PR adds two new methods to Bump: new_zeroed and new_slice_zeroed. new_zeroed allocates space for T (where T: FromZeroes) and returns &mut T. This avoids the "placement new" problem in Rust, which causes problems when attempting to allocate large types in the heap, such as [u8; 0x10000]. For most containers, such as Box, the type is briefly constructed in a temporary on the stack, then a heap allocation is done, then the value is moved into the heap allocation. If T is large enough, it can cause stack overflow.

The Bump::new_zeroed function avoids this problem by simply allocating space for T directly in the heap, then filling it with zeroes, then casting the allocation as &mut T and returning it. The FromZeroes constraint ensures that this is sound.

The new_slice_zeroed function similarly allows allocating slices directly in a Bump.

Arlie Davis added 2 commits June 7, 2024 07:40
The zerocopy crate provides traits and functions for _safely_
transmuting types to/from bytes. It is an important building block
for high-performance serialization in many designs. It is also a very
well-maintained crate with high standards for quality and its maintainers
are actively engaged with the Rust Project on advancing the goals of the
Safe Transmute working group.

The `zerocopy` crate defines the `FromZeroes` trait, which specifies
that a type can be safely constructed from a buffer containing an
all-zeroes bit pattern.

This PR adds two new methods to `Bump`: `new_zeroed` and `new_slice_zeroed`.
`new_zeroed` allocates space for `T` (where `T: FromZeroes`) and
returns `&mut T`. This avoids the "placement new" problem in Rust,
which causes problems when attempting to allocate large types in the
heap, such as `[u8; 0x10000]`. For most containers, such as `Box`,
the type is briefly constructed in a temporary on the stack, then
a heap allocation is done, then the value is moved into the heap
allocation. If `T` is large enough, it can cause stack overflow.

The `Bump::new_zeroed` function avoids this problem by simply allocating
space for `T` directly in the heap, then filling it with zeroes, then
casting the allocation as `&mut T` and returning it. The `FromZeroes`
constraint ensures that this is sound.

The `new_slice_zeroed` function similarly allows allocating slices
directly in a Bump.
Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Happy to take a PR that adds a zeroing allocation method, but I don't want to take a dependency on any new crates.

@sivadeilra
Copy link
Author

Happy to take a PR that adds a zeroing allocation method, but I don't want to take a dependency on any new crates.

It's an optional dependency, though, and the zerocopy crate is part of the Safe Transmute initiative.

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