Skip to content

Commit c5e76a3

Browse files
CoAlloc: Moved CoAllocMeta to both Allocator and GlobalAlloc.
1 parent af76916 commit c5e76a3

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
22

3-
use core::alloc::{self, GlobalCoAllocMeta, LayoutError, PtrAndMeta};
3+
use core::alloc::{self, LayoutError, PtrAndMeta};
44
use core::cmp;
55
use core::intrinsics;
66
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
@@ -62,7 +62,7 @@ where
6262
//pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}],
6363
//pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()],
6464
pub(crate) metas:
65-
[GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
65+
[A::CoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
6666
}
6767

6868
impl<T, const COOP_PREF: bool> RawVec<T, Global, COOP_PREF>
@@ -136,11 +136,12 @@ where
136136
/// the returned `RawVec`.
137137
pub const fn new_in(alloc: A) -> Self {
138138
// `cap: 0` means "unallocated". zero-sized types are ignored.
139+
#[allow(unreachable_code)] // @FIXME CoAlloc
139140
Self {
140141
ptr: Unique::dangling(),
141142
cap: 0,
142143
alloc,
143-
metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/};
144+
metas: [loop {}; // @FIXME CoAlloc
144145
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
145146
}
146147
}
@@ -215,11 +216,12 @@ where
215216
// Allocators currently return a `NonNull<[u8]>` whose length
216217
// matches the size requested. If that ever changes, the capacity
217218
// here should change to `ptr.len() / mem::size_of::<T>()`.
219+
#[allow(unreachable_code)] // @FIXME CoAlloc
218220
Self {
219221
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
220222
cap: capacity,
221223
alloc,
222-
metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/};
224+
metas: [loop {}; // @FIXME CoAlloc
223225
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
224226
}
225227
}
@@ -237,11 +239,12 @@ where
237239
/// guaranteed.
238240
#[inline]
239241
pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self {
242+
#[allow(unreachable_code)] //@FIXME CoAlloc
240243
Self {
241244
ptr: unsafe { Unique::new_unchecked(ptr) },
242245
cap: capacity,
243246
alloc,
244-
metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/};
247+
metas: [loop {}; //@FIXME CoAlloc
245248
alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREF)],
246249
}
247250
}

library/core/src/alloc/global.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use crate::alloc::GlobalCoAllocMeta;
21
use crate::alloc::Layout;
32
use crate::cmp;
43
use crate::ptr;
54

