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
use std::mem::size_of;
use std::cell::Cell;
const ARENA_BLOCK: usize = 64 * 1024;
pub struct Arena {
store: Cell<Vec<Vec<u8>>>,
ptr: Cell<*mut u8>,
offset: Cell<usize>
}
impl Arena {
pub fn new() -> Self {
let mut store = vec![Vec::with_capacity(ARENA_BLOCK)];
let ptr = store[0].as_mut_ptr();
Arena {
store: Cell::new(store),
ptr: Cell::new(ptr),
offset: Cell::new(0)
}
}
#[inline]
pub fn alloc<'a, T: Sized + Copy>(&'a self, val: T) -> &'a T {
unsafe {
let ptr = self.alloc_uninitialized();
*ptr = val;
&*ptr
}
}
#[inline]
pub unsafe fn alloc_uninitialized<'a, T: Sized + Copy>(&'a self) -> *mut T {
self.require(size_of::<T>()) as *mut T
}
pub fn alloc_str<'a>(&'a self, val: &str) -> &'a str {
let offset = self.offset.get();
let alignment = size_of::<usize>() - (val.len() % size_of::<usize>());
let cap = offset + val.len() + alignment;
if cap > ARENA_BLOCK {
return self.alloc_string(val.into());
}
self.offset.set(cap);
unsafe {
use std::ptr::copy_nonoverlapping;
use std::str::from_utf8_unchecked;
use std::slice::from_raw_parts;
let ptr = self.ptr.get().offset(offset as isize);
copy_nonoverlapping(val.as_ptr(), ptr, val.len());
from_utf8_unchecked(from_raw_parts(ptr, val.len()))
}
}
pub fn alloc_str_zero_end<'a>(&'a self, val: &str) -> *const u8 {
let len_with_zero = val.len() + 1;
let ptr = self.require(len_with_zero);
unsafe {
use std::ptr::copy_nonoverlapping;
copy_nonoverlapping(val.as_ptr(), ptr, val.len());
*ptr.offset(val.len() as isize) = 0;
ptr
}
}
pub fn alloc_string<'a>(&'a self, val: String) -> &'a str {
let len = val.len();
let ptr = self.alloc_vec(val.into_bytes());
unsafe {
use std::str::from_utf8_unchecked;
use std::slice::from_raw_parts;
from_utf8_unchecked(from_raw_parts(ptr, len))
}
}
#[inline]
fn alloc_vec(&self, mut val: Vec<u8>) -> *mut u8 {
let ptr = val.as_mut_ptr();
let mut temp = self.store.replace(Vec::new());
temp.push(val);
self.store.replace(temp);
ptr
}
fn alloc_bytes(&self, size: usize) -> *mut u8 {
self.alloc_vec(Vec::with_capacity(size))
}
#[inline]
fn require(&self, size: usize) -> *mut u8 {
if size > ARENA_BLOCK {
return self.alloc_bytes(size);
}
let size = match size % size_of::<usize>() {
0 => size,
n => size + n
};
let offset = self.offset.get();
let cap = offset + size;
if cap > ARENA_BLOCK {
self.grow();
self.offset.set(size);
self.ptr.get()
} else {
self.offset.set(cap);
unsafe { self.ptr.get().offset(offset as isize) }
}
}
fn grow(&self) {
let ptr = self.alloc_vec(Vec::with_capacity(ARENA_BLOCK));
self.ptr.set(ptr);
}
#[inline]
pub unsafe fn clear(&self) {
self.offset.set(0)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn allocate_some_stuff() {
let arena = Arena::new();
assert_eq!(arena.alloc(0u64), &0);
assert_eq!(arena.alloc(42u64), &42);
assert_eq!(arena.alloc(0x8000000u64), &0x8000000u64);
assert_eq!(arena.offset.get(), 8 * 3);
let mut arena = arena;
assert_eq!(arena.store.get_mut().len(), 1);
}
#[test]
fn allocate_huge_heap() {
let arena = Arena::new();
assert_eq!(arena.alloc(0u64), &0);
assert_eq!(arena.alloc(42u64), &42);
unsafe { arena.alloc_uninitialized::<[usize; 1024 * 1024]>() };
assert_eq!(arena.offset.get(), 8 * 2);
assert_eq!(arena.alloc(0x8000000u64), &0x8000000u64);
assert_eq!(arena.offset.get(), 8 * 3);
let mut arena = arena;
assert_eq!(arena.store.get_mut().len(), 2);
assert_eq!(arena.store.get_mut()[1].capacity(), size_of::<usize>() * 1024 * 1024);
}
#[test]
fn aligns_str_allocs() {
let arena = Arena::new();
assert_eq!(arena.alloc_str("foo"), "foo");
assert_eq!(arena.offset.get(), 8);
assert_eq!(arena.alloc_str("doge to the moon!"), "doge to the moon!");
assert_eq!(arena.offset.get(), 32);
}
#[test]
fn alloc_str_zero_end() {
let arena = Arena::new();
let ptr = arena.alloc_str_zero_end("abcdefghijk");
let allocated = unsafe { ::std::slice::from_raw_parts(ptr, 12) };
assert_eq!(arena.offset.get(), 16);
assert_eq!(allocated, &[b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', 0]);
}
}