Hello,
I am trying to do a particular optimization, where I deallocate part of a block of memory, and give the ownership of the rest to something like a Vec
:
struct MyStruct {
// the length is also contained in here in the first bytes
ptr: NonNull<u8>,
}
impl Into<Vec<u8>> for MyStruct {
fn into(self) -> Vec<u8> {
unsafe {
let length = *(self.ptr.as_ptr() as *mut usize);
let ptr = self.ptr.as_ptr().add(size_of::<usize>());
// somehow only deallocate the part that contains length
Vec::from_raw_parts(ptr, length, length)
}
}
}
I tried using something like
let layout = alloc::Layout::from_size_align_unchecked(
size_of::<usize>(),
align_of::<usize>(),
);
alloc::dealloc(self.ptr.as_ptr(), layout);
But miri complained that the Layout
was incorrect, with something like incorrect layout on deallocation: alloc3805 has size 29 and alignment 8, but gave size 8 and alignment 8
. So if miri is right, is there something in the alloc
API that can achieve this ? I am guessing not, since I didn't find anything like this in std, but who knows...