Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mitchellh/zig-libgc
Zig-friendly library for interfacing with libgc (bdwgc) -- the Boehm-Demers-Weiser conservative garbage collector
https://github.com/mitchellh/zig-libgc
Last synced: about 1 month ago
JSON representation
Zig-friendly library for interfacing with libgc (bdwgc) -- the Boehm-Demers-Weiser conservative garbage collector
- Host: GitHub
- URL: https://github.com/mitchellh/zig-libgc
- Owner: mitchellh
- License: mit
- Created: 2021-12-19T03:58:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-19T20:28:24.000Z (about 1 year ago)
- Last Synced: 2025-01-02T23:53:06.794Z (about 1 month ago)
- Language: Zig
- Size: 29.3 KB
- Stars: 175
- Watchers: 3
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - zig-libgc🗒️Zig-friendly library for interfacing with libgc (bdwgc) -- the Boehm-Demers-Weiser conservative garbage collector
README
# zig-libgc
This library implements a Zig allocator that uses the
[Boehm-Demers-Weiser conservative Garbage Collector (libgc, bdwgc, boehm-gc)](https://github.com/ivmai/bdwgc).
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
```zig
const std = @import("std");
const gc = @import("gc");
const GcAllocator = gc.GcAllocator;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.
var p = @ptrCast(**u8, try alloc.alloc(*u8, @sizeOf(*u8)));
var q = try alloc.alloc(u8, @sizeOf(u8));
p.* = @ptrCast(*u8, alloc.resize(q, 2 * @sizeOf(u8)).?);if (i % 100_000 == 0) {
const heap = gc.getHeapSize();
try stdout.print("heap size: {d}\n", .{heap});
}
}
}
```