-
Notifications
You must be signed in to change notification settings - Fork 290
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
Conversation
// # 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok thanks.
Close for now. I have no idea to archive aligned drop and ensure share ref able to drop aligned yet. If anyone have idea please comment 😄 . |
My use-case is the same as yours, @Wireless4024: I need an aligned buffer to use I just submitted PR #684, which gives one way in which it might be possible to create a |
added
BytesMut::with_capacity_aligned
to allocate BytesMut with custom alignmentfix #600