mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
Remove usable_size
APIs
This commit is contained in:
parent
cd5441faf4
commit
d8e3557dba
@ -165,8 +165,8 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
unsafe impl AllocRef for Global {
|
||||
#[inline]
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(alloc(layout)).ok_or(AllocErr)
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(alloc(layout)).ok_or(AllocErr).map(|p| (p, layout.size()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -180,13 +180,13 @@ unsafe impl AllocRef for Global {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
||||
) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr).map(|p| (p, new_size))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr).map(|p| (p, layout.size()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,7 +201,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
||||
} else {
|
||||
let layout = Layout::from_size_align_unchecked(size, align);
|
||||
match Global.alloc(layout) {
|
||||
Ok(ptr) => ptr.as_ptr(),
|
||||
Ok((ptr, _)) => ptr.as_ptr(),
|
||||
Err(_) => handle_alloc_error(layout),
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use test::Bencher;
|
||||
fn allocate_zeroed() {
|
||||
unsafe {
|
||||
let layout = Layout::from_size_align(1024, 1).unwrap();
|
||||
let ptr =
|
||||
let (ptr, _) =
|
||||
Global.alloc_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
|
||||
let mut i = ptr.cast::<u8>().as_ptr();
|
||||
|
@ -200,7 +200,7 @@ impl<T> Box<T> {
|
||||
let ptr = if layout.size() == 0 {
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
|
||||
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).0.cast()
|
||||
};
|
||||
Box::from_raw(ptr.as_ptr())
|
||||
}
|
||||
@ -270,7 +270,7 @@ impl<T> Box<[T]> {
|
||||
let ptr = if layout.size() == 0 {
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
|
||||
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).0.cast()
|
||||
};
|
||||
Box::from_raw(slice::from_raw_parts_mut(ptr.as_ptr(), len))
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
RawVec::allocate_in(capacity, true, a)
|
||||
}
|
||||
|
||||
fn allocate_in(capacity: usize, zeroed: bool, mut a: A) -> Self {
|
||||
fn allocate_in(mut capacity: usize, zeroed: bool, mut a: A) -> Self {
|
||||
unsafe {
|
||||
let elem_size = mem::size_of::<T>();
|
||||
|
||||
@ -87,7 +87,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
let layout = Layout::from_size_align(alloc_size, align).unwrap();
|
||||
let result = if zeroed { a.alloc_zeroed(layout) } else { a.alloc(layout) };
|
||||
match result {
|
||||
Ok(ptr) => ptr.cast(),
|
||||
Ok((ptr, size)) => {
|
||||
capacity = size / elem_size;
|
||||
ptr.cast()
|
||||
}
|
||||
Err(_) => handle_alloc_error(layout),
|
||||
}
|
||||
};
|
||||
@ -280,7 +283,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
// 0, getting to here necessarily means the `RawVec` is overfull.
|
||||
assert!(elem_size != 0, "capacity overflow");
|
||||
|
||||
let (new_cap, ptr) = match self.current_layout() {
|
||||
let (ptr, new_cap) = match self.current_layout() {
|
||||
Some(cur) => {
|
||||
// Since we guarantee that we never allocate more than
|
||||
// `isize::MAX` bytes, `elem_size * self.cap <= isize::MAX` as
|
||||
@ -297,7 +300,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
alloc_guard(new_size).unwrap_or_else(|_| capacity_overflow());
|
||||
let ptr_res = self.a.realloc(NonNull::from(self.ptr).cast(), cur, new_size);
|
||||
match ptr_res {
|
||||
Ok(ptr) => (new_cap, ptr),
|
||||
Ok((ptr, new_size)) => (ptr, new_size / elem_size),
|
||||
Err(_) => handle_alloc_error(Layout::from_size_align_unchecked(
|
||||
new_size,
|
||||
cur.align(),
|
||||
@ -310,7 +313,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
|
||||
let layout = Layout::array::<T>(new_cap).unwrap();
|
||||
match self.a.alloc(layout) {
|
||||
Ok(ptr) => (new_cap, ptr),
|
||||
Ok((ptr, new_size)) => (ptr, new_size / elem_size),
|
||||
Err(_) => handle_alloc_error(layout),
|
||||
}
|
||||
}
|
||||
@ -598,7 +601,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
let align = mem::align_of::<T>();
|
||||
let old_layout = Layout::from_size_align_unchecked(old_size, align);
|
||||
match self.a.realloc(NonNull::from(self.ptr).cast(), old_layout, new_size) {
|
||||
Ok(p) => self.ptr = p.cast().into(),
|
||||
Ok((ptr, _)) => self.ptr = ptr.cast().into(),
|
||||
Err(_) => {
|
||||
handle_alloc_error(Layout::from_size_align_unchecked(new_size, align))
|
||||
}
|
||||
@ -631,6 +634,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
fallibility: Fallibility,
|
||||
strategy: ReserveStrategy,
|
||||
) -> Result<(), TryReserveError> {
|
||||
let elem_size = mem::size_of::<T>();
|
||||
|
||||
unsafe {
|
||||
// NOTE: we don't early branch on ZSTs here because we want this
|
||||
// to actually catch "asking for more than usize::MAX" in that case.
|
||||
@ -662,7 +667,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
None => self.a.alloc(new_layout),
|
||||
};
|
||||
|
||||
let ptr = match (res, fallibility) {
|
||||
let (ptr, new_cap) = match (res, fallibility) {
|
||||
(Err(AllocErr), Infallible) => handle_alloc_error(new_layout),
|
||||
(Err(AllocErr), Fallible) => {
|
||||
return Err(TryReserveError::AllocError {
|
||||
@ -670,7 +675,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||
non_exhaustive: (),
|
||||
});
|
||||
}
|
||||
(Ok(ptr), _) => ptr,
|
||||
(Ok((ptr, new_size)), _) => (ptr, new_size / elem_size),
|
||||
};
|
||||
|
||||
self.ptr = ptr.cast().into();
|
||||
|
@ -20,7 +20,7 @@ fn allocator_param() {
|
||||
fuel: usize,
|
||||
}
|
||||
unsafe impl AllocRef for BoundedAlloc {
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
let size = layout.size();
|
||||
if size > self.fuel {
|
||||
return Err(AllocErr);
|
||||
|
@ -923,7 +923,7 @@ impl<T: ?Sized> Rc<T> {
|
||||
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
|
||||
|
||||
// Allocate for the layout.
|
||||
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
let (mem, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
|
||||
// Initialize the RcBox
|
||||
let inner = mem_to_rcbox(mem.as_ptr());
|
||||
|
@ -784,7 +784,7 @@ impl<T: ?Sized> Arc<T> {
|
||||
// reference (see #54908).
|
||||
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
|
||||
|
||||
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
let (mem, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
|
||||
// Initialize the ArcInner
|
||||
let inner = mem_to_arcinner(mem.as_ptr());
|
||||
|
@ -20,7 +20,7 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
|
||||
unsafe {
|
||||
let pointers: Vec<_> = (0..iterations)
|
||||
.map(|_| {
|
||||
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
|
||||
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap().0
|
||||
})
|
||||
.collect();
|
||||
for &ptr in &pointers {
|
||||
|
@ -11,12 +11,6 @@ use crate::num::NonZeroUsize;
|
||||
use crate::ptr::{self, NonNull};
|
||||
use crate::usize;
|
||||
|
||||
/// Represents the combination of a starting address and
|
||||
/// a total capacity of the returned block.
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[derive(Debug)]
|
||||
pub struct Excess(pub NonNull<u8>, pub usize);
|
||||
|
||||
const fn size_align<T>() -> (usize, usize) {
|
||||
(mem::size_of::<T>(), mem::align_of::<T>())
|
||||
}
|
||||
@ -593,13 +587,12 @@ pub unsafe trait GlobalAlloc {
|
||||
///
|
||||
/// * the starting address for that memory block was previously
|
||||
/// returned by a previous call to an allocation method (`alloc`,
|
||||
/// `alloc_zeroed`, `alloc_excess`) or reallocation method
|
||||
/// (`realloc`, `realloc_excess`), and
|
||||
/// `alloc_zeroed`) or reallocation method (`realloc`), and
|
||||
///
|
||||
/// * the memory block has not been subsequently deallocated, where
|
||||
/// blocks are deallocated either by being passed to a deallocation
|
||||
/// method (`dealloc`, `dealloc_one`, `dealloc_array`) or by being
|
||||
/// passed to a reallocation method (see above) that returns `Ok`.
|
||||
/// method (`dealloc`) or by being passed to a reallocation method
|
||||
/// (see above) that returns `Ok`.
|
||||
///
|
||||
/// A note regarding zero-sized types and zero-sized layouts: many
|
||||
/// methods in the `AllocRef` trait state that allocation requests
|
||||
@ -625,11 +618,9 @@ pub unsafe trait GlobalAlloc {
|
||||
///
|
||||
/// 2. The block's size must fall in the range `[use_min, use_max]`, where:
|
||||
///
|
||||
/// * `use_min` is `self.usable_size(layout).0`, and
|
||||
/// * `use_min` is `layout.size()`, and
|
||||
///
|
||||
/// * `use_max` is the capacity that was (or would have been)
|
||||
/// returned when (if) the block was allocated via a call to
|
||||
/// `alloc_excess` or `realloc_excess`.
|
||||
/// * `use_max` is the capacity that was returned.
|
||||
///
|
||||
/// Note that:
|
||||
///
|
||||
@ -643,6 +634,9 @@ pub unsafe trait GlobalAlloc {
|
||||
/// currently allocated via an allocator `a`, then it is legal to
|
||||
/// use that layout to deallocate it, i.e., `a.dealloc(ptr, k);`.
|
||||
///
|
||||
/// * if an allocator does not support overallocating, it is fine to
|
||||
/// simply return `layout.size()` as the allocated size.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The `AllocRef` trait is an `unsafe` trait for a number of reasons, and
|
||||
@ -671,8 +665,9 @@ pub unsafe trait AllocRef {
|
||||
// However in jemalloc for example,
|
||||
// `mallocx(0)` is documented as undefined behavior.)
|
||||
|
||||
/// Returns a pointer meeting the size and alignment guarantees of
|
||||
/// `layout`.
|
||||
/// On success, returns a pointer meeting the size and alignment
|
||||
/// guarantees of `layout` and the actual size of the allocated block,
|
||||
/// which must be greater than or equal to `layout.size()`.
|
||||
///
|
||||
/// If this method returns an `Ok(addr)`, then the `addr` returned
|
||||
/// will be non-null address pointing to a block of storage
|
||||
@ -709,7 +704,7 @@ pub unsafe trait AllocRef {
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr>;
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr>;
|
||||
|
||||
/// Deallocate the memory referenced by `ptr`.
|
||||
///
|
||||
@ -728,38 +723,31 @@ pub unsafe trait AllocRef {
|
||||
/// to allocate that block of memory.
|
||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
|
||||
|
||||
// == ALLOCATOR-SPECIFIC QUANTITIES AND LIMITS ==
|
||||
// usable_size
|
||||
|
||||
/// Returns bounds on the guaranteed usable size of a successful
|
||||
/// allocation created with the specified `layout`.
|
||||
/// Behaves like `alloc`, but also ensures that the contents
|
||||
/// are set to zero before being returned.
|
||||
///
|
||||
/// In particular, if one has a memory block allocated via a given
|
||||
/// allocator `a` and layout `k` where `a.usable_size(k)` returns
|
||||
/// `(l, u)`, then one can pass that block to `a.dealloc()` with a
|
||||
/// layout in the size range [l, u].
|
||||
/// # Safety
|
||||
///
|
||||
/// (All implementors of `usable_size` must ensure that
|
||||
/// `l <= k.size() <= u`)
|
||||
/// This function is unsafe for the same reasons that `alloc` is.
|
||||
///
|
||||
/// Both the lower- and upper-bounds (`l` and `u` respectively)
|
||||
/// are provided, because an allocator based on size classes could
|
||||
/// misbehave if one attempts to deallocate a block without
|
||||
/// providing a correct value for its size (i.e., one within the
|
||||
/// range `[l, u]`).
|
||||
/// # Errors
|
||||
///
|
||||
/// Clients who wish to make use of excess capacity are encouraged
|
||||
/// to use the `alloc_excess` and `realloc_excess` instead, as
|
||||
/// this method is constrained to report conservative values that
|
||||
/// serve as valid bounds for *all possible* allocation method
|
||||
/// calls.
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `alloc`.
|
||||
///
|
||||
/// However, for clients that do not wish to track the capacity
|
||||
/// returned by `alloc_excess` locally, this method is likely to
|
||||
/// produce useful results.
|
||||
#[inline]
|
||||
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
|
||||
(layout.size(), layout.size())
|
||||
/// Clients wishing to abort computation in response to an
|
||||
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
let size = layout.size();
|
||||
let result = self.alloc(layout);
|
||||
if let Ok((p, _)) = result {
|
||||
ptr::write_bytes(p.as_ptr(), 0, size);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
// == METHODS FOR MEMORY REUSE ==
|
||||
@ -767,9 +755,10 @@ pub unsafe trait AllocRef {
|
||||
|
||||
/// Returns a pointer suitable for holding data described by
|
||||
/// a new layout with `layout`’s alignment and a size given
|
||||
/// by `new_size`. To
|
||||
/// accomplish this, this may extend or shrink the allocation
|
||||
/// referenced by `ptr` to fit the new layout.
|
||||
/// by `new_size` and the actual size of the allocated block.
|
||||
/// The latter is greater than or equal to `layout.size()`.
|
||||
/// To accomplish this, the allocator may extend or shrink
|
||||
/// the allocation referenced by `ptr` to fit the new layout.
|
||||
///
|
||||
/// If this returns `Ok`, then ownership of the memory block
|
||||
/// referenced by `ptr` has been transferred to this
|
||||
@ -824,23 +813,25 @@ pub unsafe trait AllocRef {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<NonNull<u8>, AllocErr> {
|
||||
) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
let old_size = layout.size();
|
||||
|
||||
if new_size >= old_size {
|
||||
if let Ok(()) = self.grow_in_place(ptr, layout, new_size) {
|
||||
return Ok(ptr);
|
||||
if new_size > old_size {
|
||||
if let Ok(size) = self.grow_in_place(ptr, layout, new_size) {
|
||||
return Ok((ptr, size));
|
||||
}
|
||||
} else if new_size < old_size {
|
||||
if let Ok(()) = self.shrink_in_place(ptr, layout, new_size) {
|
||||
return Ok(ptr);
|
||||
if let Ok(size) = self.shrink_in_place(ptr, layout, new_size) {
|
||||
return Ok((ptr, size));
|
||||
}
|
||||
} else {
|
||||
return Ok((ptr, new_size));
|
||||
}
|
||||
|
||||
// otherwise, fall back on alloc + copy + dealloc.
|
||||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
||||
let result = self.alloc(new_layout);
|
||||
if let Ok(new_ptr) = result {
|
||||
if let Ok((new_ptr, _)) = result {
|
||||
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), cmp::min(old_size, new_size));
|
||||
self.dealloc(ptr, layout);
|
||||
}
|
||||
@ -877,174 +868,40 @@ pub unsafe trait AllocRef {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<NonNull<u8>, AllocErr> {
|
||||
) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
let old_size = layout.size();
|
||||
|
||||
if new_size >= old_size {
|
||||
if let Ok(()) = self.grow_in_place_zeroed(ptr, layout, new_size) {
|
||||
return Ok(ptr);
|
||||
if new_size > old_size {
|
||||
if let Ok(size) = self.grow_in_place_zeroed(ptr, layout, new_size) {
|
||||
return Ok((ptr, size));
|
||||
}
|
||||
} else if new_size < old_size {
|
||||
if let Ok(()) = self.shrink_in_place(ptr, layout, new_size) {
|
||||
return Ok(ptr);
|
||||
if let Ok(size) = self.shrink_in_place(ptr, layout, new_size) {
|
||||
return Ok((ptr, size));
|
||||
}
|
||||
} else {
|
||||
return Ok((ptr, new_size));
|
||||
}
|
||||
|
||||
// otherwise, fall back on alloc + copy + dealloc.
|
||||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
||||
let result = self.alloc_zeroed(new_layout);
|
||||
if let Ok(new_ptr) = result {
|
||||
if let Ok((new_ptr, _)) = result {
|
||||
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), cmp::min(old_size, new_size));
|
||||
self.dealloc(ptr, layout);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Behaves like `alloc`, but also ensures that the contents
|
||||
/// are set to zero before being returned.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe for the same reasons that `alloc` is.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `alloc`.
|
||||
///
|
||||
/// Clients wishing to abort computation in response to an
|
||||
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
let size = layout.size();
|
||||
let p = self.alloc(layout);
|
||||
if let Ok(p) = p {
|
||||
ptr::write_bytes(p.as_ptr(), 0, size);
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// Behaves like `alloc`, but also returns the whole size of
|
||||
/// the returned block. For some `layout` inputs, like arrays, this
|
||||
/// may include extra storage usable for additional data.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe for the same reasons that `alloc` is.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `alloc`.
|
||||
///
|
||||
/// Clients wishing to abort computation in response to an
|
||||
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
|
||||
let usable_size = self.usable_size(&layout);
|
||||
self.alloc(layout).map(|p| Excess(p, usable_size.1))
|
||||
}
|
||||
|
||||
/// Behaves like `alloc`, but also returns the whole size of
|
||||
/// the returned block. For some `layout` inputs, like arrays, this
|
||||
/// may include extra storage usable for additional data.
|
||||
/// Also it ensures that the contents are set to zero before being returned.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe for the same reasons that `alloc` is.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `alloc`.
|
||||
///
|
||||
/// Clients wishing to abort computation in response to an
|
||||
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn alloc_excess_zeroed(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
|
||||
let usable_size = self.usable_size(&layout);
|
||||
self.alloc_zeroed(layout).map(|p| Excess(p, usable_size.1))
|
||||
}
|
||||
|
||||
/// Behaves like `realloc`, but also returns the whole size of
|
||||
/// the returned block. For some `layout` inputs, like arrays, this
|
||||
/// may include extra storage usable for additional data.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe for the same reasons that `realloc` is.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `realloc`.
|
||||
///
|
||||
/// Clients wishing to abort computation in response to a
|
||||
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn realloc_excess(
|
||||
&mut self,
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<Excess, AllocErr> {
|
||||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
||||
let usable_size = self.usable_size(&new_layout);
|
||||
self.realloc(ptr, layout, new_size).map(|p| Excess(p, usable_size.1))
|
||||
}
|
||||
|
||||
/// Behaves like `realloc`, but also returns the whole size of
|
||||
/// the returned block. For some `layout` inputs, like arrays, this
|
||||
/// may include extra storage usable for additional data.
|
||||
/// Also it ensures that the contents are set to zero before being returned.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function is unsafe for the same reasons that `realloc` is.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returning `Err` indicates that either memory is exhausted or
|
||||
/// `layout` does not meet allocator's size or alignment
|
||||
/// constraints, just as in `realloc`.
|
||||
///
|
||||
/// Clients wishing to abort computation in response to a
|
||||
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||
/// rather than directly invoking `panic!` or similar.
|
||||
///
|
||||
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||
unsafe fn realloc_excess_zeroed(
|
||||
&mut self,
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<Excess, AllocErr> {
|
||||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
||||
let usable_size = self.usable_size(&new_layout);
|
||||
self.realloc_zeroed(ptr, layout, new_size).map(|p| Excess(p, usable_size.1))
|
||||
}
|
||||
|
||||
/// Attempts to extend the allocation referenced by `ptr` to fit `new_size`.
|
||||
///
|
||||
/// If this returns `Ok`, then the allocator has asserted that the
|
||||
/// memory block referenced by `ptr` now fits `new_size`, and thus can
|
||||
/// be used to carry data of a layout of that size and same alignment as
|
||||
/// `layout`. (The allocator is allowed to
|
||||
/// expend effort to accomplish this, such as extending the memory block to
|
||||
/// include successor blocks, or virtual memory tricks.)
|
||||
/// `layout`. The returned value is the new size of the allocated block.
|
||||
/// (The allocator is allowed to expend effort to accomplish this, such
|
||||
/// as extending the memory block to include successor blocks, or virtual
|
||||
/// memory tricks.)
|
||||
///
|
||||
/// Regardless of what this method returns, ownership of the
|
||||
/// memory block referenced by `ptr` has not been transferred, and
|
||||
@ -1072,18 +929,17 @@ pub unsafe trait AllocRef {
|
||||
/// function; clients are expected either to be able to recover from
|
||||
/// `grow_in_place` failures without aborting, or to fall back on
|
||||
/// another reallocation method before resorting to an abort.
|
||||
#[inline]
|
||||
unsafe fn grow_in_place(
|
||||
&mut self,
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<(), CannotReallocInPlace> {
|
||||
let _ = ptr; // this default implementation doesn't care about the actual address.
|
||||
debug_assert!(new_size >= layout.size());
|
||||
let (_l, u) = self.usable_size(&layout);
|
||||
// _l <= layout.size() [guaranteed by usable_size()]
|
||||
// layout.size() <= new_layout.size() [required by this method]
|
||||
if new_size <= u { Ok(()) } else { Err(CannotReallocInPlace) }
|
||||
) -> Result<usize, CannotReallocInPlace> {
|
||||
let _ = ptr;
|
||||
let _ = layout;
|
||||
let _ = new_size;
|
||||
Err(CannotReallocInPlace)
|
||||
}
|
||||
|
||||
/// Behaves like `grow_in_place`, but also ensures that the new
|
||||
@ -1108,10 +964,10 @@ pub unsafe trait AllocRef {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<(), CannotReallocInPlace> {
|
||||
self.grow_in_place(ptr, layout, new_size)?;
|
||||
) -> Result<usize, CannotReallocInPlace> {
|
||||
let size = self.grow_in_place(ptr, layout, new_size)?;
|
||||
ptr.as_ptr().add(layout.size()).write_bytes(0, new_size - layout.size());
|
||||
Ok(())
|
||||
Ok(size)
|
||||
}
|
||||
|
||||
/// Attempts to shrink the allocation referenced by `ptr` to fit `new_size`.
|
||||
@ -1119,7 +975,8 @@ pub unsafe trait AllocRef {
|
||||
/// If this returns `Ok`, then the allocator has asserted that the
|
||||
/// memory block referenced by `ptr` now fits `new_size`, and
|
||||
/// thus can only be used to carry data of that smaller
|
||||
/// layout. (The allocator is allowed to take advantage of this,
|
||||
/// layout. The returned value is the new size the allocated block.
|
||||
/// (The allocator is allowed to take advantage of this,
|
||||
/// carving off portions of the block for reuse elsewhere.) The
|
||||
/// truncated contents of the block within the smaller layout are
|
||||
/// unaltered, and ownership of block has not been transferred.
|
||||
@ -1153,17 +1010,16 @@ pub unsafe trait AllocRef {
|
||||
/// function; clients are expected either to be able to recover from
|
||||
/// `shrink_in_place` failures without aborting, or to fall back
|
||||
/// on another reallocation method before resorting to an abort.
|
||||
#[inline]
|
||||
unsafe fn shrink_in_place(
|
||||
&mut self,
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<(), CannotReallocInPlace> {
|
||||
let _ = ptr; // this default implementation doesn't care about the actual address.
|
||||
debug_assert!(new_size <= layout.size());
|
||||
let (l, _u) = self.usable_size(&layout);
|
||||
// layout.size() <= _u [guaranteed by usable_size()]
|
||||
// new_layout.size() <= layout.size() [required by this method]
|
||||
if l <= new_size { Ok(()) } else { Err(CannotReallocInPlace) }
|
||||
) -> Result<usize, CannotReallocInPlace> {
|
||||
let _ = ptr;
|
||||
let _ = layout;
|
||||
let _ = new_size;
|
||||
Err(CannotReallocInPlace)
|
||||
}
|
||||
}
|
||||
|
@ -137,13 +137,15 @@ pub struct System;
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
unsafe impl AllocRef for System {
|
||||
#[inline]
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr).map(|p| (p, layout.size()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout))
|
||||
.ok_or(AllocErr)
|
||||
.map(|p| (p, layout.size()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -157,8 +159,10 @@ unsafe impl AllocRef for System {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
new_size: usize,
|
||||
) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
||||
) -> Result<(NonNull<u8>, usize), AllocErr> {
|
||||
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size))
|
||||
.ok_or(AllocErr)
|
||||
.map(|p| (p, new_size))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ fn main() {
|
||||
unsafe {
|
||||
let layout = Layout::from_size_align(4, 2).unwrap();
|
||||
|
||||
let ptr = Global.alloc(layout.clone()).unwrap();
|
||||
let (ptr, _) = Global.alloc(layout.clone()).unwrap();
|
||||
helper::work_with(&ptr);
|
||||
assert_eq!(HITS.load(Ordering::SeqCst), n + 1);
|
||||
Global.dealloc(ptr, layout.clone());
|
||||
@ -49,7 +49,7 @@ fn main() {
|
||||
drop(s);
|
||||
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
|
||||
|
||||
let ptr = System.alloc(layout.clone()).unwrap();
|
||||
let (ptr, _) = System.alloc(layout.clone()).unwrap();
|
||||
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
|
||||
helper::work_with(&ptr);
|
||||
System.dealloc(ptr, layout);
|
||||
|
@ -20,13 +20,13 @@ fn main() {
|
||||
let n = GLOBAL.0.load(Ordering::SeqCst);
|
||||
let layout = Layout::from_size_align(4, 2).unwrap();
|
||||
|
||||
let ptr = Global.alloc(layout.clone()).unwrap();
|
||||
let (ptr, _) = Global.alloc(layout.clone()).unwrap();
|
||||
helper::work_with(&ptr);
|
||||
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
|
||||
Global.dealloc(ptr, layout.clone());
|
||||
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
||||
|
||||
let ptr = System.alloc(layout.clone()).unwrap();
|
||||
let (ptr, _) = System.alloc(layout.clone()).unwrap();
|
||||
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
||||
helper::work_with(&ptr);
|
||||
System.dealloc(ptr, layout);
|
||||
|
@ -41,13 +41,13 @@ unsafe fn test_triangle() -> bool {
|
||||
println!("allocate({:?})", layout);
|
||||
}
|
||||
|
||||
let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
let (ptr, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
|
||||
if PRINT {
|
||||
println!("allocate({:?}) = {:?}", layout, ret);
|
||||
println!("allocate({:?}) = {:?}", layout, ptr);
|
||||
}
|
||||
|
||||
ret.cast().as_ptr()
|
||||
ptr.cast().as_ptr()
|
||||
}
|
||||
|
||||
unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
|
||||
@ -63,16 +63,16 @@ unsafe fn test_triangle() -> bool {
|
||||
println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
|
||||
}
|
||||
|
||||
let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
|
||||
let (ptr, _) = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
|
||||
.unwrap_or_else(|_| handle_alloc_error(
|
||||
Layout::from_size_align_unchecked(new.size(), old.align())
|
||||
));
|
||||
|
||||
if PRINT {
|
||||
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
|
||||
ptr, old, new, ret);
|
||||
ptr, old, new, ptr);
|
||||
}
|
||||
ret.cast().as_ptr()
|
||||
ptr.cast().as_ptr()
|
||||
}
|
||||
|
||||
fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
|
||||
|
@ -24,29 +24,29 @@ struct Ccx {
|
||||
x: isize
|
||||
}
|
||||
|
||||
fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
|
||||
fn alloc(_bcx: &arena) -> &Bcx<'_> {
|
||||
unsafe {
|
||||
let layout = Layout::new::<Bcx>();
|
||||
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
let (ptr, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||
&*(ptr.as_ptr() as *const _)
|
||||
}
|
||||
}
|
||||
|
||||
fn h<'a>(bcx : &'a Bcx<'a>) -> &'a Bcx<'a> {
|
||||
fn h<'a>(bcx: &'a Bcx<'a>) -> &'a Bcx<'a> {
|
||||
return alloc(bcx.fcx.arena);
|
||||
}
|
||||
|
||||
fn g(fcx : &Fcx) {
|
||||
let bcx = Bcx { fcx: fcx };
|
||||
fn g(fcx: &Fcx) {
|
||||
let bcx = Bcx { fcx };
|
||||
let bcx2 = h(&bcx);
|
||||
unsafe {
|
||||
Global.dealloc(NonNull::new_unchecked(bcx2 as *const _ as *mut _), Layout::new::<Bcx>());
|
||||
}
|
||||
}
|
||||
|
||||
fn f(ccx : &Ccx) {
|
||||
fn f(ccx: &Ccx) {
|
||||
let a = arena(());
|
||||
let fcx = Fcx { arena: &a, ccx: ccx };
|
||||
let fcx = Fcx { arena: &a, ccx };
|
||||
return g(&fcx);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user