Skip to content

Commit 119d2a1

Browse files
committed
Auto merge of rust-lang#75174 - JohnTitor:rollup-z9djftk, r=JohnTitor
Rollup of 5 pull requests Successful merges: - rust-lang#75139 (Remove log alias from librustdoc) - rust-lang#75140 (Clean up E0745) - rust-lang#75149 (Correct a typo in interpret/memory.rs) - rust-lang#75152 (Replace `Memoryblock` with `NonNull<[u8]>`) - rust-lang#75168 (Update books) Failed merges: r? @ghost
2 parents 32d14eb + 324faf1 commit 119d2a1

File tree

24 files changed

+148
-130
lines changed

24 files changed

+148
-130
lines changed

library/alloc/src/alloc.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,27 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
164164
#[unstable(feature = "allocator_api", issue = "32838")]
165165
unsafe impl AllocRef for Global {
166166
#[inline]
167-
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
167+
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
168168
let size = layout.size();
169169
let ptr = if size == 0 {
170170
layout.dangling()
171171
} else {
172172
// SAFETY: `layout` is non-zero in size,
173173
unsafe { NonNull::new(alloc(layout)).ok_or(AllocErr)? }
174174
};
175-
Ok(MemoryBlock { ptr, size })
175+
Ok(NonNull::slice_from_raw_parts(ptr, size))
176176
}
177177

178178
#[inline]
179-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
179+
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
180180
let size = layout.size();
181181
let ptr = if size == 0 {
182182
layout.dangling()
183183
} else {
184184
// SAFETY: `layout` is non-zero in size,
185185
unsafe { NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)? }
186186
};
187-
Ok(MemoryBlock { ptr, size })
187+
Ok(NonNull::slice_from_raw_parts(ptr, size))
188188
}
189189

