Skip to content

A Boehm-Demers-Weiser conservative garbage collector library for Zig.

License

Notifications You must be signed in to change notification settings

heliumlang/zig-gc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-gc

This library implements a Zig allocator that uses the Boehm-Demers-Weiser conservative Garbage Collector (libgc, bdwgc, boehm-gc). Values allocated within this allocator do not need to be explicitly freed.

Should I use a GC with Zig? Probably not, but it depends on your use case. A garbage collector is nice in certain scenarios, can make implementing certain data structures easier (particularly immutable ones), etc. The nice thing about Zig is you can choose what you want and don't want in the garbage collector.

Example

const std = @import("std");
const gc = @import("gc");

pub fn main() !void {
    var alloc = gc.allocator();

    // We'll write to the terminal
    const stdout = std.io.getStdOut().writer();

    // Compare the output by enabling/disabling
    // gc.disable();

    // Allocate a bunch of stuff and never free it, outputting
    // the heap size along the way. When the GC is enabled,
    // it'll stabilize at a certain size.
    var i: u64 = 0;
    while (i < 10_000_000) : (i += 1) {
        // This is all really ugly but its not idiomatic Zig code so
        // just take this at face value. We're doing weird stuff here
        // to show that we're collecting garbage.
        const p: **u8 = @ptrCast(try alloc.alloc(*u8, @sizeOf(*u8)));
        const q = try alloc.alloc(u8, @sizeOf(u8));
        p.* = @ptrCast(q);
        _ = alloc.resize(q, 2 * @sizeOf(u8));

        if (i % 100_000 == 0) {
            const heap = gc.getHeapSize();
            try stdout.print("heap size: {d}\n", .{heap});
        }
    }
}

About

A Boehm-Demers-Weiser conservative garbage collector library for Zig.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 74.4%
  • Shell 14.6%
  • Makefile 5.0%
  • C++ 2.4%
  • M4 1.7%
  • CMake 1.2%
  • Other 0.7%