Skip to documentation
SLOP

alloc_fixed.Tracked

Reference alloc_fixed Tracked

Internal implementation documentation

Defined in alloc_fixed.

When a caller allocates from its own slice and needs to find out afterwards how much memory the work took, this fixed-capacity buffer allocator records usage figures alongside a sticky record of any raw allocation that ran out of room.

API (9)

Actions

Public operations.

Types and contracts

Public types and contracts.

Fields and members

Public fields and members.

No direct callersNo direct callsalloc_fixedTracked
Static calls · unresolved targets: unknown · external targets: unknown.

Source

Source: lib/alloc/fixed/src/fixed.zig:67

zig
/// When a caller allocates from its own slice and needs to find out afterwards/// how much memory the work took, this fixed-capacity buffer allocator records/// usage figures alongside a sticky record of any raw allocation that ran out/// of room. It wraps a `FixedBuffer` and records the current bump frontier/// offset, the peak since the last reset, the high-water mark over its life,/// and the allocation failure.////// ### Owner stability/// The instance stays alive at one memory address for as long as any handle/// from `allocator()` is in use. One instance owns one set of numbers, and the/// caller takes no copy of it to act as a second allocator over that same/// state.pub const Tracked = struct {    /// The fixed-buffer storage owner whose frontier every other field counts,    /// maintained by `Tracked`.    fixed: FixedBuffer,    /// Furthest the bump frontier has reached in the current interval, which    /// starts when the instance starts and again at every `reset()`.    peak_bytes: usize = 0,    /// Furthest the bump frontier has reached at any point in the life of the    /// struct, the figure for sizing the buffer. A `reset()` leaves it    /// standing.    high_water_bytes: usize = 0,    /// Set to `true` exactly when `rawAlloc` returns `null` for want of room in    /// the buffer, separating a buffer too small from work that fits. A failed    /// `rawResize` or `rawRemap` leaves it alone. Calling `reset()` clears it    /// to `false`.    exhausted: bool = false,    const Self = @This();    /// A point-in-time snapshot of the numbers a `Tracked` allocator records,    /// used to read every figure at once as it stood when taken.    pub const Status = struct {        /// Current bump frontier offset in bytes, counting the payload and the        /// alignment padding.        used_bytes: usize,        /// Furthest the bump frontier reached in the interval that ends here,        /// which began at the last reset, or at initialization when there was        /// none.        peak_bytes: usize,        /// Furthest the bump frontier reached at any point, with every reset        /// behind it counted.        high_water_bytes: usize,        /// Indicates whether a raw allocation failure happened since        /// initialization or the last reset. A failed in-place resize or remap        /// leaves it unchanged.        exhausted: bool,    };    /// Starts a `Tracked` instance over a byte slice the caller supplies. The    /// caller keeps ownership of `bytes`. It performs no backing allocation.    pub fn init(bytes: []u8) Self {        return .{ .fixed = FixedBuffer.init(bytes) };    }    /// Hands the whole buffer back to the next round of work by resetting the    /// bump frontier, the peak offset, and the exhausted flag. It rewinds the    /// frontier to zero, sets `peak_bytes` to 0, and sets `exhausted` to    /// `false`. It leaves `high_water_bytes` standing across resets. Every    /// active allocation becomes invalid, and the space is available again.    pub fn reset(self: *Self) void {        self.fixed.reset();        self.peak_bytes = 0;        self.exhausted = false;    }    /// Returns a borrowed `std.mem.Allocator` handle pointing at `self` to pass    /// to code that allocates. The `self` instance stays alive at one memory    /// address while the handle is in use.    pub fn allocator(self: *Self) Allocator {        return .{ .ptr = self, .vtable = &vtable };    }    /// Returns a snapshot of the current usage numbers by value so a caller can    /// inspect them. The caller serializes access, because the read is unsafe    /// while another thread mutates the allocator.    pub fn status(self: *const Self) Status {        return .{            .used_bytes = used(&self.fixed),            .peak_bytes = self.peak_bytes,            .high_water_bytes = self.high_water_bytes,            .exhausted = self.exhausted,        };    }    fn observeUsage(self: *Self) void {        const used_bytes = used(&self.fixed);        self.peak_bytes = @max(self.peak_bytes, used_bytes);        self.high_water_bytes = @max(self.high_water_bytes, used_bytes);    }    fn rawAlloc(        context: *anyopaque,        len: usize,        alignment: Alignment,        return_address: usize,    ) ?[*]u8 {        const self: *Self = @ptrCast(@alignCast(context));        const result = self.fixed.allocator().rawAlloc(            len,            alignment,            return_address,        ) orelse {            self.exhausted = true;            return null;        };        self.observeUsage();        return result;    }    fn rawResize(        context: *anyopaque,        memory: []u8,        alignment: Alignment,        new_len: usize,        return_address: usize,    ) bool {        const self: *Self = @ptrCast(@alignCast(context));        const resized = self.fixed.allocator().rawResize(            memory,            alignment,            new_len,            return_address,        );        if (resized) self.observeUsage();        return resized;    }    fn rawRemap(        context: *anyopaque,        memory: []u8,        alignment: Alignment,        new_len: usize,        return_address: usize,    ) ?[*]u8 {        const self: *Self = @ptrCast(@alignCast(context));        const result = self.fixed.allocator().rawRemap(            memory,            alignment,            new_len,            return_address,        );        if (result != null) self.observeUsage();        return result;    }    fn rawFree(        context: *anyopaque,        memory: []u8,        alignment: Alignment,        return_address: usize,    ) void {        const self: *Self = @ptrCast(@alignCast(context));        self.fixed.allocator().rawFree(            memory,            alignment,            return_address,        );    }    const vtable: Allocator.VTable = .{        .alloc = rawAlloc,        .resize = rawResize,        .remap = rawRemap,        .free = rawFree,    };};

