johan0A/gc.zig
a Zig garbage collector interface for the bdwgc garbage collector.
a Zig garbage collector package that provides a garbage collector interface as well as the bdwgc Boehm GC garbage collector and more.
const zig_gc = @import("zig_gc");
pub fn main() !void {
// create a new garbage collector interface
const gc = zig_gc.BdwGarbageCollector.gc();
// coerce the gc interface to the standard allocator interface before passing it to ArrayList
var list = std.ArrayList(u8).init(gc.allocator());
try list.appendSlice("Hello");
try list.appendSlice(" World");
std.debug.print("{s}\n", .{list.items});
// the program will exit without memory leaks :D
}
Why use a specialized garbage collector interface? (Gc
)
otherwise, the BdwGarbageCollector acts similarely to a standard allocator and can be used with the standard allocator interface by using Gc.allocator(self: Gc)
or BdwGarbageCollector.allocator()
.
zig_gc
to the depency list in build.zig.zon
:zig fetch --save https://github.com/johan0A/gc.zig/archive/refs/tags/0.2.0.tar.gz
build.zig
:...
const zig_gc = b.dependency("zig_gc", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zig_gc", zig_gc.module("zig_gc"));
...
Licensed under the MIT License.