Skip to content

Commit 5acfcce

Browse files
committed
Dogfood new_uninit and maybe_uninit_slice in rustc_arena
1 parent 285fc7d commit 5acfcce

File tree

1 file changed

+16
-19
lines changed
  • compiler/rustc_arena/src

1 file changed

+16
-19
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
)]
1414
#![feature(core_intrinsics)]
1515
#![feature(dropck_eyepatch)]
16-
#![feature(raw_vec_internals)]
16+
#![feature(new_uninit)]
17+
#![feature(maybe_uninit_slice)]
1718
#![cfg_attr(test, feature(test))]
1819
#![allow(deprecated)]
1920

20-
extern crate alloc;
21-
2221
use rustc_data_structures::cold_path;
2322
use smallvec::SmallVec;
2423

@@ -27,12 +26,10 @@ use std::cell::{Cell, RefCell};
2726
use std::cmp;
2827
use std::intrinsics;
2928
use std::marker::{PhantomData, Send};
30-
use std::mem;
29+
use std::mem::{self, MaybeUninit};
3130
use std::ptr;
3231
use std::slice;
3332

34-
use alloc::raw_vec::RawVec;
35-
3633
/// An arena that can hold objects of only one type.
3734
pub struct TypedArena<T> {
3835
/// A pointer to the next object to be allocated.
@@ -52,15 +49,15 @@ pub struct TypedArena<T> {
5249

5350
struct TypedArenaChunk<T> {
5451
/// The raw storage for the arena chunk.
55-
storage: RawVec<T>,
52+
storage: Box<[MaybeUninit<T>]>,
5653
/// The number of valid entries in the chunk.
5754
entries: usize,
5855
}
5956

6057
impl<T> TypedArenaChunk<T> {
6158
#[inline]
6259
unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
63-
TypedArenaChunk { storage: RawVec::with_capacity(capacity), entries: 0 }
60+
TypedArenaChunk { storage: Box::new_uninit_slice(capacity), entries: 0 }
6461
}
6562

6663
/// Destroys this arena chunk.
@@ -80,19 +77,19 @@ impl<T> TypedArenaChunk<T> {
8077

8178
// Returns a pointer to the first allocated object.
8279
#[inline]
83-
fn start(&self) -> *mut T {
84-
self.storage.ptr()
80+
fn start(&mut self) -> *mut T {
81+
MaybeUninit::slice_as_mut_ptr(&mut self.storage)
8582
}
8683

8784
// Returns a pointer to the end of the allocated space.
8885
#[inline]
89-
fn end(&self) -> *mut T {
86+
fn end(&mut self) -> *mut T {
9087
unsafe {
9188
if mem::size_of::<T>() == 0 {
9289
// A pointer as large as possible for zero-sized elements.
9390
!0 as *mut T
9491
} else {
95-
self.start().add(self.storage.capacity())
92+
self.start().add(self.storage.len())
9693
}
9794
}
9895
}
@@ -226,10 +223,10 @@ impl<T> TypedArena<T> {
226223
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
227224
last_chunk.entries = used_bytes / mem::size_of::<T>();
228225

229-
// If the previous chunk's capacity is less than HUGE_PAGE
226+
// If the previous chunk's len is less than HUGE_PAGE
230227
// bytes, then this chunk will be least double the previous
231228
// chunk's size.
232-
new_cap = last_chunk.storage.capacity();
229+
new_cap = last_chunk.storage.len();
233230
if new_cap < HUGE_PAGE / elem_size {
234231
new_cap = new_cap.checked_mul(2).unwrap();
235232
}
@@ -239,7 +236,7 @@ impl<T> TypedArena<T> {
239236
// Also ensure that this chunk can fit `additional`.
240237
new_cap = cmp::max(additional, new_cap);
241238

242-
let chunk = TypedArenaChunk::<T>::new(new_cap);
239+
let mut chunk = TypedArenaChunk::<T>::new(new_cap);
243240
self.ptr.set(chunk.start());
244241
self.end.set(chunk.end());
245242
chunks.push(chunk);
@@ -301,7 +298,7 @@ unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
301298
chunk.destroy(chunk.entries);
302299
}
303300
}
304-
// RawVec handles deallocation of `last_chunk` and `self.chunks`.
301+
// Box handles deallocation of `last_chunk` and `self.chunks`.
305302
}
306303
}
307304
}
@@ -344,10 +341,10 @@ impl DroplessArena {
344341
// There is no need to update `last_chunk.entries` because that
345342
// field isn't used by `DroplessArena`.
346343

347-
// If the previous chunk's capacity is less than HUGE_PAGE
344+
// If the previous chunk's len is less than HUGE_PAGE
348345
// bytes, then this chunk will be least double the previous
349346
// chunk's size.
350-
new_cap = last_chunk.storage.capacity();
347+
new_cap = last_chunk.storage.len();
351348
if new_cap < HUGE_PAGE {
352349
new_cap = new_cap.checked_mul(2).unwrap();
353350
}
@@ -357,7 +354,7 @@ impl DroplessArena {
357354
// Also ensure that this chunk can fit `additional`.
358355
new_cap = cmp::max(additional, new_cap);
359356

360-
let chunk = TypedArenaChunk::<u8>::new(new_cap);
357+
let mut chunk = TypedArenaChunk::<u8>::new(new_cap);
361358
self.ptr.set(chunk.start());
362359
self.end.set(chunk.end());
363360
chunks.push(chunk);

0 commit comments

Comments
 (0)