Source: lib/alloc/fixed/src/root.zig:446

zig
/// `Tracked` provides fixed-capacity bump allocation over a caller-supplied/// slice while measuring exact memory consumption. Bookkeeping records the/// current bump frontier offset, the peak offset reached since the last reset,/// the high-water mark over the owner's whole life, and the exhausted flag that/// latches any raw allocation that ran out of room.pub const Tracked = fixed.Tracked;
Called byCallsNo direct callstiny.accyartifact.InputJobdestroyprivate sourcelib.accy.src.artifact.input.InputJobinitializeprivate sourcelib.accy.src.artifact.input.InputJobrequireSameProgramprivate sourcelib.accy.src.artifact.input.InputJobrestoreDispatchprivate sourcelib.accy.src.artifact.input.InputJobrestoreKernel+25 moreTrackedallocator
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallsNo direct callsprivate sourcelib.accy.src.artifact.input.InputJobinitializeprivate sourcelib.accy.src.preparation.backendcheckBackendStorageprivate sourcelib.accy.src.preparation.bufferization.passcheckBufferStorageprivate sourcelib.accy.src.preparation.kernelization.loweri...checkBuilderStorageprivate sourcelib.accy.src.preparation.layoutcheckLayoutStorage+19 moreTrackedinit
Static calls · unresolved targets: 1 · external targets: 0.
Called byCallsNo direct callstest sourcelib.alloc.fixed.src.testtest: recycling chunks leave maximum ...test sourcelib.alloc.fixed.src.testtest: tracked fixed storage reports e...Trackedreset
Static calls · unresolved targets: 1 · external targets: 0.
Called byCallsprivate sourcelib.accy.src.preparation.kernelization.loweri...checkBuilderStoragetest sourcelib.alloc.fixed.src.testtest: recycling chunks distinguish me...test sourcelib.alloc.fixed.src.testtest: recycling chunks leave maximum ...test sourcelib.alloc.fixed.src.testtest: recycling chunks preserve a liv...test sourcelib.alloc.fixed.src.testtest: recycling chunks release reloca...+5 morealloc_fixedusedTrackedstatus
Static calls · unresolved targets: 0 · external targets: 0.

Complete caller list for Tracked.allocator

30 direct callers.

Complete caller list for Tracked.init

24 direct callers.

Complete caller list for Tracked.status

10 direct callers.

Audit

Definitions6
Public names6
Members8
Version26.7.0
Revisiondaab053ee433