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
pub mod collect;
pub mod type_info;
use std::{cmp, mem, ptr};
use crate::boxed::heap::type_info::TypeInfo;
use crate::boxed::refs::Gc;
use crate::boxed::{AllocType, Any, Boxed};
use crate::intern::{AsInterner, Interner};
#[repr(C)]
pub struct Segment {
next: *mut Any,
end: *const Any,
backing_vec: Vec<Any>,
}
#[repr(C)]
pub struct Heap {
current_segment: Segment,
full_segments: Vec<Segment>,
type_info: TypeInfo,
len_at_last_gc: usize,
}
impl Segment {
fn with_capacity(count: usize) -> Segment {
let mut backing_vec = Vec::with_capacity(count + 1);
let next: *mut Any = backing_vec.as_mut_ptr();
Segment {
next,
end: unsafe { next.add(count) },
backing_vec,
}
}
fn alloc_cells(&mut self, count: usize) -> Option<*mut Any> {
let current_next = self.next;
let new_next = (self.next as usize) + (count * mem::size_of::<Any>());
if new_next > self.end as usize {
None
} else {
self.next = new_next as *mut Any;
Some(current_next)
}
}
fn len(&self) -> usize {
(self.next as usize - self.backing_vec.as_ptr() as usize) / mem::size_of::<Any>()
}
}
impl Drop for Segment {
fn drop(&mut self) {
let mut current = self.backing_vec.as_mut_ptr();
while current < self.next as *mut Any {
unsafe {
match (*current).header.alloc_type {
AllocType::Heap16 | AllocType::Heap32 => ptr::drop_in_place(current),
AllocType::HeapForward16 | AllocType::HeapForward32 => {}
AllocType::Const | AllocType::Stack => {
unreachable!("Unexpected alloc type in heap")
}
}
match (*current).header.alloc_type {
AllocType::Heap16 | AllocType::HeapForward16 => {
current = current.add(1);
}
AllocType::Heap32 | AllocType::HeapForward32 => {
current = current.add(2);
}
AllocType::Const | AllocType::Stack => {
unreachable!("Unexpected alloc type in heap")
}
}
}
}
}
}
impl Heap {
const DEFAULT_SEGMENT_CAPACITY: usize = 1024;
const DEFAULT_CAPACITY: usize = Self::DEFAULT_SEGMENT_CAPACITY;
pub fn empty() -> Heap {
Self::new(TypeInfo::empty(), Self::DEFAULT_CAPACITY)
}
pub fn new(type_info: TypeInfo, count: usize) -> Heap {
Heap {
current_segment: Segment::with_capacity(count),
full_segments: vec![],
type_info,
len_at_last_gc: 0,
}
}
pub fn should_collect(&self) -> bool {
let maximum_len = std::cmp::max(Self::DEFAULT_SEGMENT_CAPACITY, self.len_at_last_gc) * 2;
self.len() > maximum_len
}
fn save_len_at_gc(&mut self) {
self.len_at_last_gc = self.len();
}
pub fn alloc_cells(&mut self, count: usize) -> *mut Any {
if let Some(alloc) = self.current_segment.alloc_cells(count) {
return alloc;
}
let capacity = cmp::max(count, Self::DEFAULT_SEGMENT_CAPACITY);
let mut new_segment = Segment::with_capacity(capacity);
let alloc = new_segment.alloc_cells(count).unwrap();
let previous_segment = mem::replace(&mut self.current_segment, new_segment);
self.full_segments.push(previous_segment);
alloc
}
pub fn type_info(&self) -> &TypeInfo {
&self.type_info
}
pub fn type_info_mut(&mut self) -> &mut TypeInfo {
&mut self.type_info
}
pub fn len(&self) -> usize {
let full_len: usize = self.full_segments.iter().map(Segment::len).sum();
self.current_segment.len() + full_len
}
pub fn is_empty(&self) -> bool {
self.current_segment.len() == 0 && self.full_segments.is_empty()
}
pub fn place_box<T: Boxed>(&mut self, boxed: T) -> Gc<T> {
let heap_size = boxed
.header()
.alloc_type()
.to_heap_box_size()
.expect("non-heap alloc type");
let needed_cells = heap_size.cell_count();
let insert_at = self.alloc_cells(needed_cells);
unsafe {
ptr::copy_nonoverlapping(&boxed as *const T as *const Any, insert_at, needed_cells);
}
mem::forget(boxed);
unsafe { Gc::new(insert_at as *const T) }
}
}
impl Default for Heap {
fn default() -> Self {
Self::empty()
}
}
pub trait AsHeap {
fn as_heap(&self) -> &Heap;
fn as_heap_mut(&mut self) -> &mut Heap;
}
impl AsHeap for Heap {
fn as_heap(&self) -> &Heap {
self
}
fn as_heap_mut(&mut self) -> &mut Heap {
self
}
}
impl AsInterner for Heap {
fn as_interner(&self) -> &Interner {
self.type_info().interner()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic_alloc() {
use crate::boxed::Str;
let mut heap = Heap::new(TypeInfo::empty(), 2);
let string1 = Str::new(&mut heap, "HELLO");
let string2 = Str::new(&mut heap, "WORLD");
assert_eq!("HELLO", string1.as_str());
assert_eq!("WORLD", string2.as_str());
}
}