rune/hir/
arena.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#[cfg(test)]
mod tests;

use core::alloc::Layout;
use core::cell::{Cell, RefCell};
use core::marker::PhantomData;
use core::mem;
use core::ptr;
use core::slice;
use core::str;

use crate::alloc::prelude::*;
use crate::alloc::{self, HashMap};

#[non_exhaustive]
pub struct ArenaWriteSliceOutOfBounds {
    pub index: usize,
}

#[derive(Debug)]
#[non_exhaustive]
pub struct ArenaAllocError {
    pub requested: usize,
}

impl From<alloc::Error> for ArenaAllocError {
    fn from(_: alloc::Error) -> Self {
        Self { requested: 0 }
    }
}

/// The size of a slab in the arena allocator.
const PAGE: usize = 4096;
const HUGE_PAGE: usize = 2 * 1024 * 1024;

struct Chunk {
    storage: Box<[u8]>,
}

impl Chunk {
    /// Construct a new chunk with the specified length.
    fn new(len: usize) -> Result<Self, ArenaAllocError> {
        Ok(Self {
            storage: try_vec![0u8; len].try_into_boxed_slice()?,
        })
    }
}

/// An arena allocator.
pub struct Arena {
    start: Cell<*mut u8>,
    end: Cell<*mut u8>,
    chunks: RefCell<Vec<Chunk>>,
    /// Allocated bytes. The pointers are stable into the chunks.
    bytes: RefCell<HashMap<Box<[u8]>, ptr::NonNull<u8>>>,
}

impl Arena {
    /// Construct a new empty arena allocator.
    pub fn new() -> Self {
        Self {
            start: Cell::new(ptr::null_mut()),
            end: Cell::new(ptr::null_mut()),
            chunks: RefCell::new(Vec::new()),
            bytes: RefCell::new(HashMap::new()),
        }
    }

    /// Allocate a string from the arena.
    pub(crate) fn alloc_bytes(&self, bytes: &[u8]) -> Result<&[u8], ArenaAllocError> {
        if let Some(ptr) = self.bytes.borrow().get(bytes).copied() {
            // SAFETY: The pointer returned was previously allocated correctly
            // in the arena.
            unsafe {
                return Ok(slice::from_raw_parts(ptr.as_ptr() as *const _, bytes.len()));
            }
        }

        let layout = Layout::array::<u8>(bytes.len()).map_err(|_| ArenaAllocError {
            requested: bytes.len(),
        })?;
        let ptr = self.alloc_raw(layout)?;

        // SAFETY: we're ensuring the valid contents of pointer by copying a
        // safe bytes slice into it.
        let output = unsafe {
            ptr::copy_nonoverlapping(bytes.as_ptr(), ptr.as_ptr(), bytes.len());
            slice::from_raw_parts(ptr.as_ptr() as *const _, bytes.len())
        };

        self.bytes.borrow_mut().try_insert(bytes.try_into()?, ptr)?;
        Ok(output)
    }

    /// Allocate a string from the arena.
    pub(crate) fn alloc_str(&self, string: &str) -> Result<&str, ArenaAllocError> {
        let bytes = self.alloc_bytes(string.as_bytes())?;

        // SAFETY: we're ensuring the valid contents of the returned string by
        // safely accessing it above.
        unsafe { Ok(str::from_utf8_unchecked(bytes)) }
    }

    /// Allocate a new object of the given type.
    pub(crate) fn alloc<T>(&self, object: T) -> Result<&mut T, ArenaAllocError> {
        assert!(!mem::needs_drop::<T>());

        let mut ptr = self.alloc_raw(Layout::for_value::<T>(&object))?.cast();

        unsafe {
            // Write into uninitialized memory.
            ptr::write(ptr.as_ptr(), object);
            Ok(ptr.as_mut())
        }
    }

    /// Allocate an iterator with the given length as a slice.
    pub(crate) fn alloc_iter<T>(&self, len: usize) -> Result<AllocIter<'_, T>, ArenaAllocError> {
        assert!(!mem::needs_drop::<T>(), "cannot allocate drop element");

