Skip to documentation
SLOP

tiny.bench.CountingAllocator

Reference tiny.bench CountingAllocator

Defined in tiny.bench.

API (6)

Actions

Public operations.

Fields and members

Public fields and members.

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

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;
Called byCallsNo direct callstest sourcelib.bench.src.allocationtest: allocation counters report a wi...test sourcelib.bench.src.allocationtest: allocation counters retain conc...private sourcelib.bench.src.suiterunOneprivate sourcelib.bumpalo.src.properties.bufferexerciseFailureprivate sourcelib.bumpalo.src.properties.bufferexerciseResetCase+2 moreCountingAllocatorallocator
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallsNo direct callstest sourcelib.bench.src.allocationtest: allocation counters report a wi...test sourcelib.bench.src.allocationtest: allocation counters retain conc...private sourcelib.bench.src.suiterunOneprivate sourcelib.bumpalo.src.properties.bufferCallerBufferPropertyprivate sourcelib.bumpalo.src.properties.bufferexerciseFailure+3 moreCountingAllocatorinit
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallstest sourcelib.bench.src.allocationtest: allocation counters report a wi...private sourcelib.bench.src.suiterunOneprivate sourcelib.bench.src.allocation.CountingAllocatorlockCountingAllocatorreset
Static calls · unresolved targets: 0 · external targets: 1.

Complete caller list for CountingAllocator.allocator

7 direct callers.

Complete caller list for CountingAllocator.init

8 direct callers.

Audit

Definitions4
Public names4
Members3
Version26.7.0
Revisiondaab053ee433