Skip to content

Commit 0dc6d4f

Browse files
committed
Stabilize dyn subset of Allocator as core::alloc::Alloc
1 parent d9a49e6 commit 0dc6d4f

24 files changed

Lines changed: 271 additions & 236 deletions

File tree

library/alloc/src/alloc.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ unsafe extern "Rust" {
4545

4646
/// The global memory allocator.
4747
///
48-
/// This type implements the [`Allocator`] trait by forwarding calls
48+
/// This type implements the [`Alloc`] trait by forwarding calls
4949
/// to the allocator registered with the `#[global_allocator]` attribute
5050
/// if there is one, or the `std` crate’s default.
51-
///
52-
/// Note: while this type is unstable, the functionality it provides can be
53-
/// accessed through the [free functions in `alloc`](self#functions).
54-
#[unstable(feature = "allocator_api", issue = "32838")]
51+
#[stable(feature = "allocator_api_mvp", since = "CURRENT_RUSTC_VERSION")]
5552
#[derive(Copy, Clone, Default, Debug)]
5653
// the compiler needs to know when a Box uses the global allocator vs a custom one
5754
#[lang = "global_alloc_ty"]
@@ -202,14 +199,14 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
202199
impl Global {
203200
#[inline]
204201
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
205-
fn alloc_impl_runtime(layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
202+
fn alloc_impl_runtime(layout: Layout, zeroed: bool) -> Result<NonNull<u8>, AllocError> {
206203
match layout.size() {
207-
0 => Ok(layout.dangling_ptr().cast_slice(0)),
204+
0 => Ok(layout.dangling_ptr()),
208205
// SAFETY: `layout` is non-zero in size,
209-
size => unsafe {
206+
_ => unsafe {
210207
let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };
211208
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
212-
Ok(ptr.cast_slice(size))
209+
Ok(ptr)
213210
},
214211
}
215212
}
@@ -224,13 +221,13 @@ impl Global {
224221
// "fit" always means a layout that is equal to the original, because our
225222
// `allocate()`, `grow()`, and `shrink()` implementations never returns a larger
226223
// allocation than requested.
227-
// * Other conditions must be upheld by the caller, as per `Allocator::deallocate()`'s
224+
// * Other conditions must be upheld by the caller, as per `Alloc::deallocate()`'s
228225
// safety documentation.
229226
unsafe { dealloc_nonnull(ptr, layout) }
230227
}
231228
}
232229

233-
// SAFETY: Same as `Allocator::grow`
230+
// SAFETY: Same as `Alloc::grow`
234231
#[inline]
235232
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
236233
fn grow_impl_runtime(
@@ -239,7 +236,7 @@ impl Global {
239236
old_layout: Layout,
240237
new_layout: Layout,
241238
zeroed: bool,
242-
) -> Result<NonNull<[u8]>, AllocError> {
239+
) -> Result<NonNull<u8>, AllocError> {
243240
debug_assert!(
244241
new_layout.size() >= old_layout.size(),
245242
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -261,7 +258,7 @@ impl Global {
261258
if zeroed {
262259
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
263260
}
264-
Ok(ptr.cast_slice(new_size))
261+
Ok(ptr)
265262
},
266263

267264
// SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
@@ -271,14 +268,14 @@ impl Global {
271268
// for `dealloc` must be upheld by the caller.
272269
old_size => unsafe {
273270
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
274-
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
271+
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), old_size);
275272
self.deallocate(ptr, old_layout);
276273
Ok(new_ptr)
277274
},
278275
}
279276
}
280277

281-
// SAFETY: Same as `Allocator::grow`
278+
// SAFETY: Same as `Alloc::grow`
282279
#[inline]
283280
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
284281
fn shrink_impl_runtime(
@@ -287,7 +284,7 @@ impl Global {
287284
old_layout: Layout,
288285
new_layout: Layout,
289286
_zeroed: bool,
290-
) -> Result<NonNull<[u8]>, AllocError> {
287+
) -> Result<NonNull<u8>, AllocError> {
291288
debug_assert!(
292289
new_layout.size() <= old_layout.size(),
293290
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
@@ -297,7 +294,7 @@ impl Global {
297294
// SAFETY: conditions must be upheld by the caller
298295
0 => unsafe {
299296
self.deallocate(ptr, old_layout);
300-
Ok(new_layout.dangling_ptr().cast_slice(0))
297+
Ok(new_layout.dangling_ptr())
301298
},
302299

303300
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
@@ -307,7 +304,7 @@ impl Global {
307304

308305
let raw_ptr = realloc_nonnull(ptr, old_layout, new_size);
309306
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
310-
Ok(ptr.cast_slice(new_size))
307+
Ok(ptr)
311308
},
312309

313310
// SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
@@ -317,26 +314,26 @@ impl Global {
317314
// for `dealloc` must be upheld by the caller.
318315
new_size => unsafe {
319316
let new_ptr = self.allocate(new_layout)?;
320-
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
317+
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), new_size);
321318
self.deallocate(ptr, old_layout);
322319
Ok(new_ptr)
323320
},
324321
}
325322
}
326323

327-
// SAFETY: Same as `Allocator::allocate`
324+
// SAFETY: Same as `Alloc::allocate`
328325
#[inline]
329326
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
330327
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
331-
const fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
328+
const fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<u8>, AllocError> {
332329
core::intrinsics::const_eval_select(
333330
(layout, zeroed),
334331
Global::alloc_impl_const,
335332
Global::alloc_impl_runtime,
336333
)
337334
}
338335

339-
// SAFETY: Same as `Allocator::deallocate`
336+
// SAFETY: Same as `Alloc::deallocate`
340337
#[inline]
341338
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
342339
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
@@ -348,7 +345,7 @@ impl Global {
348345
)
349346
}
350347

351-
// SAFETY: Same as `Allocator::grow`
348+
// SAFETY: Same as `Alloc::grow`
352349
#[inline]
353350
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
354351
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
@@ -358,15 +355,15 @@ impl Global {
358355
old_layout: Layout,
359356
new_layout: Layout,
360357
zeroed: bool,
361-
) -> Result<NonNull<[u8]>, AllocError> {
358+
) -> Result<NonNull<u8>, AllocError> {
362359
core::intrinsics::const_eval_select(
363360
(self, ptr, old_layout, new_layout, zeroed),
364361
Global::grow_shrink_impl_const,
365362
Global::grow_impl_runtime,
366363
)
367364
}
368365

369-
// SAFETY: Same as `Allocator::shrink`
366+
// SAFETY: Same as `Alloc::shrink`
370367
#[inline]
371368
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
372369
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
@@ -375,7 +372,7 @@ impl Global {
375372
ptr: NonNull<u8>,
376373
old_layout: Layout,
377374
new_layout: Layout,
378-
) -> Result<NonNull<[u8]>, AllocError> {
375+
) -> Result<NonNull<u8>, AllocError> {
379376
core::intrinsics::const_eval_select(
380377
(self, ptr, old_layout, new_layout, false),
381378
Global::grow_shrink_impl_const,
@@ -386,9 +383,9 @@ impl Global {
386383
#[inline]
387384
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
388385
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
389-
const fn alloc_impl_const(layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
386+
const fn alloc_impl_const(layout: Layout, zeroed: bool) -> Result<NonNull<u8>, AllocError> {
390387
match layout.size() {
391-
0 => Ok(layout.dangling_ptr().cast_slice(0)),
388+
0 => Ok(layout.dangling_ptr()),
392389
// SAFETY: `layout` is non-zero in size,
393390
size => unsafe {
394391
let raw_ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
@@ -397,7 +394,7 @@ impl Global {
397394
// SAFETY: the pointer returned by `const_allocate` is valid to write to.
398395
ptr.write_bytes(0, size);
399396
}
400-
Ok(ptr.cast_slice(size))
397+
Ok(ptr)
401398
},
402399
}
403400
}
@@ -423,13 +420,13 @@ impl Global {
423420
old_layout: Layout,
424421
new_layout: Layout,
425422
zeroed: bool,
426-
) -> Result<NonNull<[u8]>, AllocError> {
423+
) -> Result<NonNull<u8>, AllocError> {
427424
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
428425
// SAFETY: both pointers are valid and this operations is in bounds.
429426
unsafe {
430427
ptr::copy_nonoverlapping(
431428
ptr.as_ptr(),
432-
new_ptr.as_mut_ptr(),
429+
new_ptr.as_ptr(),
433430
cmp::min(old_layout.size(), new_layout.size()),
434431
);
435432
}
@@ -440,18 +437,18 @@ impl Global {
440437
}
441438
}
442439

443-
#[unstable(feature = "allocator_api", issue = "32838")]
440+
#[stable(feature = "allocator_api_mvp", since = "CURRENT_RUSTC_VERSION")]
444441
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
445-
const unsafe impl Allocator for Global {
442+
const unsafe impl Alloc for Global {
446443
#[inline]
447444
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
448-
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
445+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
449446
self.alloc_impl(layout, false)
450447
}
451448

452449
#[inline]
453450
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
454-
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
451+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
455452
self.alloc_impl(layout, true)
456453
}
457454

@@ -469,7 +466,7 @@ const unsafe impl Allocator for Global {
469466
ptr: NonNull<u8>,
470467
old_layout: Layout,
471468
new_layout: Layout,
472-
) -> Result<NonNull<[u8]>, AllocError> {
469+
) -> Result<NonNull<u8>, AllocError> {
473470
// SAFETY: all conditions must be upheld by the caller
474471
unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
475472
}
@@ -481,7 +478,7 @@ const unsafe impl Allocator for Global {
481478
ptr: NonNull<u8>,
482479
old_layout: Layout,
483480
new_layout: Layout,
484-
) -> Result<NonNull<[u8]>, AllocError> {
481+
) -> Result<NonNull<u8>, AllocError> {
485482
// SAFETY: all conditions must be upheld by the caller
486483
unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
487484
}
@@ -493,12 +490,16 @@ const unsafe impl Allocator for Global {
493490
ptr: NonNull<u8>,
494491
old_layout: Layout,
495492
new_layout: Layout,
496-
) -> Result<NonNull<[u8]>, AllocError> {
493+
) -> Result<NonNull<u8>, AllocError> {
497494
// SAFETY: all conditions must be upheld by the caller
498495
unsafe { self.shrink_impl(ptr, old_layout, new_layout) }
499496
}
500497
}
501498

499+
#[unstable(feature = "allocator_api", issue = "32838")]
500+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
501+
const unsafe impl Allocator for Global {}
502+
502503
// # Allocation error handler
503504

504505
#[cfg(not(no_global_oom_handling))]

library/alloc/src/boxed.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ use core::task::{Context, Poll};
206206

207207
#[cfg(not(no_global_oom_handling))]
208208
use crate::alloc::handle_alloc_error;
209-
use crate::alloc::{AllocError, Allocator, Global, Layout};
209+
use crate::alloc::{Alloc, AllocError, Allocator, Global, Layout};
210210
use crate::raw_vec::RawVec;
211211
#[cfg(not(no_global_oom_handling))]
212212
use crate::str::from_boxed_utf8_unchecked;
@@ -246,7 +246,7 @@ pub struct Box<
246246
#[cfg(not(no_global_oom_handling))]
247247
fn box_new_uninit(layout: Layout) -> *mut u8 {
248248
match Global.allocate(layout) {
249-
Ok(ptr) => ptr.as_mut_ptr(),
249+
Ok(ptr) => ptr.as_ptr(),
250250
Err(_) => handle_alloc_error(layout),
251251
}
252252
}
@@ -801,7 +801,6 @@ impl<T: ?Sized + CloneToUninit> Box<T> {
801801
///
802802
/// ```
803803
/// #![feature(clone_from_ref)]
804-
/// #![feature(allocator_api)]
805804
///
806805
/// let hello: Box<str> = Box::try_clone_from_ref("hello")?;
807806
/// # Ok::<(), std::alloc::AllocError>(())
@@ -1537,12 +1536,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15371536
/// ```
15381537
/// Manually create a `Box` from scratch by using the system allocator:
15391538
/// ```
1540-
/// #![feature(allocator_api, slice_ptr_get)]
1539+
/// #![feature(allocator_api)]
15411540
///
1542-
/// use std::alloc::{Allocator, Layout, System};
1541+
/// use std::alloc::{Alloc, Layout, System};
15431542
///
15441543
/// unsafe {
1545-
/// let ptr = System.allocate(Layout::new::<i32>())?.as_mut_ptr() as *mut i32;
1544+
/// let ptr = System.allocate(Layout::new::<i32>())?.as_ptr() as *mut i32;
15461545
/// // In general .write is required to avoid attempting to destruct
15471546
/// // the (uninitialized) previous contents of `ptr`, though for this
15481547
/// // simple example `*ptr = 5` would have worked as well.
@@ -1596,7 +1595,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15961595
/// ```
15971596
/// #![feature(allocator_api)]
15981597
///
1599-
/// use std::alloc::{Allocator, Layout, System};
1598+
/// use std::alloc::{Alloc, Layout, System};
16001599
///
16011600
/// unsafe {
16021601
/// let non_null = System.allocate(Layout::new::<i32>())?.cast::<i32>();
@@ -1651,7 +1650,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
16511650
/// ```
16521651
/// #![feature(allocator_api)]
16531652
///
1654-
/// use std::alloc::{Allocator, Layout, System};
1653+
/// use std::alloc::{Alloc, Layout, System};
16551654
/// use std::ptr::{self, NonNull};
16561655
///
16571656
/// let x = Box::new_in(String::from("Hello"), System);
@@ -1713,7 +1712,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
17131712
/// ```
17141713
/// #![feature(allocator_api)]
17151714
///
1716-
/// use std::alloc::{Allocator, Layout, System};
1715+
/// use std::alloc::{Alloc, Layout, System};
17171716
///
17181717
/// let x = Box::new_in(String::from("Hello"), System);
17191718
/// let (non_null, alloc) = Box::into_non_null_with_allocator(x);
@@ -2434,15 +2433,15 @@ impl<E: Error> Error for Box<E> {
24342433
}
24352434
}
24362435

2437-
#[unstable(feature = "allocator_api", issue = "32838")]
2438-
unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
2436+
#[stable(feature = "allocator_api_mvp", since = "CURRENT_RUSTC_VERSION")]
2437+
unsafe impl<T: ?Sized + Alloc, A: Allocator> Alloc for Box<T, A> {
24392438
#[inline]
2440-
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
2439+
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
24412440
(**self).allocate(layout)
24422441
}
24432442

24442443
#[inline]
2445-
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
2444+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
24462445
(**self).allocate_zeroed(layout)
24472446
}
24482447

@@ -2458,7 +2457,7 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
24582457
ptr: NonNull<u8>,
24592458
old_layout: Layout,
24602459
new_layout: Layout,
2461-
) -> Result<NonNull<[u8]>, AllocError> {
2460+
) -> Result<NonNull<u8>, AllocError> {
24622461
// SAFETY: the safety contract must be upheld by the caller
24632462
unsafe { (**self).grow(ptr, old_layout, new_layout) }
24642463
}
@@ -2469,7 +2468,7 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
24692468
ptr: NonNull<u8>,
24702469
old_layout: Layout,
24712470
new_layout: Layout,
2472-
) -> Result<NonNull<[u8]>, AllocError> {
2471+
) -> Result<NonNull<u8>, AllocError> {
24732472
// SAFETY: the safety contract must be upheld by the caller
24742473
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
24752474
}
@@ -2480,8 +2479,11 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
24802479
ptr: NonNull<u8>,
24812480
old_layout: Layout,
24822481
new_layout: Layout,
2483-
) -> Result<NonNull<[u8]>, AllocError> {
2482+
) -> Result<NonNull<u8>, AllocError> {
24842483
// SAFETY: the safety contract must be upheld by the caller
24852484
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
24862485
}
24872486
}
2487+
2488+
#[unstable(feature = "allocator_api", issue = "32838")]
2489+
unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {}

0 commit comments

Comments
 (0)