65
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
76
#[allow(missing_debug_implementations)]
87
/// Used for parameters and results (to/from `GlobalCoAllocator`'s functions, where applicable).
9-
pub struct RawAndMeta {
8+
pub struct RawAndMeta<M: Clone + Copy> {
109
pub ptr: *mut u8,
11-
pub meta: GlobalCoAllocMeta,
10+
pub meta: M,
1211
}
1312

1413
/// A memory allocator that can be registered as the standard library’s default
@@ -130,6 +129,11 @@ pub struct RawAndMeta {
130129
/// having side effects.
131130
#[stable(feature = "global_alloc", since = "1.28.0")]
132131
pub unsafe trait GlobalAlloc {
132+
/// @FIXME Validate (preferrable at compile time, otherwise as a test) that this type's
133+
/// alignment <= `usize` alignment.
134+
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
135+
type CoAllocMeta: Clone + Copy = ();
136+
133137
/// Allocate memory as described by the given `layout`.
134138
///
135139
/// Returns a pointer to newly-allocated memory,
@@ -166,7 +170,7 @@ pub unsafe trait GlobalAlloc {
166170
unsafe fn alloc(&self, layout: Layout) -> *mut u8;
167171

168172
#[unstable(feature = "global_co_alloc", issue = "none")]
169-
unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {
173+
unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta<Self::CoAllocMeta>) {
170174
panic!("@FIXME")
171175
}
172176

@@ -186,7 +190,7 @@ pub unsafe trait GlobalAlloc {
186190
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
187191

188192
#[unstable(feature = "global_co_alloc", issue = "none")]
189-
unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {
193+
unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta<Self::CoAllocMeta>, _layout: Layout) {
190194
panic!("@FIXME")
191195
}
192196

@@ -223,7 +227,11 @@ pub unsafe trait GlobalAlloc {
223227
}
224228

225229
#[unstable(feature = "global_co_alloc", issue = "none")]
226-
unsafe fn co_alloc_zeroed(&self, layout: Layout, mut result: &mut RawAndMeta) {
230+
unsafe fn co_alloc_zeroed(
231+
&self,
232+
layout: Layout,
233+
mut result: &mut RawAndMeta<Self::CoAllocMeta>,
234+
) {
227235
let size = layout.size();
228236
// SAFETY: the safety contract for `alloc` must be upheld by the caller.
229237
unsafe { self.co_alloc(layout, &mut result) };
@@ -310,10 +318,10 @@ pub unsafe trait GlobalAlloc {
310318
#[unstable(feature = "global_co_alloc", issue = "none")]
311319
unsafe fn co_realloc(
312320
&self,
313-
ptr_and_meta: RawAndMeta,
321+
ptr_and_meta: RawAndMeta<Self::CoAllocMeta>,
314322
layout: Layout,
315323
new_size: usize,
316-
mut result: &mut RawAndMeta,
324+
mut result: &mut RawAndMeta<Self::CoAllocMeta>,
317325
) {
318326
// SAFETY: the caller must ensure that the `new_size` does not overflow.
319327
// `layout.align()` comes from a `Layout` and is thus guaranteed to be valid.

library/core/src/alloc/mod.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,6 @@ use crate::error::Error;
2525
use crate::fmt;
2626
use crate::ptr::{self, NonNull};
2727

28-
// @FIXME Make this target-specific
29-
/// Metadata for `Vec/VecDeque/RawVec` to assist the allocator. Make sure its
30-
/// alignment is not bigger than alignment of `usize`. Otherwise, even if (a
31-
/// particular) `Vec/VecDeque/RawVec` generic instance doesn't use cooperation,
32-
/// it would increase size of that `Vec/VecDeque/RawVec` because of alignment
33-
/// rules! @FIXME compile time test that `GlobalCoAllocMeta` alignment <=
34-
/// `usize` alignment.
35-
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
36-
#[allow(missing_debug_implementations)]
37-
#[derive(Clone, Copy)]
38-
pub struct GlobalCoAllocMeta {
39-
//pub one: usize,
40-
/*pub two: usize,
41-
pub three: usize,
42-
pub four: usize,*/
43-
}
44-
4528
/// The `AllocError` error indicates an allocation failure
4629
/// that may be due to resource exhaustion or to
4730
/// something wrong when combining the given input arguments with this
@@ -68,18 +51,20 @@ impl fmt::Display for AllocError {
6851
/// (Non-Null) Pointer and coallocation metadata.
6952
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
7053
#[allow(missing_debug_implementations)]
71-
pub struct PtrAndMeta {
54+
#[derive(Clone, Copy)]
55+
pub struct PtrAndMeta<M: Clone + Copy> {
7256
pub ptr: NonNull<u8>,
73-
pub meta: GlobalCoAllocMeta,
57+
pub meta: M,
7458
}
7559

7660
/// (NonNull) Slice and coallocation metadata.
7761
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
7862
#[allow(missing_debug_implementations)]
63+
#[derive(Clone, Copy)]
7964
/// Used for results (from `CoAllocator`'s functions, where applicable).
80-
pub struct SliceAndMeta {
65+
pub struct SliceAndMeta<M: Clone + Copy> {
8166
pub slice: NonNull<[u8]>,
82-
pub meta: GlobalCoAllocMeta,
67+
pub meta: M,
8368
}
8469

8570
#[unstable(feature = "global_co_alloc_short_term_pref", issue = "none")]
@@ -94,8 +79,9 @@ macro_rules! SHORT_TERM_VEC_PREFERS_COOP {
9479
/// `Result` of `SliceAndMeta` or `AllocError`.
9580
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
9681
#[allow(missing_debug_implementations)]
97-
pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>;
82+
pub type SliceAndMetaResult<M> = Result<SliceAndMeta<M>, AllocError>;
9883

84+
// @FIXME REMOVE
9985
/// Return 0 or 1, indicating whether to use coallocation metadata or not.
10086
/// Param `coop_preferred` - if false, then this returns `0`, regardless of
10187
/// whether allocator `A` is cooperative.
@@ -172,6 +158,8 @@ pub unsafe trait Allocator {
172158
// It applies to the global (default) allocator only. And/or System allocator?! @FIXME
173159
const CO_ALLOCATES_WITH_META: bool = false;
174160

161+
/// @FIXME Validate (preferrable at compile time, otherwise as a test) that this type's
162+
/// alignment <= `usize` alignment.
175163
type CoAllocMeta: Clone + Copy = ();
176164

177165
/// Attempts to allocate a block of memory.
@@ -196,7 +184,7 @@ pub unsafe trait Allocator {
196184
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
197185
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
198186

199-
fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {
187+
fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult<Self::CoAllocMeta>) {
200188
panic!("FIXME")
201189
}
202190

@@ -222,7 +210,11 @@ pub unsafe trait Allocator {
222210
Ok(ptr)
223211
}
224212

225-
fn co_allocate_zeroed(&self, layout: Layout, mut result: &mut SliceAndMetaResult) {
213+
fn co_allocate_zeroed(
214+
&self,
215+
layout: Layout,
216+
mut result: &mut SliceAndMetaResult<Self::CoAllocMeta>,
217+
) {
226218
self.co_allocate(layout, &mut result);
227219
if let Ok(SliceAndMeta { slice, .. }) = result {
228220
// SAFETY: `alloc` returns a valid memory block
@@ -241,7 +233,7 @@ pub unsafe trait Allocator {
241233
/// [*fit*]: #memory-fitting
242234
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
243235

244-
unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {
236+
unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta<Self::CoAllocMeta>, _layout: Layout) {
245237
panic!("FIXME")
246238
}
247239

@@ -311,10 +303,10 @@ pub unsafe trait Allocator {
311303

312304
unsafe fn co_grow(
313305
&self,
314-
ptr_and_meta: PtrAndMeta,
306+
ptr_and_meta: PtrAndMeta<Self::CoAllocMeta>,
315307
old_layout: Layout,
316308
new_layout: Layout,
317-
mut result: &mut SliceAndMetaResult,
309+
mut result: &mut SliceAndMetaResult<Self::CoAllocMeta>,
318310
) {
319311
debug_assert!(
320312
new_layout.size() >= old_layout.size(),
@@ -405,10 +397,10 @@ pub unsafe trait Allocator {
405397

406398
unsafe fn co_grow_zeroed(
407399
&self,
408-
ptr_and_meta: PtrAndMeta,
400+
ptr_and_meta: PtrAndMeta<Self::CoAllocMeta>,
409401
old_layout: Layout,
410402
new_layout: Layout,
411-
mut result: &mut SliceAndMetaResult,
403+
mut result: &mut SliceAndMetaResult<Self::CoAllocMeta>,
412404
) {
413405
debug_assert!(
414406
new_layout.size() >= old_layout.size(),
@@ -500,10 +492,10 @@ pub unsafe trait Allocator {
500492

501493
unsafe fn co_shrink(
502494
&self,
503-
ptr_and_meta: PtrAndMeta,
495+
ptr_and_meta: PtrAndMeta<Self::CoAllocMeta>,
504496
old_layout: Layout,
505497
new_layout: Layout,
506-
mut result: &mut SliceAndMetaResult,
498+
mut result: &mut SliceAndMetaResult<Self::CoAllocMeta>,
507499
) {
508500
debug_assert!(
509501
new_layout.size() <= old_layout.size(),

0 commit comments

Comments
 (0)