Skip to content

Commit

Permalink
Remove usable_size APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Mar 2, 2020
1 parent cd5441f commit d8e3557
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 269 deletions.
14 changes: 7 additions & 7 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()))
}
}

Expand All @@ -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),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/alloc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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))
}
Expand Down
21 changes: 13 additions & 8 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();

Expand All @@ -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),
}
};
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -662,15 +667,15 @@ 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 {
layout: new_layout,
non_exhaustive: (),
});
}
(Ok(ptr), _) => ptr,
(Ok((ptr, new_size)), _) => (ptr, new_size / elem_size),
};

self.ptr = ptr.cast().into();
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/raw_vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/tests/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit d8e3557

Please sign in to comment.