|
|
|
@ -93,10 +93,11 @@ struct Carton<T>(NonNull<T>);
|
|
|
|
|
impl<T> Carton<T> {
|
|
|
|
|
pub fn new(value: T) -> Self {
|
|
|
|
|
// Allocate enough memory on the heap to store one T.
|
|
|
|
|
let memptr = &mut null_mut() as *mut *mut T;
|
|
|
|
|
assert_ne!(size_of::<T>(), 0, "Zero-sized types are out of the scope of this example);
|
|
|
|
|
let memptr: *mut c_void = ptr::null_mut();
|
|
|
|
|
unsafe {
|
|
|
|
|
let ret = libc::posix_memalign(
|
|
|
|
|
memptr as *mut *mut c_void,
|
|
|
|
|
&mut memptr,
|
|
|
|
|
align_of::<T>(),
|
|
|
|
|
size_of::<T>()
|
|
|
|
|
);
|
|
|
|
@ -107,7 +108,7 @@ impl<T> Carton<T> {
|
|
|
|
|
let mut ptr = unsafe {
|
|
|
|
|
// Safety: memptr is dereferenceable because we created it from a
|
|
|
|
|
// reference and have exclusive access.
|
|
|
|
|
NonNull::new(*memptr)
|
|
|
|
|
NonNull::new(memptr.cast::<T>())
|
|
|
|
|
.expect("Guaranteed non-null if posix_memalign returns 0")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|