tiny.zen.DiagramDocumentStorage
Defined in tiny.zen.
API (27)
Actions
Public operations.
Types and contracts
Public types and contracts.
Fields and members
Public fields and members.
capacitydocument_bytesdocument_enddocument_peakdocument_slothigh_water_document_byteshigh_water_line_scratch_byteshigh_water_source_bytesin_useline_scratchphasepreparedrejected_document_countsource_bytes
Source
Source: lib/zen/src/diagram/document/storage.zig:25
zig
pub const Storage = struct { phase: alloc_phase.capacity.Phase, capacity: capacity_mod.Capacity, line_scratch: []u8, document_bytes: []u8, prepared: ?plan_mod.Plan = null, document_slot: [@sizeOf(spec.Document)]u8 align(@alignOf(spec.Document)) = undefined, document_end: usize = 0, document_peak: usize = 0, in_use: bool = false, source_bytes: usize = 0, high_water_source_bytes: usize = 0, high_water_line_scratch_bytes: usize = 0, high_water_document_bytes: usize = 0, rejected_document_count: u64 = 0, pub const Limits: type = capacity_mod.Limits; pub const Capacity: type = capacity_mod.Capacity; pub const Exhaustion: type = model.Exhaustion; pub const InitError = std.mem.Allocator.Error || capacity_mod.DeriveError; pub const ParseError: type = model.Error; pub const claim: alloc_phase.capacity.Declaration = .{ .source = .{ .id = "zen.diagram_document_storage", .kind = .phase_static, .limit_source = .caller, .storage = .{ .covered = &.{ .{ .id = "json_line_parse_scratch", .lifetime = .steady, .detail = "JSON line parse scratch", }, .{ .id = "parsed_diagram_document_values_and_retained_strings", .lifetime = .steady, .detail = "parsed diagram Document values and retained strings", }, }, .excluded = &.{ "caller-owned JSONL source", "ASCII and SVG render scratch, output, Markdown, and filesystem owners", }, }, .capacity = .{ .inputs = &.{ alloc_phase.capacity.bindInput(Limits, "max_line_scratch_bytes", "max_line_scratch_bytes"), alloc_phase.capacity.bindInput(Limits, "max_document_bytes", "max_document_bytes"), }, .type_selectors = &.{}, .nodes = &.{ .{ .input = 0 }, .{ .input = 1 }, .{ .add = .{ .left = 0, .right = 1 } }, }, .assertions = &.{.{ .scope = .closure_total, .measure = .retained, .relation = .exact, .expression = 2, }}, }, .overload = .{ .kind = .drop, .detail = "an in-use, oversized, or invalid attempted source is dropped while the two reusable regions remain eligible for later input", }, .risks = .{ .transitive = .{ .status = .witnessed, .detail = "survey and parsing use only caller source plus the two activated regions, with a stable owner-backed allocator context", }, .foreign = .{ .status = .excluded, .detail = "diagram Document parsing crosses no operating-system or foreign callback boundary", }, }, .obligations = &.{ .{ .key = "zen_diagram_document_capacity", .role = .capacity_model }, .{ .key = "zen_diagram_document_acquisition", .role = .custom }, .{ .key = "zen_diagram_document_oom", .role = .custom }, .{ .key = "zen_diagram_document_boundaries", .role = .overload }, .{ .key = "zen_diagram_document_reuse", .role = .overload }, .{ .key = "zen_diagram_document_differential", .role = .custom }, .{ .key = "zen_diagram_document_sealed", .role = .transitive_risk }, .{ .key = "zen_diagram_document_root", .role = .custom }, .{ .key = "zen_diagram_document_consumer", .role = .foreign_risk }, }, }, .bindings = .{ .owner = @This(), .seal = .{ .family = alloc_phase.capacity.selector(@This().activate), .premise = .{ .class = .checked_semantic_fact, .authority = .checker, }, }, .teardown = .{ .family = alloc_phase.capacity.selector(@This().deinit), .premise = .{ .class = .checked_semantic_fact, .authority = .checker, }, }, }, }; pub fn init(allocator: std.mem.Allocator, limits: Limits) InitError!Storage { const capacity = try Capacity.derive(limits); const line_scratch = try allocateRegion(allocator, capacity.line_scratch_bytes); errdefer allocator.free(line_scratch); const document_bytes = try allocateRegion(allocator, capacity.document_bytes); return .{ .phase = .initialization, .capacity = capacity, .line_scratch = line_scratch, .document_bytes = document_bytes, }; } pub fn initExact(allocator: std.mem.Allocator, jsonl: []const u8) !Storage { const line_scratch_bytes = try plan_mod.Plan.requiredLineScratchBytes(jsonl); const line_scratch = try allocateRegion(allocator, line_scratch_bytes); errdefer allocator.free(line_scratch); const plan = try plan_mod.Plan.inspect(line_scratch, jsonl); const capacity = try Capacity.derive(plan.exactLimits()); const document_bytes = try allocateRegion(allocator, capacity.document_bytes); return .{ .phase = .initialization, .capacity = capacity, .line_scratch = line_scratch, .document_bytes = document_bytes, .prepared = plan, .high_water_line_scratch_bytes = @intCast(plan.surveyed.scratch_high_water), }; } pub fn activate(self: *Storage) void { std.debug.assert(self.phase == .initialization); self.assertStorage(); self.phase = .steady; } pub fn parse(self: *Storage, jsonl: []const u8) ParseError!*const spec.Document { std.debug.assert(self.phase == .steady); if (self.in_use) return self.reject(error.DiagramDocumentStorageInUse); const required_line_scratch = plan_mod.Plan.requiredLineScratchBytes(jsonl) catch |err| { self.rejected_document_count +|= 1; return err; }; if (required_line_scratch > self.capacity.line_scratch_bytes) { return self.reject(error.DiagramLineScratchCapacityExceeded); } const plan = if (self.prepared) |prepared| if (prepared.matches(jsonl)) prepared else plan_mod.Plan.inspect(self.line_scratch, jsonl) catch |err| { self.rejected_document_count +|= 1; return err; } else plan_mod.Plan.inspect(self.line_scratch, jsonl) catch |err| { self.rejected_document_count +|= 1; return err; }; plan.require(self.capacity.limits) catch |err| return self.reject(err); self.document_end = 0; self.document_peak = 0; const parsed = spec.Document.parse( documentAllocator(self), self.line_scratch[0..plan.line_scratch_bytes], jsonl, ) catch |err| { self.observe(plan, jsonl.len); self.document_end = 0; self.document_peak = 0; self.prepared = null; self.rejected_document_count +|= 1; return err; }; documentSlot(self).* = parsed; self.in_use = true; self.source_bytes = jsonl.len; self.prepared = null; self.observe(plan, jsonl.len); self.assertStorage(); return documentSlot(self); } pub fn reset(self: *Storage) void { std.debug.assert(self.phase == .steady); std.debug.assert(self.in_use); documentSlot(self).deinit(); self.document_end = 0; self.document_peak = 0; self.in_use = false; self.source_bytes = 0; self.assertStorage(); } pub fn status(self: *const Storage) Status { return .{ .phase = self.phase, .in_use = self.in_use, .storage_bytes = self.capacity.storage_bytes, .line_scratch_bytes = self.capacity.line_scratch_bytes, .document_bytes = self.capacity.document_bytes, .source_bytes = self.source_bytes, .document_bytes_used = self.document_end, .high_water_source_bytes = self.high_water_source_bytes, .high_water_line_scratch_bytes = self.high_water_line_scratch_bytes, .high_water_document_bytes = self.high_water_document_bytes, .rejected_document_count = self.rejected_document_count, }; } pub fn deinit(self: *Storage, allocator: std.mem.Allocator) void { std.debug.assert(self.phase != .teardown); std.debug.assert(!self.in_use); self.assertStorage(); self.phase = .teardown; allocator.free(self.document_bytes); allocator.free(self.line_scratch); self.document_bytes = &.{}; self.line_scratch = &.{}; self.prepared = null; } fn reject(self: *Storage, err: model.Exhaustion) model.Exhaustion { self.rejected_document_count +|= 1; return err; } fn observe(self: *Storage, plan: plan_mod.Plan, source_bytes: usize) void { self.high_water_source_bytes = @max(self.high_water_source_bytes, source_bytes); self.high_water_line_scratch_bytes = @max( self.high_water_line_scratch_bytes, @as(usize, @intCast(plan.surveyed.scratch_high_water)), ); self.high_water_document_bytes = @max( self.high_water_document_bytes, @max(self.document_end, self.document_peak), ); } fn assertStorage(self: *const Storage) void { std.debug.assert(self.line_scratch.len == self.capacity.line_scratch_bytes); std.debug.assert(self.document_bytes.len == self.capacity.document_bytes); std.debug.assert(self.capacity.storage_bytes == self.line_scratch.len + self.document_bytes.len); std.debug.assert(self.document_end <= self.document_bytes.len); std.debug.assert(self.document_peak <= self.document_bytes.len); std.debug.assert(self.high_water_line_scratch_bytes <= self.line_scratch.len); std.debug.assert(self.high_water_document_bytes <= self.document_bytes.len); if (!self.in_use) std.debug.assert(self.source_bytes == 0); }};Source: lib/zen/src/root.zig:53
zig
/// Reusable caller-allocated storage for one parsed JSONL diagram.pub const DiagramDocumentStorage = diagram.DocumentStorage;Also reachable as
diagram.DocumentStorage, diagram.document.Storage.
Audit
| Definitions | 14 |
|---|---|
| Public names | 42 |
| Members | 14 |
| Version | 26.7.0 |
| Revision | daab053ee433 |