Skip to content

Commit d198d60

Browse files
authored
Rollup merge of rust-lang#69609 - TimDiekmann:excess, r=Amanieu
Remove `usable_size` APIs This removes the usable size APIs: - remove `usable_size` (obv) - change return type of allocating methods to include the allocated size - remove `_excess` API r? @Amanieu closes rust-lang/wg-allocators#17
2 parents f53764a + d8e3557 commit d198d60

File tree

14 files changed

+134
-269
lines changed

14 files changed

+134
-269
lines changed

src/liballoc/alloc.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
165165
#[unstable(feature = "allocator_api", issue = "32838")]
166166
unsafe impl AllocRef for Global {
167167
#[inline]
168-
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
169-
NonNull::new(alloc(layout)).ok_or(AllocErr)
168+
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
169+
NonNull::new(alloc(layout)).ok_or(AllocErr).map(|p| (p, layout.size()))
170170
}
171171

172172
#[inline]
@@ -180,13 +180,13 @@ unsafe impl AllocRef for Global {
180180
ptr: NonNull<u8>,
181181
layout: Layout,
182182
new_size: usize,
183-
) -> Result<NonNull<u8>, AllocErr> {
184-
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
183+
) -> Result<(NonNull<u8>, usize), AllocErr> {
184+
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr).map(|p| (p, new_size))
185185
}
186186

187187
#[inline]
188-
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
189-
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
188+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
189+
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr).map(|p| (p, layout.size()))
190190
}
191191
}
192192

@@ -201,7 +201,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
201201
} else {
202202
let layout = Layout::from_size_align_unchecked(size, align);
203203
match Global.alloc(layout) {
204-
Ok(ptr) => ptr.as_ptr(),
204+
Ok((ptr, _)) => ptr.as_ptr(),
205205
Err(_) => handle_alloc_error(layout),
206206
}
207207
}

src/liballoc/alloc/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use test::Bencher;
88
fn allocate_zeroed() {
99
unsafe {
1010
let layout = Layout::from_size_align(1024, 1).unwrap();
11-
let ptr =
11+
let (ptr, _) =
1212
Global.alloc_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));
1313

1414
let mut i = ptr.cast::<u8>().as_ptr();

src/liballoc/boxed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<T> Box<T> {
200200
let ptr = if layout.size() == 0 {
201201
NonNull::dangling()
202202
} else {
203-
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
203+
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).0.cast()
204204
};
205205
Box::from_raw(ptr.as_ptr())
206206
}
@@ -270,7 +270,7 @@ impl<T> Box<[T]> {
270270
let ptr = if layout.size() == 0 {
271271
NonNull::dangling()
272272
} else {
273-
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast()
273+
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).0.cast()
274274
};
275275
Box::from_raw(slice::from_raw_parts_mut(ptr.as_ptr(), len))
276276
}

src/liballoc/raw_vec.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
7272
RawVec::allocate_in(capacity, true, a)
7373
}
7474

