Introduce deinitIfLive utility and integrate enhanced memory management
This commit is contained in:
27
src/memory/util.zig
Normal file
27
src/memory/util.zig
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user