Skip to content

Commit 7408de7

Browse files
committed
remove interior mutability from proc_macro arena
1 parent 0a6462b commit 7408de7

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

library/proc_macro/src/bridge/arena.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! as it is difficult to depend on crates from within `proc_macro`, due to it
55
//! being built at the same time as `std`.
66
7-
use std::cell::{Cell, RefCell};
87
use std::mem::MaybeUninit;
98
use std::ops::Range;
109
use std::{cmp, ptr, slice};
@@ -26,27 +25,22 @@ const HUGE_PAGE: usize = 2 * 1024 * 1024;
2625
/// This arena doesn't have support for allocating anything other than byte
2726
/// slices, as that is all that is necessary.
2827
pub(crate) struct Arena {
29-
start: Cell<*mut MaybeUninit<u8>>,
30-
end: Cell<*mut MaybeUninit<u8>>,
31-
chunks: RefCell<Vec<Box<[MaybeUninit<u8>]>>>,
28+
start: *mut MaybeUninit<u8>,
29+
end: *mut MaybeUninit<u8>,
30+
chunks: Vec<Box<[MaybeUninit<u8>]>>,
3231
}
3332

3433
impl Arena {
3534
pub(crate) fn new() -> Self {
36-
Arena {
37-
start: Cell::new(ptr::null_mut()),
38-
end: Cell::new(ptr::null_mut()),
39-
chunks: RefCell::new(Vec::new()),
40-
}
35+
Arena { start: ptr::null_mut(), end: ptr::null_mut(), chunks: Vec::new() }
4136
}
4237

4338
/// Add a new chunk with at least `additional` free bytes.
4439
#[inline(never)]
4540
#[cold]
46-
fn grow(&self, additional: usize) {
47-
let mut chunks = self.chunks.borrow_mut();
41+
fn grow(&mut self, additional: usize) {
4842
let mut new_cap;
49-
if let Some(last_chunk) = chunks.last_mut() {
43+
if let Some(last_chunk) = self.chunks.last_mut() {
5044
// If the previous chunk's len is less than HUGE_PAGE
5145
// bytes, then this chunk will be least double the previous
5246
// chunk's size.
@@ -60,48 +54,46 @@ impl Arena {
6054

6155
let mut chunk = Box::new_uninit_slice(new_cap);
6256
let Range { start, end } = chunk.as_mut_ptr_range();
63-
self.start.set(start);
64-
self.end.set(end);
65-
chunks.push(chunk);
57+
self.start = start;
58+
self.end = end;
59+
self.chunks.push(chunk);
6660
}
6761

6862
/// Allocates a byte slice with specified size from the current memory
6963
/// chunk. Returns `None` if there is no free space left to satisfy the
7064
/// request.
71-
#[allow(clippy::mut_from_ref)]
72-
fn alloc_raw_without_grow(&self, bytes: usize) -> Option<&mut [MaybeUninit<u8>]> {
73-
let start = self.start.get().addr();
74-
let old_end = self.end.get();
65+
fn alloc_raw_without_grow(&mut self, bytes: usize) -> Option<&mut [MaybeUninit<u8>]> {
66+
let start = self.start.addr();
67+
let old_end = self.end;
7568
let end = old_end.addr();
7669

7770
let new_end = end.checked_sub(bytes)?;
7871
if start <= new_end {
7972
let new_end = old_end.with_addr(new_end);
80-
self.end.set(new_end);
73+
self.end = new_end;
8174
// SAFETY: `bytes` bytes starting at `new_end` were just reserved.
8275
Some(unsafe { slice::from_raw_parts_mut(new_end, bytes) })
8376
} else {
8477
None
8578
}
8679
}
8780

88-
fn alloc_raw(&self, bytes: usize) -> &mut [MaybeUninit<u8>] {
81+
fn alloc_raw(&mut self, bytes: usize) -> &mut [MaybeUninit<u8>] {
8982
if bytes == 0 {
9083
return &mut [];
9184
}
9285

93-
loop {
94-
if let Some(a) = self.alloc_raw_without_grow(bytes) {
95-
break a;
96-
}
97-
// No free space left. Allocate a new chunk to satisfy the request.
98-
// On failure the grow will panic or abort.
99-
self.grow(bytes);
86+
if let Some(a) = self.alloc_raw_without_grow(bytes) {
87+
// SAFETY: the lifetime is extended here, but then constrained again in `alloc_str`.
88+
return unsafe { &mut *(a as *mut _) };
10089
}
90+
// No free space left. Allocate a new chunk to satisfy the request.
91+
// On failure the grow will panic or abort.
92+
self.grow(bytes);
93+
self.alloc_raw_without_grow(bytes).unwrap()
10194
}
10295

103-
#[allow(clippy::mut_from_ref)] // arena allocator
104-
pub(crate) fn alloc_str<'a>(&'a self, string: &str) -> &'a mut str {
96+
pub(crate) fn alloc_str<'a>(&'a mut self, string: &str) -> &'a mut str {
10597
let alloc = self.alloc_raw(string.len());
10698
let bytes = alloc.write_copy_of_slice(string.as_bytes());
10799

0 commit comments

Comments
 (0)