Skip to content

Commit e8d7dbd

Browse files
CoAlloc: meta_num_slots... macros- WIP
1 parent c5e76a3 commit e8d7dbd

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
77
use core::ops::Drop;
88
use core::ptr::{self, NonNull, Unique};
99
use core::slice;
10+
use core::alloc::CoAllocMetaBase;
1011

1112
#[cfg(not(no_global_oom_handling))]
1213
use crate::alloc::handle_alloc_error;
@@ -136,12 +137,11 @@ where
136137
/// the returned `RawVec`.
137138
pub const fn new_in(alloc: A) -> Self {
138139
// `cap: 0` means "unallocated". zero-sized types are ignored.
139-
#[allow(unreachable_code)] // @FIXME CoAlloc
140140
Self {
141141
ptr: Unique::dangling(),
142142
cap: 0,
143143
alloc,
144-
metas: [loop {}; // @FIXME CoAlloc
144+
metas: [A::CoAllocMeta::new_plain(); // @FIXME CoAlloc
145145
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
146146
}
147147
}
@@ -221,7 +221,7 @@ where
221221
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
222222
cap: capacity,
223223
alloc,
224-
metas: [loop {}; // @FIXME CoAlloc
224+
metas: [A::CoAllocMeta::new_plain(); // @FIXME CoAlloc
225225
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
226226
}
227227
}
@@ -244,7 +244,7 @@ where
244244
ptr: unsafe { Unique::new_unchecked(ptr) },
245245
cap: capacity,
246246
alloc,
247-
metas: [loop {}; //@FIXME CoAlloc
247+
metas: [A::CoAllocMeta::new_plain(); //@FIXME CoAlloc
248248
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
249249
}
250250
}

library/core/src/alloc/global.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::alloc::Layout;
2+
use crate::alloc::{CoAllocMetaBase, CoAllocMetaPlain};
23
use crate::cmp;
34
use crate::ptr;
45

56
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
6-
#[allow(missing_debug_implementations)]
7+
#[derive(Debug)]
78
/// Used for parameters and results (to/from `GlobalCoAllocator`'s functions, where applicable).
8-
pub struct RawAndMeta<M: Clone + Copy> {
9+
pub struct RawAndMeta<M: CoAllocMetaBase> {
910
pub ptr: *mut u8,
1011
pub meta: M,
1112
}
@@ -129,10 +130,12 @@ pub struct RawAndMeta<M: Clone + Copy> {
129130
/// having side effects.
130131
#[stable(feature = "global_alloc", since = "1.28.0")]
131132
pub unsafe trait GlobalAlloc {
133+
/// NOT for public use. The default value MAY be REMOVED or CHANGED.
134+
///
132135
/// @FIXME Validate (preferrable at compile time, otherwise as a test) that this type's
133136
/// alignment <= `usize` alignment.
134137
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
135-
type CoAllocMeta: Clone + Copy = ();
138+
type CoAllocMeta: CoAllocMetaBase = CoAllocMetaPlain;
136139

137140
/// Allocate memory as described by the given `layout`.
138141
///

library/core/src/alloc/mod.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,17 @@ impl fmt::Display for AllocError {
5050

5151
/// (Non-Null) Pointer and coallocation metadata.
5252
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
53-
#[allow(missing_debug_implementations)]
54-
#[derive(Clone, Copy)]
55-
pub struct PtrAndMeta<M: Clone + Copy> {
53+
#[derive(Clone, Copy, Debug)]
54+
pub struct PtrAndMeta<M: CoAllocMetaBase> {
5655
pub ptr: NonNull<u8>,
5756
pub meta: M,
5857
}
5958

6059
/// (NonNull) Slice and coallocation metadata.
6160
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
62-
#[allow(missing_debug_implementations)]
63-
#[derive(Clone, Copy)]
61+
#[derive(Clone, Copy, Debug)]
6462
/// Used for results (from `CoAllocator`'s functions, where applicable).
65-
pub struct SliceAndMeta<M: Clone + Copy> {
63+
pub struct SliceAndMeta<M: CoAllocMetaBase> {
6664
pub slice: NonNull<[u8]>,
6765
pub meta: M,
6866
}
@@ -78,7 +76,6 @@ macro_rules! SHORT_TERM_VEC_PREFERS_COOP {
7876

7977
/// `Result` of `SliceAndMeta` or `AllocError`.
8078
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
81-
#[allow(missing_debug_implementations)]
8279
pub type SliceAndMetaResult<M> = Result<SliceAndMeta<M>, AllocError>;
8380

8481
// @FIXME REMOVE
@@ -158,9 +155,11 @@ pub unsafe trait Allocator {
158155
// It applies to the global (default) allocator only. And/or System allocator?! @FIXME
159156
const CO_ALLOCATES_WITH_META: bool = false;
160157

158+
/// NOT for public use. The default value MAY be REMOVED or CHANGED.
159+
///
161160
/// @FIXME Validate (preferrable at compile time, otherwise as a test) that this type's
162161
/// alignment <= `usize` alignment.
163-
type CoAllocMeta: Clone + Copy = ();
162+
type CoAllocMeta: CoAllocMetaBase = CoAllocMetaPlain;
164163

165164
/// Attempts to allocate a block of memory.
166165
///
@@ -588,3 +587,24 @@ where
588587
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
589588
}
590589
}
590+
591+
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
592+
pub trait CoAllocMetaBase: Clone + Copy {
593+
/// NOT for public use. This MAY BE REMOVED or CHANGED.
594+
///
595+
/// For EXPERIMENTATION only.
596+
fn new_plain() -> Self;
597+
}
598+
599+
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
600+
#[derive(Clone, Copy, Debug)]
601+
pub struct CoAllocMetaPlain {}
602+
603+
static CO_ALLOC_META_PLAIN: CoAllocMetaPlain = CoAllocMetaPlain {};
604+
605+
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
606+
impl CoAllocMetaBase for CoAllocMetaPlain {
607+
fn new_plain() -> Self {
608+
CO_ALLOC_META_PLAIN
609+
}
610+
}

0 commit comments

Comments
 (0)