-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.mg
314 lines (263 loc) · 7.95 KB
/
mem.mg
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
import wasm "wasm";
let lower_bound: usize = 0;
let upper_bound: usize = 0;
let page_size: usize = 65536;
let freelist_head: *Header = 0 as *Header;
fn alloc<T>(): *T {
return alloc_size(wasm::size_of::<T>()) as *T;
}
fn alloc_array<T>(len: usize): [*]T {
return alloc_size(wasm::size_of::<T>() * len) as [*]T;
}
fn dealloc_array<T>(p: [*]T) {
dealloc_from_ptr(p as usize);
}
struct Header{
size: usize,
prev: usize,
next: usize,
is_used: bool,
}
let header_size: usize = wasm::size_of::<Header>();
// memory layout:
// - Header: (aligned to 8 byte)
// - size
// - next
// - is_used
// - padding
// - content (aligned to 8 byte)
// - padding
// - Footer (aligned to 8 byte)
// - size
// - is_used
// - padding
struct Footer{
size: usize,
is_used: bool,
}
let footer_size: usize = wasm::size_of::<Footer>();
fn alloc_size(size: usize): usize {
let padded_size = pad(size);
let curr = freelist_head as usize;
while curr != 0 {
let header = curr as *Header;
if header.size.* >= padded_size {
return allocate_from_chunk(header, padded_size);
}
curr = header.next.* as usize;
}
return allocate_from_new_page(size);
}
fn dealloc_from_ptr(p: usize) {
let padded_header = pad(header_size);
let padded_footer = pad(footer_size);
let header = (p - padded_header) as *Header;
header.is_used.* = false;
header.prev.* = 0;
connect(header, freelist_head);
freelist_head = header;
let footer = (p + header.size.*) as *Footer;
footer.is_used.* = false;
merge(header);
}
fn merge(header: *Header) {
let padded_header = pad(header_size);
let padded_footer = pad(footer_size);
let h = header;
while true {
let new_header = merge_left(h);
if new_header as usize == 0 {
break;
}
h = new_header;
}
while true {
let new_header = merge_right(h);
if new_header as usize == 0 {
break;
}
h = new_header;
}
}
fn merge_right(header: *Header): *Header {
let padded_header = pad(header_size);
let padded_footer = pad(footer_size);
let next_header = header as usize + padded_header + header.size.* + padded_footer;
let next_header = next_header as *Header;
return merge_left(next_header);
}
fn merge_left(header: *Header): *Header {
let padded_header = pad(header_size);
let padded_footer = pad(footer_size);
if header as usize >= upper_bound {
return 0 as *Header;
}
if header.is_used.* {
return 0 as *Header;
}
let left_footer = (header as usize - padded_footer) as *Footer;
let left_header = (left_footer as usize - left_footer.size.* - padded_header) as *Header;
if left_header as (usize) < lower_bound {
return 0 as *Header;
}
if left_header.is_used.* {
return 0 as *Header;
}
let new_size = header.size.* + left_header.size.* + padded_header + padded_footer;
let merged_header = left_header;
let merged_footer = (header as usize + padded_header + header.size.*) as *Footer;
// first: remove left from free list
if left_header.prev.* as usize != 0 {
let prev_header = left_header.prev.* as *Header;
prev_header.next.* = left_header.next.* as usize;
}
if left_header.next.* as usize != 0 {
let next_header = left_header.next.* as *Header;
next_header.prev.* = left_header.prev.* as usize;
}
if left_header == freelist_head {
freelist_head = left_header.next.* as *Header;
}
// second: remove header from free list
if header.prev.* as usize != 0 {
let prev_header = header.prev.* as *Header;
prev_header.next.* = header.next.* as usize;
}
if header.next.* as usize != 0 {
let next_header = header.next.* as *Header;
next_header.prev.* = header.prev.* as usize;
}
if header == freelist_head {
freelist_head = header.next.* as *Header;
}
// third: add merged header to free list
merged_header.* = Header{
size: new_size,
prev: 0,
next: 0,
is_used: false,
};
connect(merged_header, freelist_head);
freelist_head = merged_header;
merged_footer.* = Footer{
size: new_size,
is_used: false,
};
return merged_header;
}
fn connect(left: *Header, right: *Header) {
if left as usize != 0 {
left.next.* = right as usize;
}
if right as usize != 0 {
right.prev.* = left as usize;
}
}
fn allocate_from_chunk(header: *Header, size: usize): usize {
let padded_header = pad(header_size);
let padded_size = pad(size);
let padded_footer = pad(footer_size);
let remaining_space = header.size.* - padded_size;
let has_additional_chunk = remaining_space >= padded_header + padded_footer + 8;
if !has_additional_chunk {
padded_size = padded_size + remaining_space;
}
header.is_used.* = true;
header.size.* = padded_size;
let footer_ptr = header as usize + padded_header + padded_size;
let footer = footer_ptr as *Footer;
footer.is_used.* = true;
footer.size.* = padded_size;
if has_additional_chunk {
let remaining_header = (footer_ptr + padded_footer) as *Header;
let remaining_content_size = remaining_space - padded_header - padded_footer;
remaining_header.* = Header{
size: remaining_content_size,
prev: header.prev.* as usize,
next: header.next.* as usize,
is_used: false,
};
let remaining_footer = (remaining_header as usize + padded_header + remaining_content_size) as *Footer;
remaining_footer.* = Footer{
size: remaining_content_size,
is_used: false,
};
if header.prev.* as usize != 0 {
let prev_header = header.prev.* as *Header;
prev_header.next.* = remaining_header as usize;
}
if header.next.* as usize != 0 {
let next_header = header.next.* as *Header;
next_header.prev.* = remaining_header as usize;
}
if header == freelist_head {
freelist_head = remaining_header;
}
} else {
if header.prev.* as usize != 0 {
let prev_header = header.prev.* as *Header;
prev_header.next.* = header.next.* as usize;
}
if header.next.* as usize != 0 {
let next_header = header.next.* as *Header;
next_header.prev.* = header.prev.* as usize;
}
if header == freelist_head {
freelist_head = header.next.* as *Header;
}
}
return header as usize + padded_header;
}
fn allocate_from_new_page(size: usize): usize {
let padded_header = pad(header_size);
let padded_size = pad(size);
let padded_footer = pad(footer_size);
let total_size = padded_header + padded_size + padded_footer;
let n_additional_pages = (total_size + page_size - 1) / page_size;
let page_id = wasm::memory_grow(n_additional_pages);
let p = page_id * page_size;
if lower_bound == 0 {
lower_bound = p;
}
let total_allocated = n_additional_pages * page_size;
upper_bound = p + total_allocated;
let remaining_space = total_allocated - total_size;
let has_additional_chunk = remaining_space >= padded_header + padded_footer + 8;
if !has_additional_chunk {
padded_size = padded_size + remaining_space;
}
let header = p as *Header;
header.size.* = padded_size;
header.next.* = freelist_head as usize;
header.prev.* = 0;
header.is_used.* = true;
if freelist_head as usize != 0 {
freelist_head.prev.* = header as usize;
}
let footer = (p + padded_header + padded_size) as *Footer;
footer.size.* = padded_size;
footer.is_used.* = true;
if has_additional_chunk {
let remaining_content_size = remaining_space - padded_header - padded_footer;
let next_chunk = p + padded_header + padded_size + padded_footer;
let header = next_chunk as *Header;
header.size.* = remaining_content_size;
header.next.* = freelist_head as usize;
header.prev.* = 0;
header.is_used.* = false;
if freelist_head as usize != 0 {
freelist_head.prev.* = header as usize;
}
let footer = (next_chunk + padded_header + remaining_content_size) as *Footer;
footer.size.* = remaining_content_size;
footer.is_used.* = false;
freelist_head = header as *Header;
}
return p + padded_header;
}
fn pad(p: usize): usize {
if p % 8 == 0 {
return p;
}
return p + 8 - (p % 8);
}