This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgc.zig
323 lines (261 loc) · 9.16 KB
/
gc.zig
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
const bench = @import("bench");
const std = @import("std");
const builtin = std.builtin;
const debug = std.debug;
const heap = std.heap;
const mem = std.mem;
const testing = std.testing;
pub const GcAllocator = struct {
const PointerList = std.ArrayList(Pointer);
base: mem.Allocator,
start: [*]const u8,
ptrs: PointerList,
const Flags = packed struct {
checked: bool,
marked: bool,
const zero = Flags{
.checked = false,
.marked = false,
};
};
const Pointer = struct {
flags: Flags,
memory: []u8,
};
pub inline fn init(child_alloc: *mem.Allocator) GcAllocator {
return GcAllocator{
.base = mem.Allocator{
.allocFn = allocFn,
.resizeFn = resizeFn,
},
.start = @intToPtr([*]const u8, @frameAddress()),
.ptrs = PointerList.init(child_alloc),
};
}
pub fn deinit(gc: *GcAllocator) void {
const child_alloc = gc.childAllocator();
for (gc.ptrs.items) |ptr| {
child_alloc.free(ptr.memory);
}
gc.ptrs.deinit();
gc.* = undefined;
}
pub fn collect(gc: *GcAllocator) void {
@call(.{ .modifier = .never_inline }, collectNoInline, .{gc});
}
pub fn collectFrame(gc: *GcAllocator, frame: []const u8) void {
gc.mark(frame);
gc.sweep();
}
pub fn allocator(gc: *GcAllocator) *mem.Allocator {
return &gc.base;
}
fn collectNoInline(gc: *GcAllocator) void {
const frame = blk: {
const end = @intToPtr([*]const u8, @frameAddress());
const i_start = @ptrToInt(gc.start);
const i_end = @ptrToInt(end);
if (i_start < i_end)
break :blk gc.start[0 .. i_end - i_start];
break :blk end[0 .. i_start - i_end];
};
gc.collectFrame(frame);
}
fn mark(gc: *GcAllocator, frame: []const u8) void {
const ptr_size = @sizeOf(*u8);
for ([_]void{{}} ** ptr_size) |_, i| {
if (frame.len <= i)
break;
const frame2 = frame[i..];
const len = (frame2.len / ptr_size) * ptr_size;
for (std.mem.bytesAsSlice([*]u8, frame2[0..len])) |frame_ptr| {
const ptr = gc.findPtr(frame_ptr) orelse continue;
if (ptr.flags.checked)
continue;
ptr.flags.marked = true;
ptr.flags.checked = true;
gc.mark(ptr.memory);
}
}
}
fn sweep(gc: *GcAllocator) void {
const child_alloc = gc.childAllocator();
const ptrs = gc.ptrs.items;
var i: usize = 0;
while (i < gc.ptrs.items.len) {
const ptr = &ptrs[i];
if (ptr.flags.marked) {
ptr.flags = Flags.zero;
i += 1;
continue;
}
gc.freePtr(ptr);
}
}
fn findPtr(gc: *GcAllocator, to_find_ptr: anytype) ?*Pointer {
comptime debug.assert(@typeInfo(@TypeOf(to_find_ptr)) == builtin.TypeId.Pointer);
for (gc.ptrs.items) |*ptr| {
const ptr_start = @ptrToInt(ptr.memory.ptr);
const ptr_end = ptr_start + ptr.memory.len;
if (ptr_start <= @ptrToInt(to_find_ptr) and @ptrToInt(to_find_ptr) < ptr_end)
return ptr;
}
return null;
}
fn freePtr(gc: *GcAllocator, ptr: *Pointer) void {
const child_alloc = gc.childAllocator();
child_alloc.free(ptr.memory);
// Swap the just freed pointer with the last pointer in the list.
ptr.* = undefined;
ptr.* = gc.ptrs.popOrNull() orelse undefined;
}
fn childAllocator(gc: *GcAllocator) *mem.Allocator {
return gc.ptrs.allocator;
}
fn free(base: *mem.Allocator, bytes: []u8) void {
const gc = @fieldParentPtr(GcAllocator, "base", base);
const child_alloc = gc.childAllocator();
const ptr = gc.findPtr(bytes.ptr) orelse @panic("Freeing memory not allocated by garbage collector!");
gc.freePtr(ptr);
}
fn allocFn(
base: *mem.Allocator,
len: usize,
ptr_align: u29,
len_align: u29,
ret_addr: usize,
) ![]u8 {
const gc = @fieldParentPtr(GcAllocator, "base", base);
const child_alloc = gc.childAllocator();
const memory = try child_alloc.allocFn(child_alloc, len, ptr_align, len_align, ret_addr);
try gc.ptrs.append(Pointer{
.flags = Flags.zero,
.memory = memory,
});
return memory;
}
fn resizeFn(
base: *mem.Allocator,
buf: []u8,
buf_align: u29,
new_len: usize,
len_align: u29,
ret_addr: usize,
) !usize {
if (new_len == 0) {
free(base, buf);
return 0;
}
if (new_len > buf.len)
return error.OutOfMemory;
return new_len;
}
};
const Leaker = struct {
l: *Leaker,
};
var test_buf: [1024 * 1024]u8 = undefined;
test "gc.collect: No leaks" {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
var gc = GcAllocator.init(&fba.allocator);
defer gc.deinit();
const allocator = gc.allocator();
var a = try allocator.create(Leaker);
a.* = Leaker{ .l = try allocator.create(Leaker) };
a.l.l = a;
gc.collect();
try testing.expect(gc.findPtr(a) != null);
try testing.expect(gc.findPtr(a.l) != null);
try testing.expectEqual(@as(usize, 2), gc.ptrs.items.len);
}
fn leak(allocator: *mem.Allocator) !void {
var a = try allocator.create(Leaker);
a.* = Leaker{ .l = try allocator.create(Leaker) };
a.l.l = a;
}
test "gc.collect: Leaks" {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
var gc = GcAllocator.init(&fba.allocator);
defer gc.deinit();
const allocator = gc.allocator();
var a = try allocator.create(Leaker);
a.* = Leaker{ .l = try allocator.create(Leaker) };
a.l.l = a;
try @call(.{ .modifier = .never_inline }, leak, .{allocator});
gc.collect();
try testing.expect(gc.findPtr(a) != null);
try testing.expect(gc.findPtr(a.l) != null);
try testing.expectEqual(@as(usize, 2), gc.ptrs.items.len);
}
test "gc.free" {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
var gc = GcAllocator.init(&fba.allocator);
defer gc.deinit();
const allocator = gc.allocator();
var a = try allocator.create(Leaker);
var b = try allocator.create(Leaker);
allocator.destroy(b);
try testing.expect(gc.findPtr(a) != null);
try testing.expectEqual(@as(usize, 1), gc.ptrs.items.len);
}
test "gc.benchmark" {
try bench.benchmark(struct {
const Arg = struct {
num: usize,
size: usize,
fn benchAllocator(a: Arg, allocator: *mem.Allocator, comptime free: bool) !void {
var i: usize = 0;
while (i < a.num) : (i += 1) {
const bytes = try allocator.alloc(u8, a.size);
defer if (free) allocator.free(bytes);
}
}
};
pub const args = [_]Arg{
Arg{ .num = 10 * 1, .size = 1024 * 1 },
Arg{ .num = 10 * 2, .size = 1024 * 1 },
Arg{ .num = 10 * 4, .size = 1024 * 1 },
Arg{ .num = 10 * 1, .size = 1024 * 2 },
Arg{ .num = 10 * 2, .size = 1024 * 2 },
Arg{ .num = 10 * 4, .size = 1024 * 2 },
Arg{ .num = 10 * 1, .size = 1024 * 4 },
Arg{ .num = 10 * 2, .size = 1024 * 4 },
Arg{ .num = 10 * 4, .size = 1024 * 4 },
};
pub const iterations = 10000;
pub fn FixedBufferAllocator(a: Arg) void {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
a.benchAllocator(&fba.allocator, false) catch unreachable;
}
pub fn Arena_FixedBufferAllocator(a: Arg) void {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
var arena = heap.ArenaAllocator.init(&fba.allocator);
defer arena.deinit();
a.benchAllocator(&arena.allocator, false) catch unreachable;
}
pub fn GcAllocator_FixedBufferAllocator(a: Arg) void {
var fba = heap.FixedBufferAllocator.init(test_buf[0..]);
var gc = GcAllocator.init(&fba.allocator);
defer gc.deinit();
a.benchAllocator(gc.allocator(), false) catch unreachable;
gc.collect();
}
pub fn PageAllocator(a: Arg) void {
const pa = heap.page_allocator;
a.benchAllocator(pa, true) catch unreachable;
}
pub fn Arena_PageAllocator(a: Arg) void {
const pa = heap.page_allocator;
var arena = heap.ArenaAllocator.init(pa);
defer arena.deinit();
a.benchAllocator(&arena.allocator, false) catch unreachable;
}
pub fn GcAllocator_PageAllocator(a: Arg) void {
const pa = heap.page_allocator;
var gc = GcAllocator.init(pa);
defer gc.deinit();
a.benchAllocator(gc.allocator(), false) catch unreachable;
gc.collect();
}
});
}