tiny.bench.CountingAllocator
Defined in tiny.bench.
API (6)
Actions
Public operations.
Fields and members
Public fields and members.
Source
Source: lib/bench/src/allocation.zig:24
zig
pub const CountingAllocator = struct { backing: Allocator, counts: Counts = .{}, mutex: std.atomic.Mutex = .unlocked, pub fn init(backing: Allocator) CountingAllocator { return .{ .backing = backing }; } pub fn allocator(self: *CountingAllocator) Allocator { return .{ .ptr = self, .vtable = &vtable }; } /// Zeroes the traffic counters and sets the high water back to the live /// total. The live-byte total survives the reset, because memory handed out /// earlier is still outstanding. The high water restarts at the live total, /// so the next window reports the peak that window itself reached. pub fn reset(self: *CountingAllocator) void { self.lock(); defer self.mutex.unlock(); const live = self.counts.live_bytes; self.counts = .{ .live_bytes = live, .peak_live_bytes = live }; } const vtable: Allocator.VTable = .{ .alloc = rawAlloc, .resize = rawResize, .remap = rawRemap, .free = rawFree, }; fn rawAlloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 { const self: *CountingAllocator = @ptrCast(@alignCast(ctx)); const ptr = self.backing.rawAlloc(len, alignment, ret_addr) orelse return null; self.lock(); defer self.mutex.unlock(); self.counts.alloc_count +|= 1; self.counts.alloc_bytes +|= timing.saturatingU64(len); self.growLive(timing.saturatingU64(len)); return ptr; } fn rawResize(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool { const self: *CountingAllocator = @ptrCast(@alignCast(ctx)); const resized = self.backing.rawResize(memory, alignment, new_len, ret_addr); if (resized) self.recordLength(memory.len, new_len); return resized; } fn rawRemap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { const self: *CountingAllocator = @ptrCast(@alignCast(ctx)); const ptr = self.backing.rawRemap(memory, alignment, new_len, ret_addr) orelse return null; self.recordLength(memory.len, new_len); return ptr; } fn rawFree(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void { const self: *CountingAllocator = @ptrCast(@alignCast(ctx)); self.backing.rawFree(memory, alignment, ret_addr); self.lock(); defer self.mutex.unlock(); self.counts.free_count +|= 1; self.counts.live_bytes -|= timing.saturatingU64(memory.len); } fn recordLength(self: *CountingAllocator, old_len: usize, new_len: usize) void { if (new_len > old_len) { const grew = timing.saturatingU64(new_len - old_len); self.lock(); defer self.mutex.unlock(); self.counts.alloc_bytes +|= grew; self.counts.live_bytes +|= grew; self.counts.peak_live_bytes = @max( self.counts.peak_live_bytes, self.counts.live_bytes, ); return; } if (new_len == old_len) return; self.lock(); defer self.mutex.unlock(); self.counts.live_bytes -|= timing.saturatingU64(old_len - new_len); } fn growLive(self: *CountingAllocator, bytes: u64) void { self.counts.live_bytes +|= bytes; self.counts.peak_live_bytes = @max( self.counts.peak_live_bytes, self.counts.live_bytes, ); } fn lock(self: *CountingAllocator) void { while (!self.mutex.tryLock()) std.atomic.spinLoopHint(); }};Source: lib/bench/src/root.zig:79
zig
pub const CountingAllocator = allocation.CountingAllocator;Complete caller list for CountingAllocator.allocator
7 direct callers.
lib.bench.src.allocation.test_allocation_counters_report_a_window_high_water[function] — test source atlib/bench/src/allocation.zig:150in nearest public ownerlib.bench.src.allocationlib.bench.src.allocation.test_allocation_counters_retain_concurrent_updates[function] — test source atlib/bench/src/allocation.zig:121in nearest public ownerlib.bench.src.allocationlib.bench.src.suite.runOne[function] — private source atlib/bench/src/suite.zig:645in nearest public ownerlib.bench.src.suitelib.bumpalo.src.properties.buffer.exerciseFailure[function] — private source atlib/bumpalo/src/properties/buffer.zig:177in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.buffer.exerciseResetCase[function] — private source atlib/bumpalo/src/properties/buffer.zig:146in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.buffer.test_allocator_view_follows_the_caller_buffer_root_lifecycle[function] — test source atlib/bumpalo/src/properties/buffer.zig:326in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.capacity.CallerCapacityProperty.property[function] — private source atlib/bumpalo/src/properties/capacity.zig:127in nearest public ownerlib.bumpalo.src.properties.capacity
Complete caller list for CountingAllocator.init
8 direct callers.
lib.bench.src.allocation.test_allocation_counters_report_a_window_high_water[function] — test source atlib/bench/src/allocation.zig:150in nearest public ownerlib.bench.src.allocationlib.bench.src.allocation.test_allocation_counters_retain_concurrent_updates[function] — test source atlib/bench/src/allocation.zig:121in nearest public ownerlib.bench.src.allocationlib.bench.src.suite.runOne[function] — private source atlib/bench/src/suite.zig:645in nearest public ownerlib.bench.src.suitelib.bumpalo.src.properties.buffer.CallerBufferProperty[function] — private source atlib/bumpalo/src/properties/buffer.zig:287in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.buffer.exerciseFailure[function] — private source atlib/bumpalo/src/properties/buffer.zig:177in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.buffer.exerciseResetCase[function] — private source atlib/bumpalo/src/properties/buffer.zig:146in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.buffer.test_allocator_view_follows_the_caller_buffer_root_lifecycle[function] — test source atlib/bumpalo/src/properties/buffer.zig:326in nearest public ownerlib.bumpalo.src.properties.bufferlib.bumpalo.src.properties.capacity.CallerCapacityProperty.property[function] — private source atlib/bumpalo/src/properties/capacity.zig:127in nearest public ownerlib.bumpalo.src.properties.capacity
Audit
| Definitions | 4 |
|---|---|
| Public names | 4 |
| Members | 3 |
| Version | 26.7.0 |
| Revision | daab053ee433 |