Introduce deinitIfLive utility and integrate enhanced memory management

This commit is contained in:
2026-08-02 22:59:02 +02:00
parent b1bdfccd1e
commit d70a051a57
3 changed files with 62 additions and 10 deletions

27
src/memory/util.zig Normal file
View File

@@ -0,0 +1,27 @@
const std = @import("std");
/// Deinitializes `allocator` only if it is still live.
///
/// Pass a pointer to the original allocator owner, such as an
/// `ArenaAllocator` or `DebugAllocator`.
///
/// If `live` is `true`, calls `allocator.deinit()` and then sets
/// `live` to `false`, preventing a later cleanup path from attempting
/// to deinitialize the same allocator again.
///
/// The return value of `deinit()` is discarded.
pub fn deinitIfLive(allocator: anytype, live: *bool) void
{
comptime
{
const T = @TypeOf(allocator);
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 (live.*) {
_ = allocator.deinit();
live.* = false;
}
}