Introduce deinitIfExists utility and replace deinitIfLive for optional resource cleanup in main.zig.

This commit is contained in:
2026-08-02 23:41:15 +02:00
parent 88d312e713
commit 0a7ad2662c
2 changed files with 90 additions and 14 deletions

View File

@@ -1,23 +1,25 @@
const std = @import("std");
/// Deinitializes `resource` only if it is still live.
/// Deinitializes `resource` (allocator owner) only if it is still live.
///
/// Pass a pointer to the original allocator owner,
/// such as an `ArenaAllocator`.
/// Pass a pointer to the allocator owner, such as an `ArenaAllocator`.
///
/// If `live` is `true`, calls `allocator.deinit()` and then sets
/// If `live` is `true`, calls `resource.deinit()` and then sets
/// `live` to `false`, preventing a later cleanup path from attempting
/// to deinitialize the same allocator again.
/// to deinitialize the same resource again.
///
/// The return value of `deinit()` is returned from the function.
/// The return value of `resource.deinit()` is returned from the function.
pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
{
comptime
{
const T = @TypeOf(resource);
const Pointer = @TypeOf(resource);
if (@typeInfo(T) != .pointer) @compileError("deinitIfLive expects a pointer!");
if (T == std.mem.Allocator) @compileError("deinitIfLive expects a pointer to an allocator owner, not std.mem.Allocator!");
if (@typeInfo(Pointer) != .pointer)
@compileError("deinitIfLive expects a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("deinitIfLive expects a single-item pointer!");
}
if (!live.*) return null;
@@ -25,3 +27,78 @@ pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
live.* = false;
return resource.deinit();
}
/// Deinitializes the resources stored in `resource`
/// (allocator owner) only if still exists.
///
/// Pass a pointer to an optional allocator owner, such as
/// `*?std.heap.ArenaAllocator`.
///
/// If `resource.*` is non-null, calls `deinit()` on the payload
/// and then sets `resource.* = null`, preventing a later cleanup
/// path from attempting to deinitialize the same resource again.
///
/// The return value of `resource.*.?.deinit()` is returned
/// from the function.
pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource))
{
if (resource.*) |*payload|
{
const result = payload.deinit();
resource.* = null;
return result;
}
return null;
}
//
fn DeinitReturn(comptime Pointer: type) type {
return @typeInfo(@TypeOf(Payload(Pointer).deinit)).@"fn".return_type.?;
}
fn Payload(comptime Pointer: type) type
{
if (@typeInfo(Pointer) != .pointer)
@compileError("deinitIfExists expects a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("deinitIfExists expects a single-item pointer!");
const Child = @typeInfo(Pointer).pointer.child;
if (@typeInfo(Child) != .optional)
@compileError("deinitIfExists expects a pointer to an optional resource (*?T)!");
return @typeInfo(Child).optional.child;
}
//
test "deinitIfLive deinitializes an ArenaAllocator once" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
var live = true;
const alloc = arena.allocator();
_ = try alloc.alloc(u8, 1);
_ = deinitIfLive(&arena, &live);
try std.testing.expect(!live);
// Must not deinitialize it again.
try std.testing.expect(deinitIfLive(&arena, &live) == null);
}
test "deinitIfExists deinitializes an optional ArenaAllocator once" {
var arena: ?std.heap.ArenaAllocator = .init(std.testing.allocator);
const alloc = arena.?.allocator();
_ = try alloc.alloc(u8, 1);
_ = deinitIfExists(&arena);
try std.testing.expect(arena == null);
// Must not deinitialize it again.
try std.testing.expect(deinitIfExists(&arena) == null);
}