tiny.memtrace.AllocationLedger
Defined in ledger.
API (13)
Actions
Public operations.
allocationallocationContainingallocationCountallocatorexhaustedfreeSequenceinitisActiveliveAllocationContaining
Fields and members
Public fields and members.
Source
Source: lib/memtrace/src/ledger.zig:18
zig
pub const AllocationLedger = struct { backing: Allocator, records: []Record, count: usize = 0, sequence: Sequence = 0, pub fn init(backing: Allocator, records: []Record) AllocationLedger { std.debug.assert(records.len != 0); return .{ .backing = backing, .records = records, }; } pub fn allocator(self: *AllocationLedger) Allocator { return .{ .ptr = self, .vtable = &vtable }; } pub fn exhausted(self: *const AllocationLedger) bool { return self.count == self.records.len; } pub fn allocationCount(self: *const AllocationLedger) usize { return self.count; } pub fn allocation( self: *const AllocationLedger, id: usize, ) Record { std.debug.assert(id < self.count); return self.records[id]; } pub fn allocationContaining( self: *const AllocationLedger, pointer: anytype, len: usize, ) ?usize { const address = @intFromPtr(pointer); var index = self.count; while (index != 0) { index -= 1; if (rangeContains(self.records[index], address, len)) return index; } return null; } pub fn liveAllocationContaining( self: *const AllocationLedger, pointer: anytype, len: usize, ) ?usize { const address = @intFromPtr(pointer); var index = self.count; while (index != 0) { index -= 1; const record = self.records[index]; if (record.active and rangeContains(record, address, len)) { return index; } } return null; } pub fn isActive( self: *const AllocationLedger, id: usize, ) bool { return self.allocation(id).active; } pub fn freeSequence( self: *const AllocationLedger, id: usize, ) Sequence { const record = self.allocation(id); std.debug.assert(!record.active); return record.free_sequence; } fn nextSequence(self: *AllocationLedger) Sequence { std.debug.assert(self.sequence != std.math.maxInt(Sequence)); self.sequence += 1; return self.sequence; } fn append( self: *AllocationLedger, pointer: [*]u8, len: usize, return_address: usize, ) void { std.debug.assert(self.count < self.records.len); self.records[self.count] = .{ .address = @intFromPtr(pointer), .len = len, .allocation_sequence = self.nextSequence(), .allocation_return_address = return_address, .active = true, }; self.count += 1; } fn findActiveExact( self: *const AllocationLedger, pointer: [*]u8, ) usize { const address = @intFromPtr(pointer); var index = self.count; while (index != 0) { index -= 1; const record = self.records[index]; if (record.active and record.address == address) return index; } unreachable; } fn rawAlloc( context: *anyopaque, len: usize, alignment: std.mem.Alignment, return_address: usize, ) ?[*]u8 { const self: *AllocationLedger = @ptrCast(@alignCast(context)); if (self.exhausted()) return null; const result = self.backing.rawAlloc( len, alignment, return_address, ) orelse return null; self.append(result, len, return_address); return result; } fn rawResize( context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, return_address: usize, ) bool { const self: *AllocationLedger = @ptrCast(@alignCast(context)); const resized = self.backing.rawResize( memory, alignment, new_len, return_address, ); if (resized) { const id = self.findActiveExact(memory.ptr); _ = self.nextSequence(); self.records[id].len = new_len; self.records[id].resize_return_address = return_address; } return resized; } fn rawRemap( context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, return_address: usize, ) ?[*]u8 { const self: *AllocationLedger = @ptrCast(@alignCast(context)); const id = self.findActiveExact(memory.ptr); const result = self.backing.rawRemap( memory, alignment, new_len, return_address, ) orelse return null; _ = self.nextSequence(); self.records[id].address = @intFromPtr(result); self.records[id].len = new_len; self.records[id].remap_return_address = return_address; return result; } fn rawFree( context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, return_address: usize, ) void { const self: *AllocationLedger = @ptrCast(@alignCast(context)); const id = self.findActiveExact(memory.ptr); self.backing.rawFree(memory, alignment, return_address); const sequence = self.nextSequence(); self.records[id].active = false; self.records[id].free_sequence = sequence; self.records[id].free_return_address = return_address; } const vtable: Allocator.VTable = .{ .alloc = rawAlloc, .resize = rawResize, .remap = rawRemap, .free = rawFree, };};Source: lib/memtrace/src/root.zig:51
zig
pub const AllocationLedger = ledger_mod.AllocationLedger;Audit
| Definitions | 10 |
|---|---|
| Public names | 20 |
| Members | 4 |
| Version | 26.7.0 |
| Revision | daab053ee433 |