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
use crate::{lifetime::Lifetime, managed::ManagedLazy, Finalize};
use std::alloc::Layout;

const MINIMUM_SIZE: usize = 1024;

struct ManagedArenaObjectHeader {
    layout: Layout,
    finalizer: unsafe fn(*mut ()),
    #[allow(dead_code)]
    lifetime: Lifetime,
}

pub struct ManagedArenaPage {
    memory: Vec<u8>,
    position: usize,
}

impl Drop for ManagedArenaPage {
    fn drop(&mut self) {
        self.clear();
    }
}

impl ManagedArenaPage {
    pub fn new(mut capacity: usize) -> Self {
        capacity = capacity.max(MINIMUM_SIZE).next_power_of_two();
        let memory = vec![0; capacity];
        Self {
            memory,
            position: 0,
        }
    }

    pub fn capacity(&self) -> usize {
        self.memory.len()
    }

    pub fn size(&self) -> usize {
        self.position
    }

    pub fn available_size(&self) -> usize {
        self.capacity() - self.size()
    }

    pub fn alloc<T: Finalize>(&mut self, value: T) -> Option<ManagedLazy<T>> {
        unsafe {
            let result = self.alloc_uninitialized::<T>()?;
            result.as_mut_ptr().unwrap().write(value);
            Some(result)
        }
    }

    /// # Safety
    pub unsafe fn alloc_uninitialized<T: Finalize>(&mut self) -> Option<ManagedLazy<T>> {
        let header_layout = Layout::new::<ManagedArenaObjectHeader>().pad_to_align();
        let layout = Layout::new::<T>().pad_to_align();
        if header_layout.size() + layout.size() > self.available_size() {
            return None;
        }
        let lifetime = Lifetime::default();
        let lifetime_lazy = lifetime.lazy();
        let header = ManagedArenaObjectHeader {
            layout,
            finalizer: T::finalize_raw,
            lifetime,
        };
        self.memory
            .as_mut_ptr()
            .add(self.position)
            .cast::<ManagedArenaObjectHeader>()
            .write(header);
        self.position += header_layout.size();
        let data = self.memory.as_mut_ptr().add(self.position).cast::<T>();
        self.position += layout.size();
        Some(ManagedLazy::new_raw(data, lifetime_lazy))
    }

    pub fn clear(&mut self) {
        let header_layout = Layout::new::<ManagedArenaObjectHeader>().pad_to_align();
        let mut position = 0;
        while position < self.position {
            unsafe {
                let header = self
                    .memory
                    .as_ptr()
                    .add(position)
                    .cast::<ManagedArenaObjectHeader>()
                    .read();
                position += header_layout.size();
                let data = self.memory.as_mut_ptr().add(position).cast::<()>();
                (header.finalizer)(data);
                position += header.layout.size();
            }
        }
        self.position = 0;
    }
}

pub struct ManagedArena {
    pages: Vec<ManagedArenaPage>,
    page_capacity: usize,
}

impl Default for ManagedArena {
    fn default() -> Self {
        Self {
            pages: Default::default(),
            page_capacity: MINIMUM_SIZE,
        }
    }
}

impl ManagedArena {
    pub fn new(page_capacity: usize) -> Self {
        Self {
            pages: Default::default(),
            page_capacity: page_capacity.max(MINIMUM_SIZE),
        }
    }

    pub fn page_capacity(&self) -> usize {
        self.page_capacity
    }

    pub fn pages_count(&self) -> usize {
        self.pages.len()
    }

    pub fn capacity(&self) -> usize {
        self.pages.iter().map(|page| page.capacity()).sum()
    }

    pub fn size(&self) -> usize {
        self.pages.iter().map(|page| page.size()).sum()
    }

    pub fn available_size(&self) -> usize {
        self.pages.iter().map(|page| page.available_size()).sum()
    }

    pub fn alloc<T: Finalize>(&mut self, value: T) -> ManagedLazy<T> {
        unsafe {
            let result = self.alloc_uninitialized::<T>();
            result.as_mut_ptr().unwrap().write(value);
            result
        }
    }

    /// # Safety
    pub unsafe fn alloc_uninitialized<T: Finalize>(&mut self) -> ManagedLazy<T> {
        for page in self.pages.iter_mut() {
            if let Some(result) = page.alloc_uninitialized::<T>() {
                return result;
            }
        }
        let header_size = Layout::new::<ManagedArenaObjectHeader>()
            .pad_to_align()
            .size();
        let size = Layout::new::<T>().pad_to_align().size();
        let mut page = ManagedArenaPage::new(self.page_capacity.max(header_size + size));
        let result = page.alloc_uninitialized::<T>().unwrap();
        self.pages.push(page);
        result
    }

    pub fn clear(&mut self) {
        self.pages.clear();
    }
}

#[cfg(test)]
mod tests {
    use crate::{managed::ManagedLazy, managed_arena::ManagedArena};
    use std::sync::{Arc, RwLock};

    #[test]
    fn test_managed_arena() {
        #[derive(Default)]
        struct Droppable(Arc<RwLock<bool>>);

        impl Drop for Droppable {
            fn drop(&mut self) {
                *self
                    .0
                    .write()
                    .expect("Could not get write access to arena memory page!") = true;
            }
        }

        struct Nested(ManagedLazy<u32>);

        let droppable = Droppable::default();
        let droppable_inner = droppable.0.clone();
        let mut arena = ManagedArena::new(4);
        assert_eq!(arena.page_capacity(), 1024);
        assert_eq!(arena.pages_count(), 0);
        assert_eq!(arena.capacity(), 0);
        assert_eq!(arena.size(), 0);
        assert_eq!(arena.available_size(), 0);
        let object = arena.alloc(42u32);
        assert_eq!(arena.pages_count(), 1);
        assert_eq!(arena.capacity(), 1024);
        assert_eq!(arena.size(), 68);
        assert_eq!(arena.available_size(), 956);
        assert_eq!(*object.read().unwrap(), 42);
        *object.write().unwrap() = 100;
        assert_eq!(*object.read().unwrap(), 100);
        let _ = arena.alloc([42u8; 2048]);
        assert_eq!(arena.pages_count(), 2);
        assert_eq!(arena.capacity(), 5120);
        assert_eq!(arena.size(), 2180);
        assert_eq!(arena.available_size(), 2940);
        let _ = arena.alloc(droppable);
        assert_eq!(arena.pages_count(), 2);
        assert_eq!(arena.capacity(), 5120);
        assert_eq!(arena.size(), 2252);
        assert_eq!(arena.available_size(), 2868);
        assert_eq!(*droppable_inner.read().unwrap(), false);
        let nested = arena.alloc(Nested(object.clone()));
        assert_eq!(arena.pages_count(), 2);
        assert_eq!(arena.capacity(), 5120);
        assert_eq!(arena.size(), 2364);
        assert_eq!(arena.available_size(), 2756);
        assert_eq!(*nested.read().unwrap().0.read().unwrap(), 100);
        assert_eq!(*object.read().unwrap(), 100);
        *nested.write().unwrap().0.write().unwrap() = 42;
        assert_eq!(*nested.read().unwrap().0.read().unwrap(), 42);
        assert_eq!(*object.read().unwrap(), 42);
        arena.clear();
        assert_eq!(*droppable_inner.read().unwrap(), true);
        assert_eq!(arena.pages_count(), 0);
        assert_eq!(arena.capacity(), 0);
        assert_eq!(arena.size(), 0);
        assert_eq!(arena.available_size(), 0);
        assert!(object.read().is_none());
    }
}