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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! Typed and dropless arena allocation, paraphrased from [the Rust Compiler's `rustc_arena`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_arena/src/lib.rs). See [LICENSE][1].
//!
//! An Arena Allocator is a type of allocator which provides stable locations for allocations within
//! itself for the entire duration of its lifetime.
//!
//! [1]: https://raw.githubusercontent.com/rust-lang/rust/master/LICENSE-MIT

#![feature(dropck_eyepatch, new_uninit, strict_provenance)]
#![no_std]

extern crate alloc;

pub(crate) mod constants {
    //! Size constants for arena chunk growth
    pub(crate) const MIN_CHUNK: usize = 4096;
    pub(crate) const MAX_CHUNK: usize = 2 * 1024 * 1024;
}

mod chunk {
    //! An [ArenaChunk] contains a block of raw memory for use in arena allocators.
    use alloc::boxed::Box;
    use core::{
        mem::{self, MaybeUninit},
        ptr::{self, NonNull},
    };

    pub struct ArenaChunk<T> {
        pub(crate) mem: NonNull<[MaybeUninit<T>]>,
        pub(crate) filled: usize,
    }

    impl<T: Sized> ArenaChunk<T> {
        pub fn new(cap: usize) -> Self {
            let slice = Box::new_uninit_slice(cap);
            Self { mem: NonNull::from(Box::leak(slice)), filled: 0 }
        }

        /// Drops all elements inside self, and resets the filled count to 0
        ///
        /// # Safety
        ///
        /// The caller must ensure that `self.filled` elements of self are currently initialized
        pub unsafe fn drop_elements(&mut self) {
            if mem::needs_drop::<T>() {
                // Safety: the caller has ensured that `filled` elements are initialized
                unsafe {
                    let slice = self.mem.as_mut();
                    for t in slice[..self.filled].iter_mut() {
                        t.assume_init_drop();
                    }
                }
                self.filled = 0;
            }
        }

        /// Gets a pointer to the start of the arena
        pub fn start(&mut self) -> *mut T {
            self.mem.as_ptr() as _
        }

        /// Gets a pointer to the end of the arena
        pub fn end(&mut self) -> *mut T {
            if mem::size_of::<T>() == 0 {
                ptr::without_provenance_mut(usize::MAX) // pointers to ZSTs must be unique
            } else {
                unsafe { self.start().add(self.mem.len()) }
            }
        }
    }

    impl<T> Drop for ArenaChunk<T> {
        fn drop(&mut self) {
            let _ = unsafe { Box::from_raw(self.mem.as_ptr()) };
        }
    }
}

pub mod typed_arena {
    //! A [TypedArena] can hold many instances of a single type, and will properly [Drop] them.
    #![allow(clippy::mut_from_ref)]

    use crate::{chunk::ArenaChunk, constants::*};
    use alloc::vec::Vec;
    use core::{
        cell::{Cell, RefCell},
        marker::PhantomData,
        mem, ptr, slice,
    };

    /// A [TypedArena] can hold many instances of a single type, and will properly [Drop] them when
    /// it falls out of scope.
    pub struct TypedArena<'arena, T> {
        _lives: PhantomData<&'arena T>,
        _drops: PhantomData<T>,
        chunks: RefCell<Vec<ArenaChunk<T>>>,
        head: Cell<*mut T>,
        tail: Cell<*mut T>,
    }

    impl<'arena, T> Default for TypedArena<'arena, T> {
        fn default() -> Self {
            Self::new()
        }
    }

    impl<'arena, T> TypedArena<'arena, T> {
        pub const fn new() -> Self {
            Self {
                _lives: PhantomData,
                _drops: PhantomData,
                chunks: RefCell::new(Vec::new()),
                head: Cell::new(ptr::null_mut()),
                tail: Cell::new(ptr::null_mut()),
            }
        }

        pub fn alloc(&'arena self, value: T) -> &'arena mut T {
            if self.head == self.tail {
                self.grow(1);
            }

            let out = if mem::size_of::<T>() == 0 {
                self.head
                    .set(ptr::without_provenance_mut(self.head.get().addr() + 1));
                ptr::NonNull::<T>::dangling().as_ptr()
            } else {
                let out = self.head.get();
                self.head.set(unsafe { out.add(1) });
                out
            };

            unsafe {
                ptr::write(out, value);
                &mut *out
            }
        }

        fn can_allocate(&self, len: usize) -> bool {
            len <= unsafe { self.tail.get().offset_from(self.head.get()) as usize }
        }

        /// # Panics
        /// Panics if size_of::<T> == 0 || len == 0
        #[inline]
        fn alloc_raw_slice(&self, len: usize) -> *mut T {
            assert!(mem::size_of::<T>() != 0);
            assert!(len != 0);

            if !self.can_allocate(len) {
                self.grow(len)
            }

            let out = self.head.get();

            unsafe { self.head.set(out.add(len)) };
            out
        }

        pub fn alloc_from_iter<I>(&'arena self, iter: I) -> &'arena mut [T]
        where I: IntoIterator<Item = T> {
            // Collect them all into a buffer so they're allocated contiguously
            let mut buf = iter.into_iter().collect::<Vec<_>>();
            if buf.is_empty() {
                return &mut [];
            }

            let len = buf.len();
            // If T is a ZST, calling alloc_raw_slice will panic
            let slice = if mem::size_of::<T>() == 0 {
                self.head
                    .set(ptr::without_provenance_mut(self.head.get().addr() + len));
                ptr::NonNull::dangling().as_ptr()
            } else {
                self.alloc_raw_slice(len)
            };

            unsafe {
                buf.as_ptr().copy_to_nonoverlapping(slice, len);
                buf.set_len(0);
                slice::from_raw_parts_mut(slice, len)
            }
        }

        #[cold]
        #[inline(never)]
        fn grow(&self, len: usize) {
            let size = mem::size_of::<T>().max(1);

            let mut chunks = self.chunks.borrow_mut();

            let capacity = if let Some(last) = chunks.last_mut() {
                last.filled = self.get_filled_of_chunk(last);
                last.mem.len().min(MAX_CHUNK / size) * 2
            } else {
                MIN_CHUNK / size
            }
            .max(len);

            let mut chunk = ArenaChunk::<T>::new(capacity);

            self.head.set(chunk.start());
            self.tail.set(chunk.end());
            chunks.push(chunk);
        }

        fn get_filled_of_chunk(&self, chunk: &mut ArenaChunk<T>) -> usize {
            let Self { head: tail, .. } = self;
            let head = chunk.start();
            if mem::size_of::<T>() == 0 {
                tail.get().addr() - head.addr()
            } else {
                unsafe { tail.get().offset_from(head) as usize }
            }
        }
    }

    unsafe impl<'arena, T: Send> Send for TypedArena<'arena, T> {}

    unsafe impl<'arena, #[may_dangle] T> Drop for TypedArena<'arena, T> {
        fn drop(&mut self) {
            let mut chunks = self.chunks.borrow_mut();

            if let Some(last) = chunks.last_mut() {
                last.filled = self.get_filled_of_chunk(last);
                self.tail.set(self.head.get());
            }

            for chunk in chunks.iter_mut() {
                unsafe { chunk.drop_elements() }
            }
        }
    }

    #[cfg(test)]
    mod tests;
}

pub mod dropless_arena {
    //! A [DroplessArena] can hold *any* combination of types as long as they don't implement
    //! [Drop].
    use crate::{chunk::ArenaChunk, constants::*};
    use alloc::vec::Vec;
    use core::{
        alloc::Layout,
        cell::{Cell, RefCell},
        marker::PhantomData,
        mem, ptr, slice,
    };

    pub struct DroplessArena<'arena> {
        _lives: PhantomData<&'arena u8>,
        chunks: RefCell<Vec<ArenaChunk<u8>>>,
        head: Cell<*mut u8>,
        tail: Cell<*mut u8>,
    }

    impl Default for DroplessArena<'_> {
        fn default() -> Self {
            Self::new()
        }
    }

    impl<'arena> DroplessArena<'arena> {
        pub const fn new() -> Self {
            Self {
                _lives: PhantomData,
                chunks: RefCell::new(Vec::new()),
                head: Cell::new(ptr::null_mut()),
                tail: Cell::new(ptr::null_mut()),
            }
        }

        /// Allocates a `T` in the [DroplessArena], and returns a mutable reference to it.
        ///
        /// # Panics
        /// - Panics if T implements [Drop]
        /// - Panics if T is zero-sized
        #[allow(clippy::mut_from_ref)]
        pub fn alloc<T>(&'arena self, value: T) -> &'arena mut T {
            assert!(!mem::needs_drop::<T>());
            assert!(mem::size_of::<T>() != 0);

            let out = self.alloc_raw(Layout::new::<T>()) as *mut T;

            unsafe {
                ptr::write(out, value);
                &mut *out
            }
        }

        /// Allocates a slice of `T`s`, copied from the given slice, returning a mutable reference
        /// to it.
        ///
        /// # Panics
        /// - Panics if T implements [Drop]
        /// - Panics if T is zero-sized
        /// - Panics if the slice is empty
        #[allow(clippy::mut_from_ref)]
        pub fn alloc_slice<T: Copy>(&'arena self, slice: &[T]) -> &'arena mut [T] {
            assert!(!mem::needs_drop::<T>());
            assert!(mem::size_of::<T>() != 0);
            assert!(!slice.is_empty());

            let mem = self.alloc_raw(Layout::for_value::<[T]>(slice)) as *mut T;

            unsafe {
                mem.copy_from_nonoverlapping(slice.as_ptr(), slice.len());
                slice::from_raw_parts_mut(mem, slice.len())
            }
        }

        /// Allocates a copy of the given [`&str`](str), returning a reference to the allocation.
        ///
        /// # Panics
        /// Panics if the string is empty.
        pub fn alloc_str(&'arena self, string: &str) -> &'arena str {
            let slice = self.alloc_slice(string.as_bytes());

            // Safety: This is a clone of the input string, which was valid
            unsafe { core::str::from_utf8_unchecked(slice) }
        }

        /// Allocates some [bytes](u8) based on the given [Layout].
        ///
        /// # Panics
        /// Panics if the provided [Layout] has size 0
        pub fn alloc_raw(&'arena self, layout: Layout) -> *mut u8 {
            /// Rounds the given size (or pointer value) *up* to the given alignment
            fn align_up(size: usize, align: usize) -> usize {
                (size + align - 1) & !(align - 1)
            }
            /// Rounds the given size (or pointer value) *down* to the given alignment
            fn align_down(size: usize, align: usize) -> usize {
                size & !(align - 1)
            }

            assert!(layout.size() != 0);
            loop {
                let Self { head, tail, .. } = self;
                let start = head.get().addr();
                let end = tail.get().addr();

                let align = 8.max(layout.align());

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

                if let Some(end) = end.checked_sub(bytes) {
                    let end = align_down(end, layout.align());

                    if start <= end {
                        tail.set(tail.get().with_addr(end));
                        return tail.get();
                    }
                }

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

        /// Grows the allocator, doubling the chunk size until it reaches [MAX_CHUNK].
        #[cold]
        #[inline(never)]
        fn grow(&self, len: usize) {
            let mut chunks = self.chunks.borrow_mut();

            let capacity = if let Some(last) = chunks.last_mut() {
                last.mem.len().min(MAX_CHUNK / 2) * 2
            } else {
                MIN_CHUNK
            }
            .max(len);

            let mut chunk = ArenaChunk::<u8>::new(capacity);

            self.head.set(chunk.start());
            self.tail.set(chunk.end());
            chunks.push(chunk);
        }

        /// Checks whether the given slice is allocated in this arena
        pub fn contains_slice<T>(&self, slice: &[T]) -> bool {
            let ptr = slice.as_ptr().cast::<u8>().cast_mut();
            for chunk in self.chunks.borrow_mut().iter_mut() {
                if chunk.start() <= ptr && ptr <= chunk.end() {
                    return true;
                }
            }
            false
        }
    }

    unsafe impl<'arena> Send for DroplessArena<'arena> {}

    #[cfg(test)]
    mod tests;
}