190190
#[inline]
@@ -202,7 +202,7 @@ unsafe impl AllocRef for Global {
202202
ptr: NonNull<u8>,
203203
layout: Layout,
204204
new_size: usize,
205-
) -> Result<MemoryBlock, AllocErr> {
205+
) -> Result<NonNull<[u8]>, AllocErr> {
206206
debug_assert!(
207207
new_size >= layout.size(),
208208
"`new_size` must be greater than or equal to `layout.size()`"
@@ -212,14 +212,16 @@ unsafe impl AllocRef for Global {
212212
// Other conditions must be upheld by the caller
213213
unsafe {
214214
match layout.size() {
215-
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
215+
old_size if old_size == new_size => {
216+
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
217+
}
216218
0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
217219
old_size => {
218220
// `realloc` probably checks for `new_size > size` or something similar.
219221
intrinsics::assume(new_size > old_size);
220222
let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
221223
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
222-
Ok(MemoryBlock { ptr, size: new_size })
224+
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
223225
}
224226
}
225227
}
@@ -231,7 +233,7 @@ unsafe impl AllocRef for Global {
231233
ptr: NonNull<u8>,
232234
layout: Layout,
233235
new_size: usize,
234-
) -> Result<MemoryBlock, AllocErr> {
236+
) -> Result<NonNull<[u8]>, AllocErr> {
235237
debug_assert!(
236238
new_size >= layout.size(),
237239
"`new_size` must be greater than or equal to `layout.size()`"
@@ -241,15 +243,17 @@ unsafe impl AllocRef for Global {
241243
// Other conditions must be upheld by the caller
242244
unsafe {
243245
match layout.size() {
244-
old_size if old_size == new_size => Ok(MemoryBlock { ptr, size: new_size }),
246+
old_size if old_size == new_size => {
247+
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
248+
}
245249
0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
246250
old_size => {
247251
// `realloc` probably checks for `new_size > size` or something similar.
248252
intrinsics::assume(new_size > old_size);
249253
let raw_ptr = realloc(ptr.as_ptr(), layout, new_size);
250254
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
251255
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
252-
Ok(MemoryBlock { ptr, size: new_size })
256+
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
253257
}
254258
}
255259
}
@@ -261,7 +265,7 @@ unsafe impl AllocRef for Global {
261265
ptr: NonNull<u8>,
262266
layout: Layout,
263267
new_size: usize,
264-
) -> Result<MemoryBlock, AllocErr> {
268+
) -> Result<NonNull<[u8]>, AllocErr> {
265269
let old_size = layout.size();
266270
debug_assert!(
267271
new_size <= old_size,
@@ -288,7 +292,7 @@ unsafe impl AllocRef for Global {
288292
NonNull::new(raw_ptr).ok_or(AllocErr)?
289293
};
290294

291-
Ok(MemoryBlock { ptr, size: new_size })
295+
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
292296
}
293297
}
294298

@@ -300,7 +304,7 @@ unsafe impl AllocRef for Global {
300304
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
301305
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
302306
match Global.alloc(layout) {
303-
Ok(memory) => memory.ptr.as_ptr(),
307+
Ok(ptr) => ptr.as_non_null_ptr().as_ptr(),
304308
Err(_) => handle_alloc_error(layout),
305309
}
306310
}

library/alloc/src/alloc/tests.rs

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

14-
let mut i = memory.ptr.cast::<u8>().as_ptr();
14+
let mut i = ptr.as_non_null_ptr().as_ptr();
1515
let end = i.add(layout.size());
1616
while i < end {
1717
assert_eq!(*i, 0);
1818
i = i.offset(1);
1919
}
20-
Global.dealloc(memory.ptr, layout);
20+
Global.dealloc(ptr.as_non_null_ptr(), layout);
2121
}
2222
}
2323

library/alloc/src/boxed.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ impl<T> Box<T> {
197197
#[unstable(feature = "new_uninit", issue = "63291")]
198198
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
199199
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
200-
let ptr =
201-
Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).ptr.cast();
200+
let ptr = Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast();
202201
unsafe { Box::from_raw(ptr.as_ptr()) }
203202
}
204203

@@ -226,7 +225,6 @@ impl<T> Box<T> {
226225
let ptr = Global
227226
.alloc_zeroed(layout)
228227
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
229-
.ptr
230228
.cast();
231229
unsafe { Box::from_raw(ptr.as_ptr()) }
232230
}

library/alloc/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#![feature(negative_impls)]
105105
#![feature(new_uninit)]
106106
#![feature(nll)]
107+
#![feature(nonnull_slice_from_raw_parts)]
107108
#![feature(optin_builtin_traits)]
108109
#![feature(or_patterns)]
109110
#![feature(pattern)]
@@ -113,6 +114,8 @@
113114
#![feature(rustc_attrs)]
114115
#![feature(receiver_trait)]
115116
#![feature(min_specialization)]
117+
#![feature(slice_ptr_get)]
118+
#![feature(slice_ptr_len)]
116119
#![feature(staged_api)]
117120
#![feature(std_internals)]
118121
#![feature(str_internals)]

library/alloc/src/raw_vec.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")]
22
#![doc(hidden)]
33

4-
use core::alloc::{LayoutErr, MemoryBlock};
4+
use core::alloc::LayoutErr;
55
use core::cmp;
66
use core::mem::{self, ManuallyDrop, MaybeUninit};
77
use core::ops::Drop;
@@ -186,14 +186,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
186186
AllocInit::Uninitialized => alloc.alloc(layout),
187187
AllocInit::Zeroed => alloc.alloc_zeroed(layout),
188188
};
189-
let memory = match result {
190-
Ok(memory) => memory,
189+
let ptr = match result {
190+
Ok(ptr) => ptr,
191191
Err(_) => handle_alloc_error(layout),
192192
};
193193

194194
Self {
195-
ptr: unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) },
196-
cap: Self::capacity_from_bytes(memory.size),
195+
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
196+
cap: Self::capacity_from_bytes(ptr.len()),
197197
alloc,
198198
}
199199
}
@@ -384,9 +384,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
384384
excess / mem::size_of::<T>()
385385
}
386386

387-
fn set_memory(&mut self, memory: MemoryBlock) {
388-
self.ptr = unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) };
389-
self.cap = Self::capacity_from_bytes(memory.size);
387+
fn set_ptr(&mut self, ptr: NonNull<[u8]>) {
388+
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
389+
self.cap = Self::capacity_from_bytes(ptr.len());
390390
}
391391

