Skip to main content

proc_macro/bridge/
arena.rs

1//! A minimal arena allocator inspired by `rustc_arena::DroplessArena`.
2//!
3//! This is unfortunately a minimal re-implementation rather than a dependency
4//! as it is difficult to depend on crates from within `proc_macro`, due to it
5//! being built at the same time as `std`.
6
7use std::cell::{Cell, RefCell};
8use std::mem::MaybeUninit;
9use std::ops::Range;
10use std::{cmp, ptr, slice};
11
12/// A minimal arena allocator inspired by `rustc_arena::DroplessArena`.
13///
14/// This is unfortunately a complete re-implementation rather than a dependency
15/// as it is difficult to depend on crates from within `proc_macro`, due to it
16/// being built at the same time as `std`.
17///
18/// This arena doesn't have support for allocating anything other than byte
19/// slices, as that is all that is necessary.
20pub(crate) struct Arena {
21    start: Cell<*mut MaybeUninit<u8>>,
22    end: Cell<*mut MaybeUninit<u8>>,
23    chunks: RefCell<Vec<Box<[MaybeUninit<u8>]>>>,
24}
25
26impl Arena {
27    pub(crate) fn new() -> Self {
28        Arena {
29            start: Cell::new(ptr::null_mut()),
30            end: Cell::new(ptr::null_mut()),
31            chunks: RefCell::new(Vec::new()),
32        }
33    }
34
35    /// Add a new chunk with at least `additional` free bytes.
36    #[inline(never)]
37    #[cold]
38    fn grow(&self, additional: usize) {
39        // The arenas start with PAGE-sized chunks, and then each new chunk is twice as
40        // big as its predecessor, up until we reach HUGE_PAGE-sized chunks, whereupon
41        // we stop growing. This scales well, from arenas that are barely used up to
42        // arenas that are used for 100s of MiBs. Note also that the chosen sizes match
43        // the usual sizes of pages and huge pages on Linux.
44        const PAGE: usize = 4096;
45        const HUGE_PAGE: usize =
46            cfg_select! {
47                any(target_pointer_width = "64", target_pointer_width = "32") => 2 * 1024 * 1024,
48                _ => 8192, // just make it compile for -Zbuild-std
49            };
50
51        let mut chunks = self.chunks.borrow_mut();
52        let mut new_cap;
53        if let Some(last_chunk) = chunks.last_mut() {
54            // If the previous chunk's len is less than HUGE_PAGE
55            // bytes, then this chunk will be least double the previous
56            // chunk's size.
57            new_cap = last_chunk.len().min(HUGE_PAGE / 2);
58            new_cap *= 2;
59        } else {
60            new_cap = PAGE;
61        }
62        // Also ensure that this chunk can fit `additional`.
63        new_cap = cmp::max(additional, new_cap);
64
65        let chunk = chunks.push_mut(Box::new_uninit_slice(new_cap));
66        let Range { start, end } = chunk.as_mut_ptr_range();
67        self.start.set(start);
68        self.end.set(end);
69    }
70
71    /// Allocates a byte slice with specified size from the current memory
72    /// chunk. Returns `None` if there is no free space left to satisfy the
73    /// request.
74    #[allow(clippy::mut_from_ref)]
75    fn alloc_raw_without_grow(&self, bytes: usize) -> Option<&mut [MaybeUninit<u8>]> {
76        let start = self.start.get().addr();
77        let old_end = self.end.get();
78        let end = old_end.addr();
79
80        let new_end = end.checked_sub(bytes)?;
81        if start <= new_end {
82            let new_end = old_end.with_addr(new_end);
83            self.end.set(new_end);
84            // SAFETY: `bytes` bytes starting at `new_end` were just reserved.
85            Some(unsafe { slice::from_raw_parts_mut(new_end, bytes) })
86        } else {
87            None
88        }
89    }
90
91    fn alloc_raw(&self, bytes: usize) -> &mut [MaybeUninit<u8>] {
92        if bytes == 0 {
93            return &mut [];
94        }
95
96        if let Some(a) = self.alloc_raw_without_grow(bytes) {
97            return a;
98        }
99        // No free space left. Allocate a new chunk to satisfy the request.
100        // On failure the grow will panic or abort.
101        self.grow(bytes);
102        self.alloc_raw_without_grow(bytes).unwrap()
103    }
104
105    #[allow(clippy::mut_from_ref)] // arena allocator
106    pub(crate) fn alloc_str<'a>(&'a self, string: &str) -> &'a mut str {
107        let alloc = self.alloc_raw(string.len());
108        let bytes = alloc.write_copy_of_slice(string.as_bytes());
109
110        // SAFETY: we convert from `&str` to `&[u8]`, clone it into the arena,
111        // and immediately convert the clone back to `&str`.
112        unsafe { str::from_utf8_unchecked_mut(bytes) }
113    }
114}