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

allow to construct BytesMut with custom alignment #601

Closed
wants to merge 2 commits into from
Closed

allow to construct BytesMut with custom alignment #601

wants to merge 2 commits into from

Conversation

Wireless4024
Copy link

added BytesMut::with_capacity_aligned to allocate BytesMut with custom alignment

fix #600

Comment on lines +193 to +198
// # Safety
// + as long as `capacity % align == 0` it prevent `RawVec::drop` to cause `SIGSEGV`
// because `RawVec::drop` will create new Layout during free,
// `RawVec<u8>` will use `Layout::<u8>::array(capacity)`
// to free instead of our alignment
Vec::from_raw_parts(ptr, 0, capacity)
Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation for from_raw_parts says the following:

T needs to have the same alignment as what ptr was allocated with. (T having a less strict alignment is not sufficient, the alignment really needs to be equal to satisfy the dealloc requirement that memory must be allocated and deallocated with the same layout.)

Since the vector is using alignment 1, passing it a pointer of larger alignment is incorrect.

Copy link
Author

@Wireless4024 Wireless4024 Feb 22, 2023

Choose a reason for hiding this comment

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

ok, I see errors in miri.
and now I don't think it's possible without custom drop handling,
like making list of structs with alignment by list of power of two e.g. #[repr(align($align))] struct Align$align([0u8;$align]) and then pass custom struct on BytesMut::drop to make it aligned, and should I make it?
if you don't think please close my pr and my last question: can you make BytesMut::from_vec public to allow me to pass my illegal vec please?

let ptr = std::alloc::alloc(layout);
if ptr.is_null() {
// handle null ptr here because it might cause a problem at `BytesMut::from_vec`
panic!("Not enough memory");
Copy link

Choose a reason for hiding this comment

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

You should call alloc::alloc::handle_alloc_error to handle OOM errors, since panicking may cause unexpected results in low-memory environments.

Copy link
Author

Choose a reason for hiding this comment

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

ok thanks.

@Wireless4024
Copy link
Author

Close for now. I have no idea to archive aligned drop and ensure share ref able to drop aligned yet.
I'll reopen this pr later when I can do it.

If anyone have idea please comment 😄 .

@JackKelly
Copy link

My use-case is the same as yours, @Wireless4024: I need an aligned buffer to use O_DIRECT.

I just submitted PR #684, which gives one way in which it might be possible to create a Bytes with arbitrary alignment.

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.

Please allow to construct BytesMut with custom alignment
4 participants