392392
// This method is usually instantiated many times. So we want it to be as
@@ -432,8 +432,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
432432
let new_layout = Layout::array::<T>(cap);
433433

434434
// `finish_grow` is non-generic over `T`.
435-
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
436-
self.set_memory(memory);
435+
let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
436+
self.set_ptr(ptr);
437437
Ok(())
438438
}
439439

@@ -451,8 +451,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
451451
let new_layout = Layout::array::<T>(cap);
452452

453453
// `finish_grow` is non-generic over `T`.
454-
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
455-
self.set_memory(memory);
454+
let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
455+
self.set_ptr(ptr);
456456
Ok(())
457457
}
458458

@@ -462,13 +462,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
462462
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
463463
let new_size = amount * mem::size_of::<T>();
464464

465-
let memory = unsafe {
465+
let ptr = unsafe {
466466
self.alloc.shrink(ptr, layout, new_size).map_err(|_| TryReserveError::AllocError {
467467
layout: Layout::from_size_align_unchecked(new_size, layout.align()),
468468
non_exhaustive: (),
469469
})?
470470
};
471-
self.set_memory(memory);
471+
self.set_ptr(ptr);
472472
Ok(())
473473
}
474474
}
@@ -481,7 +481,7 @@ fn finish_grow<A>(
481481
new_layout: Result<Layout, LayoutErr>,
482482
current_memory: Option<(NonNull<u8>, Layout)>,
483483
alloc: &mut A,
484-
) -> Result<MemoryBlock, TryReserveError>
484+
) -> Result<NonNull<[u8]>, TryReserveError>
485485
where
486486
A: AllocRef,
487487
{

library/alloc/src/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-
fn alloc(&mut self, layout: Layout) -> Result<MemoryBlock, AllocErr> {
23+
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
2424
let size = layout.size();
2525
if size > self.fuel {
2626
return Err(AllocErr);

library/alloc/src/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,10 @@ impl<T: ?Sized> Rc<T> {
928928
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
929929

930930
// Allocate for the layout.
931-
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
931+
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
932932

933933
// Initialize the RcBox
934-
let inner = mem_to_rcbox(mem.ptr.as_ptr());
934+
let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr());
935935
unsafe {
936936
debug_assert_eq!(Layout::for_value(&*inner), layout);
937937

library/alloc/src/sync.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -883,10 +883,10 @@ impl<T: ?Sized> Arc<T> {
883883
// reference (see #54908).
884884
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
885885

886-
let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
886+
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
887887

888888
// Initialize the ArcInner
889-
let inner = mem_to_arcinner(mem.ptr.as_ptr());
889+
let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
890890
debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout);
891891

892892
unsafe {
@@ -986,7 +986,7 @@ impl<T> Arc<[T]> {
986986
let slice = from_raw_parts_mut(self.elems, self.n_elems);
987987
ptr::drop_in_place(slice);
988988

989-
Global.dealloc(self.mem.cast(), self.layout);
989+
Global.dealloc(self.mem, self.layout);
990990
}
991991
}
992992
}

library/alloc/tests/heap.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ 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().ptr
23+
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
2424
})
2525
.collect();
2626
for &ptr in &pointers {
2727
assert_eq!(
28-
(ptr.as_ptr() as usize) % align,
28+
(ptr.as_non_null_ptr().as_ptr() as usize) % align,
2929
0,
3030
"Got a pointer less aligned than requested"
3131
)
3232
}
3333

3434
// Clean up
3535
for &ptr in &pointers {
36-
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
36+
allocator.dealloc(
37+
ptr.as_non_null_ptr(),
38+
Layout::from_size_align(size, align).unwrap(),
39+
)
3740
}
3841
}
3942
}

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(associated_type_bounds)]
1414
#![feature(binary_heap_into_iter_sorted)]
1515
#![feature(binary_heap_drain_sorted)]
16+
#![feature(slice_ptr_get)]
1617
#![feature(split_inclusive)]
1718
#![feature(binary_heap_retain)]
1819

0 commit comments

Comments
 (0)