Skip to content

Commit 90dab29

Browse files
CoAlloc: RawVec includes GlobalCoAllocMeta based on allocator; related bounds - NOT COMPILING (no ICE, but standard compiler error)
1 parent f4aa6f2 commit 90dab29

File tree

5 files changed

+104
-40
lines changed

5 files changed

+104
-40
lines changed

library/alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#![warn(deprecated_in_future)]
8787
#![warn(missing_debug_implementations)]
8888
#![warn(missing_docs)]
89+
#![allow(incomplete_features)]
8990
#![allow(explicit_outlives_requirements)]
9091
#![cfg_attr(not(bootstrap), warn(multiple_supertrait_upcastable))]
9192
//
@@ -124,6 +125,7 @@
124125
#![feature(extend_one)]
125126
#![feature(fmt_internals)]
126127
#![feature(fn_traits)]
128+
#![feature(generic_const_exprs)]
127129
#![feature(global_co_alloc_meta)]
128130
#![feature(hasher_prefixfree_extras)]
129131
#![feature(inline_const)]

library/alloc/src/raw_vec.rs

Lines changed: 31 additions & 13 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::{LayoutError, GlobalCoAllocMeta};
3+
use core::alloc::{self, LayoutError, GlobalCoAllocMeta};
44
use core::cmp;
55
use core::intrinsics;
66
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
@@ -49,12 +49,19 @@ enum AllocInit {
4949
/// `usize::MAX`. This means that you need to be careful when round-tripping this type with a
5050
/// `Box<[T]>`, since `capacity()` won't yield the length.
5151
#[allow(missing_debug_implementations)]
52-
pub(crate) struct RawVec<T, A: Allocator = Global> {
52+
// @TODO
53+
// 1. make const generic _coop come from the target specification
54+
// 2. apply `_coop` with logical && to `A::IsCoAllocator`
55+
pub(crate) struct RawVec<T, A: Allocator = Global, const _coop: bool = true>
56+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
57+
{
5358
ptr: Unique<T>,
5459
cap: usize,
5560
alloc: A,
56-
#[allow(dead_code)]
57-
pub(crate) meta: GlobalCoAllocMeta,
61+
// As of v1.67.0, `cmp` for `TypeId` is not `const`, unfortunately:
62+
//pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}],
63+
//pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()],
64+
pub(crate) meta: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots::<A>()],
5865
}
5966

6067
impl<T> RawVec<T, Global> {
@@ -104,7 +111,9 @@ impl<T> RawVec<T, Global> {
104111
}
105112
}
106113

107-
impl<T, A: Allocator> RawVec<T, A> {
114+
impl<T, A: Allocator> RawVec<T, A>
115+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
116+
{
108117
// Tiny Vecs are dumb. Skip to:
109118
// - 8 if the element size is 1, because any heap allocators is likely
110119
// to round up a request of less than 8 bytes to at least 8 bytes.
@@ -284,7 +293,9 @@ impl<T, A: Allocator> RawVec<T, A> {
284293
slf: &mut RawVec<T, A>,
285294
len: usize,
286295
additional: usize,
287-
) {
296+
)
297+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
298+
{
288299
handle_reserve(slf.grow_amortized(len, additional));
289300
}
290301

@@ -357,14 +368,18 @@ impl<T, A: Allocator> RawVec<T, A> {
357368
}
358369
}
359370

360-
impl<T, A: Allocator> RawVec<T, A> {
371+
impl<T, A: Allocator> RawVec<T, A>
372+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
373+
{
361374
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
362375
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
363-
fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
376+
fn needs_to_grow(&self, len: usize, additional: usize) -> bool
377+
{
364378
additional > self.capacity().wrapping_sub(len)
365379
}
366380

367-
fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) {
381+
fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize)
382+
{
368383
// Allocators currently return a `NonNull<[u8]>` whose length matches
369384
// the size requested. If that ever changes, the capacity here should
370385
// change to `ptr.len() / mem::size_of::<T>()`.
@@ -475,24 +490,27 @@ where
475490
memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
476491
}
477492

478-
unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
493+
unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A>
494+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
495+
{
479496
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
480497
default fn drop(&mut self) {
481498
if let Some((ptr, layout)) = self.current_memory() {
482-
unsafe { self.alloc.deallocate(ptr, layout) }
499+
unsafe { self.alloc.co_deallocate(ptr, layout) }
483500
}
484501
}
485502
}
486503

487-
unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> {
504+
// @TODO Custom
505+
/*unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> {
488506
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
489507
fn drop(&mut self) {
490508
// @TODO
491509
if let Some((ptr, layout)) = self.current_memory() {
492510
unsafe { self.alloc.deallocate(ptr, layout) }
493511
}
494512
}
495-
}
513+
}*/
496514

497515
// Central function for reserve error handling.
498516
#[cfg(not(no_global_oom_handling))]

library/alloc/src/slice.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ pub(crate) mod hack {
9797
// We shouldn't add inline attribute to this since this is used in
9898
// `vec!` macro mostly and causes perf regression. See #71204 for
9999
// discussion and perf results.
100-
pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> {
100+
pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A>
101+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
101102
unsafe {
102103
let len = b.len();
103104
let (b, alloc) = Box::into_raw_with_allocator(b);
@@ -107,21 +108,24 @@ pub(crate) mod hack {
107108

108109
#[cfg(not(no_global_oom_handling))]
109110
#[inline]
110-
pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> {
111+
pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A>
112+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
111113
T::to_vec(s, alloc)
112114
}
113115

114116
#[cfg(not(no_global_oom_handling))]
115117
pub trait ConvertVec {
116118
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
117119
where
118-
Self: Sized;
120+
Self: Sized,
121+
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:;
119122
}
120123

121124
#[cfg(not(no_global_oom_handling))]
122125
impl<T: Clone> ConvertVec for T {
123126
#[inline]
124-
default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
127+
default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
128+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
125129
struct DropGuard<'a, T, A: Allocator> {
126130
vec: &'a mut Vec<T, A>,
127131
num_init: usize,
@@ -158,7 +162,8 @@ pub(crate) mod hack {
158162
#[cfg(not(no_global_oom_handling))]
159163
impl<T: Copy> ConvertVec for T {
160164
#[inline]
161-
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
165+
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
166+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
162167
let mut v = Vec::with_capacity_in(s.len(), alloc);
163168
// SAFETY:
164169
// allocated above with the capacity of `s`, and initialize to `s.len()` in
@@ -436,6 +441,7 @@ impl<T> [T] {
436441
pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A>
437442
where
438443
T: Clone,
444+
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:
439445
{
440446
// N.B., see the `hack` module in this file for more details.
441447
hack::to_vec(self, alloc)
@@ -458,7 +464,8 @@ impl<T> [T] {
458464
#[rustc_allow_incoherent_impl]
459465
#[stable(feature = "rust1", since = "1.0.0")]
460466
#[inline]
461-
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
467+
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A>
468+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
462469
// N.B., see the `hack` module in this file for more details.
463470
hack::into_vec(self)
464471
}
@@ -769,14 +776,16 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
769776
////////////////////////////////////////////////////////////////////////////////
770777

771778
#[stable(feature = "rust1", since = "1.0.0")]
772-
impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> {
779+
impl<T, A: Allocator> Borrow<[T]> for Vec<T, A>
780+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
773781
fn borrow(&self) -> &[T] {
774782
&self[..]
775783
}
776784
}
777785

778786
#[stable(feature = "rust1", since = "1.0.0")]
779-
impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> {
787+
impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A>
788+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
780789
fn borrow_mut(&mut self) -> &mut [T] {
781790
&mut self[..]
782791
}

0 commit comments

Comments
 (0)