diff --git a/library/proc_macro/src/bridge/arena.rs b/library/proc_macro/src/bridge/arena.rs index bf5a5b5a81821..5f074bf6ac5d3 100644 --- a/library/proc_macro/src/bridge/arena.rs +++ b/library/proc_macro/src/bridge/arena.rs @@ -4,10 +4,9 @@ //! as it is difficult to depend on crates from within `proc_macro`, due to it //! being built at the same time as `std`. -use std::cell::{Cell, RefCell}; use std::mem::MaybeUninit; use std::ops::Range; -use std::{cmp, ptr, slice, str}; +use std::{cmp, ptr, slice}; // The arenas start with PAGE-sized chunks, and then each new chunk is twice as // big as its predecessor, up until we reach HUGE_PAGE-sized chunks, whereupon @@ -26,27 +25,22 @@ const HUGE_PAGE: usize = 2 * 1024 * 1024; /// This arena doesn't have support for allocating anything other than byte /// slices, as that is all that is necessary. pub(crate) struct Arena { - start: Cell<*mut MaybeUninit>, - end: Cell<*mut MaybeUninit>, - chunks: RefCell]>>>, + start: *mut MaybeUninit, + end: *mut MaybeUninit, + chunks: Vec]>>, } impl Arena { pub(crate) fn new() -> Self { - Arena { - start: Cell::new(ptr::null_mut()), - end: Cell::new(ptr::null_mut()), - chunks: RefCell::new(Vec::new()), - } + Arena { start: ptr::null_mut(), end: ptr::null_mut(), chunks: Vec::new() } } /// Add a new chunk with at least `additional` free bytes. #[inline(never)] #[cold] - fn grow(&self, additional: usize) { - let mut chunks = self.chunks.borrow_mut(); + fn grow(&mut self, additional: usize) { let mut new_cap; - if let Some(last_chunk) = chunks.last_mut() { + if let Some(last_chunk) = self.chunks.last_mut() { // If the previous chunk's len is less than HUGE_PAGE // bytes, then this chunk will be least double the previous // chunk's size. @@ -60,24 +54,23 @@ impl Arena { let mut chunk = Box::new_uninit_slice(new_cap); let Range { start, end } = chunk.as_mut_ptr_range(); - self.start.set(start); - self.end.set(end); - chunks.push(chunk); + self.start = start; + self.end = end; + self.chunks.push(chunk); } /// Allocates a byte slice with specified size from the current memory /// chunk. Returns `None` if there is no free space left to satisfy the /// request. - #[allow(clippy::mut_from_ref)] - fn alloc_raw_without_grow(&self, bytes: usize) -> Option<&mut [MaybeUninit]> { - let start = self.start.get().addr(); - let old_end = self.end.get(); + fn alloc_raw_without_grow(&mut self, bytes: usize) -> Option<&mut [MaybeUninit]> { + let start = self.start.addr(); + let old_end = self.end; let end = old_end.addr(); let new_end = end.checked_sub(bytes)?; if start <= new_end { let new_end = old_end.with_addr(new_end); - self.end.set(new_end); + self.end = new_end; // SAFETY: `bytes` bytes starting at `new_end` were just reserved. Some(unsafe { slice::from_raw_parts_mut(new_end, bytes) }) } else { @@ -85,23 +78,22 @@ impl Arena { } } - fn alloc_raw(&self, bytes: usize) -> &mut [MaybeUninit] { + fn alloc_raw(&mut self, bytes: usize) -> &mut [MaybeUninit] { if bytes == 0 { return &mut []; } - loop { - if let Some(a) = self.alloc_raw_without_grow(bytes) { - break a; - } - // No free space left. Allocate a new chunk to satisfy the request. - // On failure the grow will panic or abort. - self.grow(bytes); + if let Some(a) = self.alloc_raw_without_grow(bytes) { + // SAFETY: the lifetime is extended here, but then constrained again in `alloc_str`. + return unsafe { &mut *(a as *mut _) }; } + // No free space left. Allocate a new chunk to satisfy the request. + // On failure the grow will panic or abort. + self.grow(bytes); + self.alloc_raw_without_grow(bytes).unwrap() } - #[allow(clippy::mut_from_ref)] // arena allocator - pub(crate) fn alloc_str<'a>(&'a self, string: &str) -> &'a mut str { + pub(crate) fn alloc_str<'a>(&'a mut self, string: &str) -> &'a mut str { let alloc = self.alloc_raw(string.len()); let bytes = alloc.write_copy_of_slice(string.as_bytes()); diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 1b09deb6bfe60..582c43c78fcbb 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -143,7 +143,7 @@ mod symbol; use buffer::Buffer; pub use rpc::PanicMessage; -use rpc::{Decode, DecodeMut, Encode, Reader, Writer}; +use rpc::{DecodeMut, Encode, Reader, Writer}; /// Configuration for establishing an active connection between a server and a /// client. The server creates the bridge config (`run_server` in `server.rs`), diff --git a/library/proc_macro/src/bridge/rpc.rs b/library/proc_macro/src/bridge/rpc.rs index 85fd7d138585c..7f4f5fc3a97d5 100644 --- a/library/proc_macro/src/bridge/rpc.rs +++ b/library/proc_macro/src/bridge/rpc.rs @@ -3,7 +3,6 @@ use std::any::Any; use std::io::Write; use std::num::NonZero; -use std::str; pub(super) type Writer = super::buffer::Buffer; @@ -13,10 +12,6 @@ pub(super) trait Encode: Sized { pub(super) type Reader<'a> = &'a [u8]; -pub(super) trait Decode<'a, 's, S>: Sized { - fn decode(r: &mut Reader<'a>, s: &'s S) -> Self; -} - pub(super) trait DecodeMut<'a, 's, S>: Sized { fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self; } @@ -31,7 +26,7 @@ macro_rules! rpc_encode_decode { impl DecodeMut<'_, '_, S> for $ty { fn decode(r: &mut Reader<'_>, _: &mut S) -> Self { - const N: usize = ::std::mem::size_of::<$ty>(); + const N: usize = size_of::<$ty>(); let mut bytes = [0; N]; bytes.copy_from_slice(&r[..N]); diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs index 0bb30698aa1d7..2850e1099b700 100644 --- a/library/proc_macro/src/bridge/server.rs +++ b/library/proc_macro/src/bridge/server.rs @@ -40,10 +40,10 @@ macro_rules! define_server_handles { } } - impl<'s, S: Types> Decode<'_, 's, HandleStore>> + impl<'s, S: Types> DecodeMut<'_, 's, HandleStore>> for &'s Marked { - fn decode(r: &mut Reader<'_>, s: &'s HandleStore>) -> Self { + fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore>) -> Self { &s.$oty[handle::Handle::decode(r, &mut ())] } } diff --git a/library/proc_macro/src/bridge/symbol.rs b/library/proc_macro/src/bridge/symbol.rs index 57ca7db9fcdd7..eb7d30f9a6cc9 100644 --- a/library/proc_macro/src/bridge/symbol.rs +++ b/library/proc_macro/src/bridge/symbol.rs @@ -11,7 +11,6 @@ use std::cell::RefCell; use std::num::NonZero; -use std::str; use super::*;