Skip to content

Commit 3a16f1d

Browse files
Vec & VecDeque use cases have COOP_PREFERRED. NOT COMPILABLE.
1 parent 546d076 commit 3a16f1d

File tree

12 files changed

+68
-39
lines changed

12 files changed

+68
-39
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#![stable(feature = "rust1", since = "1.0.0")]
1111
use core::alloc::{self, GlobalAlloc};
12+
use crate::vec::DEFAULT_COOP_PREFERRED;
1213
use core::cmp::{self, Ordering};
1314
use core::fmt;
1415
use core::hash::{Hash, Hasher};
@@ -93,11 +94,10 @@ mod tests;
9394
#[cfg_attr(not(test), rustc_diagnostic_item = "VecDeque")]
9495
#[stable(feature = "rust1", since = "1.0.0")]
9596
#[rustc_insignificant_dtor]
96-
// @TODO
9797
pub struct VecDeque<
9898
T,
9999
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
100-
const COOP_PREFERRED: bool = true
100+
const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED
101101
>
102102
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
103103
{
@@ -2906,7 +2906,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
29062906
if other.head != 0 {
29072907
ptr::copy(buf.add(other.head), buf, len);
29082908
}
2909-
// @TODO:
2909+
// @FIXME: COOP
29102910
Vec::from_raw_parts_in(buf, len, cap, alloc)
29112911
}
29122912
}

library/alloc/src/raw_vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::alloc::{Allocator, Global, Layout};
1414
use crate::boxed::Box;
1515
use crate::collections::TryReserveError;
1616
use crate::collections::TryReserveErrorKind::*;
17+
use crate::vec::DEFAULT_COOP_PREFERRED;
1718

1819
#[cfg(test)]
1920
mod tests;
@@ -49,8 +50,7 @@ enum AllocInit {
4950
/// `usize::MAX`. This means that you need to be careful when round-tripping this type with a
5051
/// `Box<[T]>`, since `capacity()` won't yield the length.
5152
#[allow(missing_debug_implementations)]
52-
// @TODO apply `_coop` with logical && to `A::IsCoAllocator`
53-
pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = true>
53+
pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED>
5454
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
5555
{
5656
ptr: Unique<T>,
@@ -506,12 +506,12 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
506506
}
507507
}
508508

509-
// @TODO Custom
509+
// @FIXME Custom
510510
unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED>
511511
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: {
512512
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
513513
fn drop(&mut self) {
514-
// @TODO
514+
// @TOFIXMEDO
515515
if let Some((ptr, layout)) = self.current_memory() {
516516
unsafe { self.alloc.deallocate(ptr, layout) }
517517
}

library/alloc/src/vec/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,24 +397,34 @@ mod spec_extend;
397397
#[stable(feature = "rust1", since = "1.0.0")]
398398
#[cfg_attr(not(test), rustc_diagnostic_item = "Vec")]
399399
#[rustc_insignificant_dtor]
400-
// @TODO _coop
401-
pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = true>
400+
pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED>
402401
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
403402
{
404403
buf: RawVec<T, A, COOP_PREFERRED>,
405404
len: usize,
406405
}
407406

408-
#[unstable(feature = "global_co_alloc_vec", issue = "none")]
409-
pub type CoVec<T> = Vec<T, Global, true>;
407+
#[unstable(feature = "global_co_alloc_covec", issue = "none")]
408+
pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, true>;
409+
410+
/// "Plain" Vec. Not "cooperative" - not carrying extra data to assist the allocator.
411+
#[unstable(feature = "global_co_alloc_plvec", issue = "none")]
412+
pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, false>;
413+
414+
/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter.
415+
#[unstable(feature = "global_co_alloc_def", issue = "none")]
416+
pub const DEFAULT_COOP_PREFERRED: bool = true;
417+
418+
/// "Default" Vec. Either "cooperative" or not - as specified by `DEFAULT_COOP_PREFERRED`. The
419+
/// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the
420+
/// author considered using `CoVec` or `PlVec`, but left it to default instead.
421+
#[unstable(feature = "global_co_alloc_defvec", issue = "none")]
422+
pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, DEFAULT_COOP_PREFERRED>;
410423

411-
/// "Plain" Vec.
412-
#[unstable(feature = "global_co_alloc_vec", issue = "none")]
413-
pub type PlVec<T> = Vec<T, Global, false>;
414424

415-
/// "Weighted" Vec.
416-
/// weight means how much it wants to cooperate. 0 = always pack; u8::MAX = always coop (if `Global` supports it).
417-
/// @TODO Weighing on the side of Allocator - const fn.
425+
/// "Weighted cooperative" Vec. Weight means how much it wants to cooperate (with the allocator). 0
426+
/// = always pack; u8::MAX = always cooperate (if `Global` supports it).
427+
/// @FIXME A `pub const` threshold.
418428
#[unstable(feature = "global_co_alloc_vec", issue = "none")]
419429
pub type WeVec<T, const weight: u8> = Vec<T, Global, {weight>1}>;
420430

library/core/src/alloc/global.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub unsafe trait GlobalAlloc {
166166
unsafe fn alloc(&self, layout: Layout) -> *mut u8;
167167

168168
#[unstable(feature = "global_co_alloc", issue = "none")]
169-
unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("TODO")}
169+
unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("@FIXME")}
170170

171171
/// Deallocate the block of memory at the given `ptr` pointer with the given `layout`.
172172
///
@@ -184,7 +184,7 @@ pub unsafe trait GlobalAlloc {
184184
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
185185

186186
#[unstable(feature = "global_co_alloc", issue = "none")]
187-
unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("TODO")}
187+
unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("@FIXME")}
188188