        let mem = if len == 0 {
            None
        } else {
            Some(self.alloc_raw(Layout::array::<T>(len).unwrap())?.cast())
        };

        Ok(AllocIter {
            mem,
            index: 0,
            len,
            _marker: PhantomData,
        })
    }

    #[inline]
    fn alloc_raw_without_grow(&self, layout: Layout) -> Option<ptr::NonNull<u8>> {
        let start = addr(self.start.get());
        let old_end = self.end.get();
        let end = addr(old_end);

        let align = layout.align();
        let bytes = layout.size();

        let new_end = end.checked_sub(bytes)? & !(align - 1);

        if start > new_end {
            return None;
        }

        let new_end = with_addr(old_end, new_end);
        self.end.set(new_end);

        // Pointer is guaranteed to be non-null due to how it's allocated.
        unsafe { Some(ptr::NonNull::new_unchecked(new_end)) }
    }

    #[inline]
    fn alloc_raw(&self, layout: Layout) -> Result<ptr::NonNull<u8>, ArenaAllocError> {
        // assert!(layout.size() != 0);
        assert!(layout.align() != 0);

        if layout.size() == 0 {
            // SAFETY: we've asserted that alignment is non-zero above.
            return unsafe { Ok(ptr::NonNull::new_unchecked(layout.align() as *mut u8)) };
        }

        loop {
            if let Some(a) = self.alloc_raw_without_grow(layout) {
                break Ok(a);
            }

            self.grow(layout.size())?;
        }
    }

    #[cold]
    fn grow(&self, additional: usize) -> Result<(), ArenaAllocError> {
        let mut chunks = self.chunks.borrow_mut();

        let new_cap = additional.max(
            chunks
                .last()
                .map(|c| c.storage.len().min(HUGE_PAGE / 2) * 2)
                .unwrap_or(PAGE),
        );

        chunks.try_push(Chunk::new(new_cap)?)?;

        let Some(chunk) = chunks.last_mut() else {
            return Err(ArenaAllocError {
                requested: additional,
            });
        };

        let range = chunk.storage.as_mut_ptr_range();
        self.start.set(range.start);
        self.end.set(range.end);
        Ok(())
    }
}

#[inline]
pub(crate) fn addr(this: *mut u8) -> usize {
    this as usize
}

#[inline]
pub(crate) fn with_addr(this: *mut u8, a: usize) -> *mut u8 {
    let this_addr = addr(this) as isize;
    let dest_addr = a as isize;
    let offset = dest_addr.wrapping_sub(this_addr);
    this.wrapping_offset(offset)
}

/// An iterator writer.
pub(crate) struct AllocIter<'hir, T> {
    mem: Option<ptr::NonNull<T>>,
    index: usize,
    len: usize,
    _marker: PhantomData<&'hir ()>,
}

impl<'hir, T> AllocIter<'hir, T> {
    /// Write the next element into the slice.
    pub(crate) fn write(&mut self, object: T) -> Result<(), ArenaWriteSliceOutOfBounds> {
        let mem = self
            .mem
            .ok_or(ArenaWriteSliceOutOfBounds { index: self.index })?;

        // Sanity check is necessary to ensure memory safety.
        if self.index >= self.len {
            return Err(ArenaWriteSliceOutOfBounds { index: self.index });
        }

        unsafe {
            ptr::write(mem.as_ptr().add(self.index), object);
            self.index += 1;
            Ok(())
        }
    }

    /// Finalize the iterator being written and return the appropriate closure.
    pub(crate) fn finish(self) -> &'hir mut [T] {
        match self.mem {
            Some(mem) => {
                // SAFETY: Is guaranteed to be correct due to how it's allocated and written to above.
                unsafe { slice::from_raw_parts_mut(mem.as_ptr(), self.index) }
            }
            None => &mut [],
        }
    }
}