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.
allocator: Returns a borrowedstd.mem.Allocatorhandle pointing atselfto pass to code that allocates.init: Starts aTrackedinstance over a byte slice the caller supplies.reset: Hands the whole buffer back to the next round of work by resetting the bump frontier, the peak offset, and the exhausted flag.status: Returns a snapshot of the current usage numbers by value so a caller can inspect them.
Types and contracts
Public types and contracts.
Status: A point-in-time snapshot of the numbers aTrackedallocator records, used to read every figure at once as it stood when taken.
Fields and members
Public fields and members.
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;Complete caller list for Tracked.allocator
30 direct callers.
tiny.accy.artifact.InputJob.destroy[method] atlib/accy/src/artifact/input.zig:83lib.accy.src.artifact.input.InputJob.initialize[function] — private source atlib/accy/src/artifact/input.zig:50in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.artifact.input.InputJob.requireSameProgram[method] — private source atlib/accy/src/artifact/input.zig:119in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.artifact.input.InputJob.restoreDispatch[method] — private source atlib/accy/src/artifact/input.zig:142in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.artifact.input.InputJob.restoreKernel[method] — private source atlib/accy/src/artifact/input.zig:198in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.artifact.input.InputJob.restoreMemory[method] — private source atlib/accy/src/artifact/input.zig:158in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.artifact.input.InputJob.restoreTarget[method] — private source atlib/accy/src/artifact/input.zig:214in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.preparation.backend.checkBackendStorage[function] — private source atlib/accy/src/preparation/backend.zig:519in nearest public ownertiny.accy.preparation.backendlib.accy.src.preparation.bufferization.pass.checkBufferStorage[function] — private source atlib/accy/src/preparation/bufferization/pass.zig:876in nearest public ownerlib.accy.src.preparation.bufferization.passlib.accy.src.preparation.kernelization.lowering.builder.checkBuilderStorage[function] — private source atlib/accy/src/preparation/kernelization/lowering/builder.zig:179in nearest public ownerlib.accy.src.preparation.kernelization.lowering.builderlib.accy.src.preparation.layout.checkLayoutStorage[function] — private source atlib/accy/src/preparation/layout.zig:515in nearest public ownertiny.accy.preparation.layoutlib.accy.src.preparation.memory.checkMemoryFailureStorage[function] — private source atlib/accy/src/preparation/memory.zig:700in nearest public ownertiny.accy.preparation.memorylib.accy.src.preparation.memory.checkMemoryStorage[function] — private source atlib/accy/src/preparation/memory.zig:660in nearest public ownertiny.accy.preparation.memorylib.alloc.fixed.src.test.test_recycling_chunks_distinguish_metadata_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:210in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_leave_maximum_plus_one_to_the_backing_owner[function] — test source atlib/alloc/fixed/src/test.zig:186in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_preserve_a_live_sibling_while_alternating[function] — test source atlib/alloc/fixed/src/test.zig:136in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_release_relocated_allocations[function] — test source atlib/alloc/fixed/src/test.zig:303in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_retain_aligned_storage_after_first_acquisition[function] — test source atlib/alloc/fixed/src/test.zig:166in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_alignment_padding_across_chunks[function] — test source atlib/alloc/fixed/src/test.zig:277in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_retained_storage_without_backing_growth[function] — test source atlib/alloc/fixed/src/test.zig:113in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_unwind_a_failed_nested_chunk_transaction[function] — test source atlib/alloc/fixed/src/test.zig:246in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_tracked_fixed_storage_reports_exact_capacity_and_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:36in nearest public ownerlib.alloc.fixed.src.testlib.choir.src.egraph.graph.checkMergeStorage[function] — private source atlib/choir/src/egraph/graph.zig:350in nearest public ownertiny.choir.egraph.graphlib.choir.src.passes.canonicalization.test_canonicalization_population_bounds_cover_sealed_builder_allocation_traffic[function] — test source atlib/choir/src/passes/canonicalization.zig:2906in nearest public ownertiny.choir.passes.canonicalizationlib.choir.src.passes.pass.test.checkWorkerAllocationFailure[function] — private source atlib/choir/src/passes/pass/test.zig:4157in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_caught_workspace_exhaustion_refuses_computation_results_hits_and_refresh[function] — test source atlib/choir/src/passes/pass/test.zig:4016in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_fixed_workspace_exhaustion_retains_cleanup_and_refuses_a_fresh_retry[function] — test source atlib/choir/src/passes/pass/test.zig:3364in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_host_OOM_before_workspace_allocation_stays_rejected[function] — test source atlib/choir/src/passes/pass/test.zig:4079in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_pass_caught_workspace_exhaustion_stops_subsequent_callbacks_through_one_finalizer[function] — test source atlib/choir/src/passes/pass/test.zig:3944in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.work.checkContainerGrowth[function] — private source atlib/choir/src/passes/pass/work.zig:187in nearest public ownertiny.choir.passes.pass.work
Complete caller list for Tracked.init
24 direct callers.
lib.accy.src.artifact.input.InputJob.initialize[function] — private source atlib/accy/src/artifact/input.zig:50in nearest public ownerlib.accy.src.artifact.inputlib.accy.src.preparation.backend.checkBackendStorage[function] — private source atlib/accy/src/preparation/backend.zig:519in nearest public ownertiny.accy.preparation.backendlib.accy.src.preparation.bufferization.pass.checkBufferStorage[function] — private source atlib/accy/src/preparation/bufferization/pass.zig:876in nearest public ownerlib.accy.src.preparation.bufferization.passlib.accy.src.preparation.kernelization.lowering.builder.checkBuilderStorage[function] — private source atlib/accy/src/preparation/kernelization/lowering/builder.zig:179in nearest public ownerlib.accy.src.preparation.kernelization.lowering.builderlib.accy.src.preparation.layout.checkLayoutStorage[function] — private source atlib/accy/src/preparation/layout.zig:515in nearest public ownertiny.accy.preparation.layoutlib.accy.src.preparation.memory.checkMemoryFailureStorage[function] — private source atlib/accy/src/preparation/memory.zig:700in nearest public ownertiny.accy.preparation.memorylib.accy.src.preparation.memory.checkMemoryStorage[function] — private source atlib/accy/src/preparation/memory.zig:660in nearest public ownertiny.accy.preparation.memorylib.alloc.fixed.src.test.test_recycling_chunks_distinguish_metadata_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:210in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_leave_maximum_plus_one_to_the_backing_owner[function] — test source atlib/alloc/fixed/src/test.zig:186in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_preserve_a_live_sibling_while_alternating[function] — test source atlib/alloc/fixed/src/test.zig:136in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_release_relocated_allocations[function] — test source atlib/alloc/fixed/src/test.zig:303in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_retain_aligned_storage_after_first_acquisition[function] — test source atlib/alloc/fixed/src/test.zig:166in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_alignment_padding_across_chunks[function] — test source atlib/alloc/fixed/src/test.zig:277in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_retained_storage_without_backing_growth[function] — test source atlib/alloc/fixed/src/test.zig:113in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_unwind_a_failed_nested_chunk_transaction[function] — test source atlib/alloc/fixed/src/test.zig:246in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_tracked_fixed_storage_reports_exact_capacity_and_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:36in nearest public ownerlib.alloc.fixed.src.testlib.choir.src.egraph.graph.checkMergeStorage[function] — private source atlib/choir/src/egraph/graph.zig:350in nearest public ownertiny.choir.egraph.graphlib.choir.src.passes.canonicalization.test_canonicalization_population_bounds_cover_sealed_builder_allocation_traffic[function] — test source atlib/choir/src/passes/canonicalization.zig:2906in nearest public ownertiny.choir.passes.canonicalizationlib.choir.src.passes.pass.test.checkWorkerAllocationFailure[function] — private source atlib/choir/src/passes/pass/test.zig:4157in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_caught_workspace_exhaustion_refuses_computation_results_hits_and_refresh[function] — test source atlib/choir/src/passes/pass/test.zig:4016in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_fixed_workspace_exhaustion_retains_cleanup_and_refuses_a_fresh_retry[function] — test source atlib/choir/src/passes/pass/test.zig:3364in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_analysis_host_OOM_before_workspace_allocation_stays_rejected[function] — test source atlib/choir/src/passes/pass/test.zig:4079in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.test.test_U0_pass_caught_workspace_exhaustion_stops_subsequent_callbacks_through_one_finalizer[function] — test source atlib/choir/src/passes/pass/test.zig:3944in nearest public ownerlib.choir.src.passes.pass.testlib.choir.src.passes.pass.work.checkContainerGrowth[function] — private source atlib/choir/src/passes/pass/work.zig:187in nearest public ownertiny.choir.passes.pass.work
Complete caller list for Tracked.status
10 direct callers.
lib.accy.src.preparation.kernelization.lowering.builder.checkBuilderStorage[function] — private source atlib/accy/src/preparation/kernelization/lowering/builder.zig:179in nearest public ownerlib.accy.src.preparation.kernelization.lowering.builderlib.alloc.fixed.src.test.test_recycling_chunks_distinguish_metadata_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:210in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_leave_maximum_plus_one_to_the_backing_owner[function] — test source atlib/alloc/fixed/src/test.zig:186in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_preserve_a_live_sibling_while_alternating[function] — test source atlib/alloc/fixed/src/test.zig:136in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_release_relocated_allocations[function] — test source atlib/alloc/fixed/src/test.zig:303in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_retain_aligned_storage_after_first_acquisition[function] — test source atlib/alloc/fixed/src/test.zig:166in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_alignment_padding_across_chunks[function] — test source atlib/alloc/fixed/src/test.zig:277in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_rewind_retained_storage_without_backing_growth[function] — test source atlib/alloc/fixed/src/test.zig:113in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_recycling_chunks_unwind_a_failed_nested_chunk_transaction[function] — test source atlib/alloc/fixed/src/test.zig:246in nearest public ownerlib.alloc.fixed.src.testlib.alloc.fixed.src.test.test_tracked_fixed_storage_reports_exact_capacity_and_exhaustion[function] — test source atlib/alloc/fixed/src/test.zig:36in nearest public ownerlib.alloc.fixed.src.test
Audit
| Definitions | 6 |
|---|---|
| Public names | 6 |
| Members | 8 |
| Version | 26.7.0 |
| Revision | daab053ee433 |