-
Book Overview & Buying
-
Table Of Contents
Systems Programming with Zig
By :
The three standard allocators covered in the previous chapter handle the vast majority of real-world workloads. But understanding what an allocator actually is — not just how to use one — requires building one from scratch. Zig makes this unusually accessible because std.mem.Allocator is not a special compiler construct or a privileged runtime object. It is an ordinary interface: a struct containing a pointer to an implementation and a vtable of three function pointers covering alloc(), resize(), and free(). Any struct that satisfies those three operations and exposes an .allocator() method to produce a vtable-bound value is a fully interchangeable std.mem.Allocator — passable to std.ArrayList, standard library containers, and any function that accepts the interface. The TrackingAllocator we built in this section takes a slightly different approach: it exposes allocation methods directly rather than wiring into the...