Skip to content

Commit 6d7bbcb

Browse files
authored
Rollup merge of rust-lang#69058 - TimDiekmann:box, r=Amanieu
Preparation for allocator aware `Box` This cleans up the `Box` code a bit, and uses `Box::from_raw(ptr)` instead of `Box(ptr)`. Additionally, `box_free` and `exchange_malloc` now uses the `AllocRef` trait and a comment was added on how `box_free` is tied to `Box`. This a preparation for an upcoming PR, which makes `Box` aware of an allocator. r? @Amanieu
2 parents 0d15165 + 76aa29f commit 6d7bbcb

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

src/liballoc/alloc.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,27 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
200200
align as *mut u8
201201
} else {
202202
let layout = Layout::from_size_align_unchecked(size, align);
203-
let ptr = alloc(layout);
204-
if !ptr.is_null() { ptr } else { handle_alloc_error(layout) }
203+
match Global.alloc(layout) {
204+
Ok(ptr) => ptr.as_ptr(),
205+
Err(_) => handle_alloc_error(layout),
206+
}
205207
}
206208
}
207209

208210
#[cfg_attr(not(test), lang = "box_free")]
209211
#[inline]
212+
// This signature has to be the same as `Box`, otherwise an ICE will happen.
213+
// When an additional parameter to `Box` is added (like `A: AllocRef`), this has to be added here as
214+
// well.
215+
// For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`,
216+
// this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well.
210217
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
211-
let ptr = ptr.as_ptr();
212-
let size = size_of_val(&*ptr);
213-
let align = min_align_of_val(&*ptr);
218+
let size = size_of_val(ptr.as_ref());
219+
let align = min_align_of_val(ptr.as_ref());
214220
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
215221
if size != 0 {
216222
let layout = Layout::from_size_align_unchecked(size, align);
217-
dealloc(ptr as *mut u8, layout);
223+
Global.dealloc(ptr.cast().into(), layout);
218224
}
219225
}
220226

src/liballoc/boxed.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,14 @@ impl<T> Box<T> {
196196
#[unstable(feature = "new_uninit", issue = "63291")]
197197
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
198198
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
199-
if layout.size() == 0 {
200-
return Box(NonNull::dangling().into());
199+
unsafe {
200+
let ptr = if layout.size() == 0 {
201+
NonNull::dangling()
202+
} else {
203+
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
204+
};
205+
Box::from_raw(ptr.as_ptr())
201206
}
202-
let ptr =
203-
unsafe { Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)) };
204-
Box(ptr.cast().into())
205207
}
206208

207209
/// Constructs a new `Box` with uninitialized contents, with the memory
@@ -264,15 +266,14 @@ impl<T> Box<[T]> {
264266
#[unstable(feature = "new_uninit", issue = "63291")]
265267
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
266268
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
267-
let ptr = if layout.size() == 0 {
268-
NonNull::dangling()
269-
} else {
270-
unsafe {
269+
unsafe {
270+
let ptr = if layout.size() == 0 {
271+
NonNull::dangling()
272+
} else {
271273
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
272-
}
273-
};
274-
let slice = unsafe { slice::from_raw_parts_mut(ptr.as_ptr(), len) };
275-
Box(Unique::from(slice))
274+
};
275+
Box::from_raw(slice::from_raw_parts_mut(ptr.as_ptr(), len))
276+
}
276277
}
277278
}
278279

@@ -308,7 +309,7 @@ impl<T> Box<mem::MaybeUninit<T>> {
308309
#[unstable(feature = "new_uninit", issue = "63291")]
309310
#[inline]
310311
pub unsafe fn assume_init(self) -> Box<T> {
311-
Box(Box::into_unique(self).cast())
312+
Box::from_raw(Box::into_raw(self) as *mut T)
312313
}
313314
}
314315

@@ -346,7 +347,7 @@ impl<T> Box<[mem::MaybeUninit<T>]> {
346347
#[unstable(feature = "new_uninit", issue = "63291")]
347348
#[inline]
348349
pub unsafe fn assume_init(self) -> Box<[T]> {
349-
Box(Unique::new_unchecked(Box::into_raw(self) as _))
350+
Box::from_raw(Box::into_raw(self) as *mut [T])
350351
}
351352
}
352353

0 commit comments

Comments
 (0)