Skip to content

Commit

Permalink
Cleanup GC
Browse files Browse the repository at this point in the history
Cleanup GC
  • Loading branch information
zlw committed Dec 29, 2024
1 parent 90cb09c commit 09d26a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 60 deletions.
90 changes: 43 additions & 47 deletions src/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ const GrayObjects = DynamicArray(*Object);

pub const GCAllocator = struct {
parent_allocator: Allocator,
vm: *Vm,
grayObjects: GrayObjects,
bytesAllocated: usize = 0,
nextGC: usize = GC_DEFAULT_NEXT_GC,
collector: GarbageCollector,

const Self = @This();

pub fn init(parent_allocator: Allocator, vm: *Vm) Self {
return .{ .parent_allocator = parent_allocator, .vm = vm, .grayObjects = GrayObjects.init(parent_allocator) };
}

pub fn deinit(self: *Self) void {
self.grayObjects.deinit();
self.freeObjects();
pub fn init(parent_allocator: Allocator, collector: GarbageCollector) Self {
return .{ .parent_allocator = parent_allocator, .collector = collector };
}

pub fn allocator(self: *Self) Allocator {
Expand All @@ -49,51 +41,55 @@ pub const GCAllocator = struct {
fn alloc(ctx: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
const self: *Self = @ptrCast(@alignCast(ctx));

if ((self.bytesAllocated > self.nextGC)) {
try self.collectGarbage();
}

if (self.parent_allocator.rawAlloc(len, ptr_align, ret_addr)) |out| {
self.bytesAllocated += len;
return out;
} else {
return null;
}
return self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
}

fn resize(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
fn resize(ctx: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
const self: *Self = @ptrCast(@alignCast(ctx));

// if (new_len > buf.len) {
// if ((self.bytesAllocated + (new_len - buf.len) > self.nextGC) or stress_gc) {
// try self.collectGarbage();
// }
// }

if ((self.bytesAllocated > self.nextGC)) {
try self.collectGarbage();
}

if (self.parent_allocator.rawResize(buf, log2_buf_align, new_len, ret_addr)) {
if (new_len > buf.len) {
self.bytesAllocated += new_len - buf.len;
} else {
self.bytesAllocated -= buf.len - new_len;
}
return true;
} else {
return false;
}
return self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr);
}

fn free(ctx: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
const self: *Self = @ptrCast(@alignCast(ctx));

self.parent_allocator.rawFree(buf, buf_align, ret_addr);
self.bytesAllocated -= buf.len;
}
};

// end: Allocator interface
pub const GarbageCollector = struct {
const Self = @This();

vm: *Vm,
grayObjects: GrayObjects = undefined,
bytesAllocated: usize = 0,
nextGC: usize = GC_DEFAULT_NEXT_GC,
parent_allocator: Allocator,

pub fn init(parent_allocator: Allocator, vm: *Vm) Self {
return Self{ .vm = vm, .parent_allocator = parent_allocator };
}

pub fn allocator(self: *Self) Allocator {
var gc = GCAllocator.init(self.parent_allocator, self.*);
return gc.allocator();
}

pub fn deinit(self: *Self) void {
self.grayObjects.deinit();
}

fn bytesAllocatedIncreased(self: *Self, size: usize) void {
self.bytesAllocated += size;
}

fn bytesAllocatedDecreased(self: *Self, size: usize) void {
self.bytesAllocated -= size;
}

fn shouldCollect(self: *Self) bool {
return self.bytesAllocated > self.nextGC;
}

fn collectGarbage(self: *Self) !void {
const before = self.bytesAllocated;
Expand All @@ -119,9 +115,9 @@ pub const GCAllocator = struct {
var i: usize = undefined;

// mark Object in Values sitting on the stack
i = 0;
while (i < self.vm.stack_top) : (i += 1) {
self.markValue(&self.vm.stack[i]);
var slot: [*]Value = @ptrCast(&self.vm.stack[0]);
while (@intFromPtr(slot) < @intFromPtr(self.vm.stack_top)) : (slot += 1) {
self.markValue(&slot[0]);
}

// mark Object.Closure in CallFrames
Expand Down
25 changes: 12 additions & 13 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const compile = @import("./compiler.zig").compile;

const native = @import("./native.zig");

const GCAllocator = @import("./memory.zig").GCAllocator;
const GarbageCollector = @import("./memory.zig").GarbageCollector;
const Parser = @import("./compiler.zig").Parser;

const debug_trace_execution = debug.debug_trace_execution;
Expand All @@ -41,26 +41,24 @@ pub const Vm = struct {
framesCount: usize = 0,
stack: [stack_max]Value = undefined,
stack_top: [*]Value = undefined,
strings: Table,
strings: Table = undefined,
initString: ?*Object.String = null,
globals: Table,
globals: Table = undefined,
objects: ?*Object = null,
openUpvalues: ?*Object.Upvalue = null,
allocator: Allocator,
allocator: Allocator = undefined,
collector: *GarbageCollector = undefined,
parser: ?*Parser = null,

pub fn init(parent_allocator: Allocator) Self {
// var vm = Self{ .allocator = undefined, .strings = undefined, .globals = undefined };
// var gc = GCAllocator.init(parent_allocator, &vm);
// var allocator = gc.allocator();
// _ = allocator;
var vm = Self{};

// vm.allocator = parent_allocator;
// vm.strings = Table.init(parent_allocator);
// vm.globals = Table.init(parent_allocator);

var vm = Self{ .allocator = parent_allocator, .strings = Table.init(parent_allocator), .globals = Table.init(parent_allocator) };
var gc = GarbageCollector.init(parent_allocator, &vm);
vm.collector = @constCast(&gc);
vm.allocator = parent_allocator;

vm.strings = Table.init(vm.allocator);
vm.globals = Table.init(vm.allocator);
vm.stack_top = vm.stack[0..];
vm.initString = Object.String.copy(&vm, "init");
vm.defineNative("clock", native.clockNative);
Expand All @@ -73,6 +71,7 @@ pub const Vm = struct {
self.strings.deinit();
self.initString = null;
self.freeObjects();
self.collector.deinit();
}

fn freeObjects(self: *Self) void {
Expand Down

0 comments on commit 09d26a7

Please sign in to comment.