189189
/// Behaves like `alloc`, but also ensures that the contents
190190
/// are set to zero before being returned.

library/core/src/alloc/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::error::Error;
2626
use crate::fmt;
2727
use crate::ptr::{self, NonNull};
2828

29-
// @TODO Make this target-specific
29+
// @FIXME Make this target-specific
3030
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
3131
#[allow(missing_debug_implementations)]
3232
#[derive(Clone, Copy)]
@@ -157,7 +157,8 @@ pub unsafe trait Allocator {
157157
// Can't have: const type Xyz;
158158
/// If this is any type with non-zero size, then the actual `Allocator` implementation supports cooperative functions (`co_*`) as first class citizens.
159159
//type IsCoAllocator = ();
160-
// It applies to the global (default) allocator only. And/or System allocator?! TODO
160+
// It applies to the global (default) allocator only. And/or System allocator?! @FIXME
161+
// @FIXME make false by default
161162
const IS_CO_ALLOCATOR: bool = true;
162163

163164
/// Attempts to allocate a block of memory.
@@ -182,7 +183,7 @@ pub unsafe trait Allocator {
182183
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
183184
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
184185

185-
fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("TODO")}
186+
fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("FIXME")}
186187

187188
/// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized.
188189
///
@@ -230,7 +231,7 @@ pub unsafe trait Allocator {
230231
/// [*fit*]: #memory-fitting
231232
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
232233

233-
unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("TODO")}
234+
unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("FIXME")}
234235

235236
/// Attempts to extend the memory block.
236237
///
@@ -528,7 +529,7 @@ pub unsafe trait Allocator {
528529
}
529530
}
530531

531-
// @TODO
532+
// @FIXME
532533
#[unstable(feature = "allocator_api", issue = "32838")]
533534
unsafe impl<A> Allocator for &A
534535
where

library/std/src/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl System {
197197
}
198198
}
199199

200-
// @TODO
200+
// @FIXME
201201
// The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl,
202202
// which is in `std::sys::*::alloc`.
203203
#[unstable(feature = "allocator_api", issue = "32838")]

library/std/src/io/cursor.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::io::prelude::*;
66
use crate::alloc::Allocator;
77
use crate::cmp;
88
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
9+
use core::alloc;
910

1011
/// A `Cursor` wraps an in-memory buffer and provides it with a
1112
/// [`Seek`] implementation.
@@ -397,11 +398,12 @@ fn slice_write_vectored(
397398
}
398399

