Packages

builtin
cbindgen
core
core.alloc
core.alloc.arena
core.alloc.atomic
core.alloc.fixed
core.alloc.gc
core.alloc.heap
core.alloc.log
core.alloc.memdebug
core.alloc.pool
core.alloc.ring
core.arg_parse
core.array
core.avl_tree
core.bucket_array
core.conv
core.doc
core.encoding
core.encoding.base64
core.encoding.csv
core.encoding.hex
core.encoding.json
core.encoding.kdl
core.encoding.osad
core.encoding.utf8
core.hash
core.hash.md5
core.hash.sha1
core.hash.sha256
core.heap
core.intrinsics
core.intrinsics.atomics
core.intrinsics.onyx
core.intrinsics.types
core.intrinsics.wasm
core.io
core.io.binary
core.iter
core.js
core.list
core.map
core.math
core.memory
core.misc
core.net
core.os
core.random
core.set
core.slice
core.string
core.sync
core.test
core.thread
core.time
main
runtime
runtime.info
runtime.platform
runtime.vars
simd

package core.alloc.gc

"Garbage collection" is not somthing Onyx has. Even things like reference counted pointers is not something Onyx can do, because of Onyx's simpler semantics. That being said, with custom allocators and some careful design, GC is "achievable". This allocator wraps another allocator. With each allocation, a little extra space is allocated to build a linked list of all allocations made. This way, when the memory is done being used, everything can be freed automatically.

The auto macro makes this allocator very easy to use:

core.alloc.gc.auto() {
    // Every allocation here will automatically be freed
}
GCLink
GCLink :: struct {
    prev: &GCLink;
    next: &GCLink;
    magic_number: u32;
}
GCState
GCState :: struct {
    backing_allocator: Allocator;
    first: &GCLink;
}
auto
auto :: macro () -> void
auto :: macro (body: Code) -> i32
clear
clear :: (hs: &GCState) -> void
make
make :: (backing: Allocator) -> GCState
make_allocator
make_allocator :: (hs: &GCState) -> Allocator
untrack
untrack :: (ptr: rawptr) -> bool

Removes an allocation from the garbage collectors tracking list, so it will not be freed automatically.