Skip to content

Commit 84f494d

Browse files
CoAlloc: library/alloc now uses macros, and no ICE. But meta_num_slots macro is hardcoded - TO FIX.
1 parent b243399 commit 84f494d

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

library/alloc/src/boxed.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<T> Box<[T]> {
643643
#[must_use]
644644
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
645645
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
646-
unsafe { RawVec::<T, Global, false>::with_capacity(len).into_box(len) }
646+
unsafe { RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity(len).into_box(len) }
647647
}
648648

649649
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -669,7 +669,7 @@ impl<T> Box<[T]> {
669669
#[must_use]
670670
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
671671
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
672-
unsafe { RawVec::<T, Global, false>::with_capacity_zeroed(len).into_box(len) }
672+
unsafe { RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_zeroed(len).into_box(len) }
673673
}
674674

675675
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -701,7 +701,7 @@ impl<T> Box<[T]> {
701701
Err(_) => return Err(AllocError),
702702
};
703703
let ptr = Global.allocate(layout)?;
704-
Ok(RawVec::<T, Global, false>::from_raw_parts_in(
704+
Ok(RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::from_raw_parts_in(
705705
ptr.as_mut_ptr() as *mut _,
706706
len,
707707
Global,
@@ -738,7 +738,7 @@ impl<T> Box<[T]> {
738738
Err(_) => return Err(AllocError),
739739
};
740740
let ptr = Global.allocate_zeroed(layout)?;
741-
Ok(RawVec::<T, Global, false>::from_raw_parts_in(
741+
Ok(RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::from_raw_parts_in(
742742
ptr.as_mut_ptr() as *mut _,
743743
len,
744744
Global,
@@ -781,7 +781,7 @@ where
781781
#[must_use]
782782
#[allow(unused_braces)]
783783
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
784-
unsafe { RawVec::<T, A, false>::with_capacity_in(len, alloc).into_box(len) }
784+
unsafe { RawVec::<T, A, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_in(len, alloc).into_box(len) }
785785
}
786786

787787
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -810,7 +810,7 @@ where
810810
#[must_use]
811811
#[allow(unused_braces)]
812812
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
813-
unsafe { RawVec::<T, A, false>::with_capacity_zeroed_in(len, alloc).into_box(len) }
813+
unsafe { RawVec::<T, A, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_zeroed_in(len, alloc).into_box(len) }
814814
}
815815
}
816816

@@ -1516,7 +1516,7 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
15161516
fn from(slice: &[T]) -> Box<[T]> {
15171517
let len = slice.len();
15181518
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
1519-
let buf = RawVec::<T, Global, false>::with_capacity(len);
1519+
let buf = RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity(len);
15201520
unsafe {
15211521
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15221522
buf.into_box(slice.len()).assume_init()
@@ -2051,7 +2051,7 @@ where
20512051
fn clone(&self) -> Self {
20522052
let alloc = Box::allocator(self).clone();
20532053
// false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2054-
self.to_vec_in::<A, false>(alloc).into_boxed_slice()
2054+
self.to_vec_in::<A, {CO_ALLOC_PREF_META_NO!()}>(alloc).into_boxed_slice()
20552055
}
20562056

20572057
fn clone_from(&mut self, other: &Self) {

library/alloc/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ macro_rules! __rust_force_expr {
127127

128128
// ----- CoAlloc constant-like macros:
129129

130-
// @FIXME Docs
130+
/// Coallocation option/parameter about using metadata that does prefer to use meta data. This is of type [::alloc::co_alloc::CoAllocMetaNumSlotsPref] (but not a whole []::alloc::co_alloc::CoAllocPref]).
131131
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
132132
#[macro_export]
133133
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ONE {
@@ -136,7 +136,7 @@ macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ONE {
136136
};
137137
}
138138

139-
// @FIXME Docs
139+
/// Coallocation option/parameter about using metadata that prefers NOT to use meta data. This is of type [::alloc::co_alloc::CoAllocMetaNumSlotsPref] (but not a whole []::alloc::co_alloc::CoAllocPref]).
140140
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
141141
#[macro_export]
142142
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ZERO {
@@ -145,7 +145,7 @@ macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ZERO {
145145
};
146146
}
147147

148-
// @FIXME Docs
148+
/// Default coallocation option/parameter about using metadata (whether to use meta data, or not). This is of type [::alloc::co_alloc::CoAllocMetaNumSlotsPref] (but not a whole []::alloc::co_alloc::CoAllocPref]).
149149
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
150150
#[macro_export]
151151
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_DEFAULT {
@@ -213,7 +213,7 @@ macro_rules! CO_ALLOC_PREF_DEFAULT {
213213
() => { $crate::CO_ALLOC_PREF_META_DEFAULT!() };
214214
}
215215

216-
// @FIXME Move to library/alloc - if possible:
216+
/// Coallocation preference for (internal) short term vectors.
217217
#[unstable(feature = "global_co_alloc", issue = "none")]
218218
//pub const SHORT_TERM_VEC_CO_ALLOC_PREF: bool = true;
219219
#[macro_export]

library/alloc/src/raw_vec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> RawVec<T, A, CO_ALLOC_PR
127127
where
128128
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
129129
{
130+
#[allow(dead_code)]
130131
const fn new_plain_metas() -> [A::CoAllocMeta; { meta_num_slots_default!(A) }] {
131132
loop {}
132133
}
@@ -531,7 +532,9 @@ where
531532
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
532533
default fn drop(&mut self) {
533534
if let Some((ptr, layout)) = self.current_memory() {
534-
if A::CO_ALLOCATES_WITH_META && CO_ALLOC_PREF {
535+
let meta_num_slots = crate::meta_num_slots!(A, CO_ALLOC_PREF);
536+
if meta_num_slots!=0 {
537+
debug_assert!(meta_num_slots==1, "Number of coallocation meta slots can be only 0 or 1, but it is {}!", meta_num_slots);
535538
let meta = self.metas[0];
536539
unsafe { self.alloc.co_deallocate(PtrAndMeta { ptr, meta }, layout) }
537540
} else {

library/alloc/src/vec/in_place_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ impl<T> Drop for InPlaceDstBufDrop<T> {
3636
#[inline]
3737
fn drop(&mut self) {
3838
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
39-
unsafe { super::Vec::<T, Global, false>::from_raw_parts(self.ptr, self.len, self.cap) };
39+
unsafe { super::Vec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::from_raw_parts(self.ptr, self.len, self.cap) };
4040
}
4141
}

library/alloc/src/vec/into_iter.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,16 @@ where
134134
// this creates less assembly
135135
self.cap = 0;
136136
self.buf = unsafe {
137-
// @FIXME The below if CO_ALLOC_PREF {..} else {..}
138-
// branching exists, because the following fails. Otherwise we'd have a snowball effect of wide spread of where...Global...
137+
// @FIXME The below if .. {..} else {..}
138+
// branching exists, because the following fails. Otherwise we'd have a snowball effect of wide spread of where...Global... bounds.
139139
//
140-
// NonNull::new_unchecked(RawVec::<T, Global, CO_ALLOC_PREF>::NEW.ptr())
141-
if CO_ALLOC_PREF {
142-
NonNull::new_unchecked(RawVec::<T, Global, true>::NEW.ptr())
140+
//NonNull::new_unchecked(RawVec::<T, Global, CO_ALLOC_PREF>::NEW.ptr());
141+
let meta_num_slots = crate::meta_num_slots!(A, CO_ALLOC_PREF);
142+
if meta_num_slots>0 {
143+
debug_assert!(meta_num_slots==1, "Number of coallocation meta slots can be only 0 or 1, but it is {}!", meta_num_slots);
144+
NonNull::new_unchecked(RawVec::<T, Global, {CO_ALLOC_PREF_META_YES!()}>::NEW.ptr())
143145
} else {
144-
NonNull::new_unchecked(RawVec::<T, Global, false>::NEW.ptr())
146+
NonNull::new_unchecked(RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::NEW.ptr())
145147
}
146148
};
147149
self.ptr = self.buf.as_ptr();

library/core/src/alloc/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl fmt::Display for AllocError {
5151
/// (Non-Null) Pointer and coallocation metadata.
5252
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
5353
#[derive(Clone, Copy, Debug)]
54-
pub struct PtrAndMeta<M: CoAllocMetaBase> {
54+
pub struct PtrAndMeta<M: ~const CoAllocMetaBase> {
5555
pub ptr: NonNull<u8>,
5656
pub meta: M,
5757
}
@@ -60,7 +60,7 @@ pub struct PtrAndMeta<M: CoAllocMetaBase> {
6060
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
6161
#[derive(Clone, Copy, Debug)]
6262
/// Used for results (from `CoAllocator`'s functions, where applicable).
63-
pub struct SliceAndMeta<M: CoAllocMetaBase> {
63+
pub struct SliceAndMeta<M: ~const CoAllocMetaBase> {
6464
pub slice: NonNull<[u8]>,
6565
pub meta: M,
6666
}
@@ -70,6 +70,7 @@ pub struct SliceAndMeta<M: CoAllocMetaBase> {
7070
pub type SliceAndMetaResult<M> = Result<SliceAndMeta<M>, AllocError>;
7171

7272
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
73+
#[const_trait]
7374
pub trait CoAllocMetaBase: Clone + Copy {
7475
/// NOT for public use. This MAY BE REMOVED or CHANGED.
7576
///
@@ -90,7 +91,7 @@ pub struct CoAllocMetaPlain {}
9091
const CO_ALLOC_META_PLAIN: CoAllocMetaPlain = CoAllocMetaPlain {};
9192

9293
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
93-
impl CoAllocMetaBase for CoAllocMetaPlain {
94+
impl const CoAllocMetaBase for CoAllocMetaPlain {
9495
const ZERO_METAS: [Self; 0] = [];
9596
const ONE_METAS: [Self; 1] = [CO_ALLOC_META_PLAIN];
9697

@@ -188,7 +189,7 @@ pub unsafe trait Allocator {
188189
///
189190
/// @FIXME Validate (preferrable at compile time, otherwise as a test) that this type's
190191
/// alignment <= `usize` alignment.
191-
type CoAllocMeta: CoAllocMetaBase = CoAllocMetaPlain;
192+
type CoAllocMeta: ~const CoAllocMetaBase = CoAllocMetaPlain;
192193

193194
/// Attempts to allocate a block of memory.
194195
///

0 commit comments

Comments
 (0)