399400
/// Reserves the required space, and pads the vec with 0s if necessary.
400-
fn reserve_and_pad<A: Allocator>(
401+
fn reserve_and_pad<A: Allocator, const COOP_PREFERRED: bool>(
401402
pos_mut: &mut u64,
402-
vec: &mut Vec<u8, A>,
403+
vec: &mut Vec<u8, A, COOP_PREFERRED>,
403404
buf_len: usize,
404-
) -> io::Result<usize> {
405+
) -> io::Result<usize>
406+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
405407
let pos: usize = (*pos_mut).try_into().map_err(|_| {
406408
io::const_io_error!(
407409
ErrorKind::InvalidInput,
@@ -440,9 +442,10 @@ fn reserve_and_pad<A: Allocator>(
440442

441443
/// Writes the slice to the vec without allocating
442444
/// # Safety: vec must have buf.len() spare capacity
443-
unsafe fn vec_write_unchecked<A>(pos: usize, vec: &mut Vec<u8, A>, buf: &[u8]) -> usize
445+
unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>(pos: usize, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> usize
444446
where
445447
A: Allocator,
448+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
446449
{
447450
debug_assert!(vec.capacity() >= pos + buf.len());
448451
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
@@ -458,9 +461,10 @@ where
458461
/// This also allows for the vec body to be empty, but with a position of N.
459462
/// This means that [`Write`] will pad the vec with 0 initially,
460463
/// before writing anything from that point
461-
fn vec_write<A>(pos_mut: &mut u64, vec: &mut Vec<u8, A>, buf: &[u8]) -> io::Result<usize>
464+
fn vec_write<A, const COOP_PREFERRED: bool>(pos_mut: &mut u64, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> io::Result<usize>
462465
where
463466
A: Allocator,
467+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
464468
{
465469
let buf_len = buf.len();
466470
let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
@@ -489,13 +493,14 @@ where
489493
/// This also allows for the vec body to be empty, but with a position of N.
490494
/// This means that [`Write`] will pad the vec with 0 initially,
491495
/// before writing anything from that point
492-
fn vec_write_vectored<A>(
496+
fn vec_write_vectored<A, const COOP_PREFERRED: bool>(
493497
pos_mut: &mut u64,
494-
vec: &mut Vec<u8, A>,
498+
vec: &mut Vec<u8, A, COOP_PREFERRED>,
495499
bufs: &[IoSlice<'_>],
496500
) -> io::Result<usize>
497501
where
498502
A: Allocator,
503+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
499504
{
500505
// For safety reasons, we don't want this sum to overflow ever.
501506
// If this saturates, the reserve should panic to avoid any unsound writing.
@@ -543,9 +548,10 @@ impl Write for Cursor<&mut [u8]> {
543548
}
544549

545550
#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
546-
impl<A> Write for Cursor<&mut Vec<u8, A>>
551+
impl<A, const COOP_PREFERRED: bool> Write for Cursor<&mut Vec<u8, A, COOP_PREFERRED>>
547552
where
548553
A: Allocator,
554+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
549555
{
550556
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
551557
vec_write(&mut self.pos, self.inner, buf)
@@ -567,9 +573,10 @@ where
567573
}
568574

569575
#[stable(feature = "rust1", since = "1.0.0")]
570-
impl<A> Write for Cursor<Vec<u8, A>>
576+
impl<A, const COOP_PREFERRED: bool> Write for Cursor<Vec<u8, A, COOP_PREFERRED>>
571577
where
572578
A: Allocator,
579+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
573580
{
574581
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
575582
vec_write(&mut self.pos, &mut self.inner, buf)

library/std/src/io/impls.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ impl Write for &mut [u8] {
378378
/// Write is implemented for `Vec<u8>` by appending to the vector.
379379
/// The vector will grow as needed.
380380
#[stable(feature = "rust1", since = "1.0.0")]
381-
impl<A: Allocator> Write for Vec<u8, A> {
381+
impl<A: Allocator, const COOP_PREFERRED: bool> Write for Vec<u8, A, COOP_PREFERRED>
382+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
382383
#[inline]
383384
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
384385
self.extend_from_slice(buf);

library/std/src/sys/hermit/thread_local_dtor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#![cfg(target_thread_local)]
22
#![unstable(feature = "thread_local_internals", issue = "none")]
3+
#![feature(global_co_alloc_plvec)]
34

45
// Simplify dtor registration by using a list of destructors.
56
// The this solution works like the implementation of macOS and
67
// doesn't additional OS support
78

89
use crate::cell::Cell;
910
use crate::ptr;
11+
use core::alloc::PlVec;
1012

1113
#[thread_local]
1214
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
1315

14-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
16+
type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
1517

1618
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1719
if DTORS.get().is_null() {

library/std/src/sys/solid/thread_local_dtor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#![cfg(target_thread_local)]
22
#![unstable(feature = "thread_local_internals", issue = "none")]
3+
#![feature(global_co_alloc_plvec)]
34

45
// Simplify dtor registration by using a list of destructors.
56

67
use super::{abi, itron::task};
78
use crate::cell::Cell;
89
use crate::ptr;
10+
use core::alloc::PlVec;
911

1012
#[thread_local]
1113
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
1214

13-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
15+
type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
1416

1517
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1618
if DTORS.get().is_null() {

0 commit comments

Comments
 (0)