75-
fn allocate_in(capacity: usize, zeroed: bool, mut a: A) -> Self {
75+
fn allocate_in(mut capacity: usize, zeroed: bool, mut a: A) -> Self {
7676
unsafe {
7777
let elem_size = mem::size_of::<T>();
7878

@@ -87,7 +87,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
8787
let layout = Layout::from_size_align(alloc_size, align).unwrap();
8888
let result = if zeroed { a.alloc_zeroed(layout) } else { a.alloc(layout) };
8989
match result {
90-
Ok(ptr) => ptr.cast(),
90+
Ok((ptr, size)) => {
91+
capacity = size / elem_size;
92+
ptr.cast()
93+
}
9194
Err(_) => handle_alloc_error(layout),
9295
}
9396
};
@@ -280,7 +283,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
280283
// 0, getting to here necessarily means the `RawVec` is overfull.
281284
assert!(elem_size != 0, "capacity overflow");
282285

283-
let (new_cap, ptr) = match self.current_layout() {
286+
let (ptr, new_cap) = match self.current_layout() {
284287
Some(cur) => {
285288
// Since we guarantee that we never allocate more than
286289
// `isize::MAX` bytes, `elem_size * self.cap <= isize::MAX` as
@@ -297,7 +300,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
297300
alloc_guard(new_size).unwrap_or_else(|_| capacity_overflow());
298301
let ptr_res = self.a.realloc(NonNull::from(self.ptr).cast(), cur, new_size);
299302
match ptr_res {
300-
Ok(ptr) => (new_cap, ptr),
303+
Ok((ptr, new_size)) => (ptr, new_size / elem_size),
301304
Err(_) => handle_alloc_error(Layout::from_size_align_unchecked(
302305
new_size,
303306
cur.align(),
@@ -310,7 +313,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
310313
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
311314
let layout = Layout::array::<T>(new_cap).unwrap();
312315
match self.a.alloc(layout) {
313-
Ok(ptr) => (new_cap, ptr),
316+
Ok((ptr, new_size)) => (ptr, new_size / elem_size),
314317
Err(_) => handle_alloc_error(layout),
315318
}
316319
}
@@ -598,7 +601,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
598601
let align = mem::align_of::<T>();
599602
let old_layout = Layout::from_size_align_unchecked(old_size, align);
600603
match self.a.realloc(NonNull::from(self.ptr).cast(), old_layout, new_size) {
601-
Ok(p) => self.ptr = p.cast().into(),
604+
Ok((ptr, _)) => self.ptr = ptr.cast().into(),
602605
Err(_) => {
603606
handle_alloc_error(Layout::from_size_align_unchecked(new_size, align))
604607
}
@@ -631,6 +634,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
631634
fallibility: Fallibility,
632635
strategy: ReserveStrategy,
633636
) -> Result<(), TryReserveError> {
637+
let elem_size = mem::size_of::<T>();
638+
634639
unsafe {
635640
// NOTE: we don't early branch on ZSTs here because we want this
636641
// to actually catch "asking for more than usize::MAX" in that case.
@@ -662,15 +667,15 @@ impl<T, A: AllocRef> RawVec<T, A> {
662667
None => self.a.alloc(new_layout),
663668
};
664669

665-
let ptr = match (res, fallibility) {
670+
let (ptr, new_cap) = match (res, fallibility) {
666671
(Err(AllocErr), Infallible) => handle_alloc_error(new_layout),
667672
(Err(AllocErr), Fallible) => {
668673
return Err(TryReserveError::AllocError {
669674
layout: new_layout,
670675
non_exhaustive: (),
671676
});
672677
}
673-
(Ok(ptr), _) => ptr,
678+
(Ok((ptr, new_size)), _) => (ptr, new_size / elem_size),
674679
};
675680

676681
self.ptr = ptr.cast().into();

src/liballoc/raw_vec/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn allocator_param() {
2020
fuel: usize,
2121
}
2222
unsafe impl AllocRef for BoundedAlloc {
23-
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
23+
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
2424
let size = layout.size();
2525
if size > self.fuel {
2626
return Err(AllocErr);

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl<T: ?Sized> Rc<T> {
923923
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
924924

925925
// Allocate for the layout.
926-
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
926+
let (mem, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
927927

928928
// Initialize the RcBox
929929
let inner = mem_to_rcbox(mem.as_ptr());

src/liballoc/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ impl<T: ?Sized> Arc<T> {
784784
// reference (see #54908).
785785
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
786786

787-
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
787+
let (mem, _) = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
788788

789789
// Initialize the ArcInner
790790
let inner = mem_to_arcinner(mem.as_ptr());

src/liballoc/tests/heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
2020
unsafe {
2121
let pointers: Vec<_> = (0..iterations)
2222
.map(|_| {
23-
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
23+
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap().0
2424
})
2525
.collect();
2626
for &ptr in &pointers {

0 commit comments

Comments
 (0)