Refactor memory utilities: unify validation logic, adjust return types, and clean up main.zig formatting.

This commit is contained in:
2026-08-03 12:43:34 +02:00
parent 0a7ad2662c
commit 2688cacfa7
3 changed files with 27 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ const std = @import("std");
pub fn build(b: *std.Build) void
{
const target = b.standardTargetOptions(.{
.default_target = .{ .abi = .musl },
// .default_target = .{ .abi = .musl },
});
const optimize = b.standardOptimizeOption(.{});
@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) void
}),
});
exe.root_module.link_libc = true;
// exe.root_module.link_libc = true;
exe.root_module.addOptions("build_options", options);
//

View File

@@ -28,7 +28,7 @@ pub fn main(init: std.process.Init) !void
const allocator = if (debug) gpa.allocator() else std.heap.smp_allocator;
var tmp_arena: ?std.heap.ArenaAllocator = .init(allocator);
var tmp_arena: ?std.heap.ArenaAllocator = .init(allocator);
errdefer _ = zocket.memory.deinitIfExists(&tmp_arena);
const tmp_allocator = tmp_arena.?.allocator();

View File

@@ -9,19 +9,8 @@ const std = @import("std");
/// to deinitialize the same resource again.
///
/// The return value of `resource.deinit()` is returned from the function.
pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
pub fn deinitIfLive(resource: anytype, live: *bool) ?LiveDeinitReturn(@TypeOf(resource))
{
comptime
{
const Pointer = @TypeOf(resource);
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;
live.* = false;
@@ -40,7 +29,7 @@ pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
///
/// The return value of `resource.*.?.deinit()` is returned
/// from the function.
pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource))
pub fn deinitIfExists(resource: anytype) ?ExistsDeinitReturn(@TypeOf(resource))
{
if (resource.*) |*payload|
{
@@ -54,26 +43,36 @@ pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource))
//
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;
const Child = ValidatedChild(Pointer);
if (@typeInfo(Child) != .optional)
@compileError("deinitIfExists expects a pointer to an optional resource (*?T)!");
@compileError("Expected a pointer to an optional resource (*?T)!");
return @typeInfo(Child).optional.child;
}
fn ValidatedChild(comptime Pointer: type) type
{
if (@typeInfo(Pointer) != .pointer)
@compileError("Expected a pointer!");
if (@typeInfo(Pointer).pointer.size != .one)
@compileError("Expected a single-item pointer!");
return @typeInfo(Pointer).pointer.child;
}
fn LiveDeinitReturn(comptime Pointer: type) type {
const Child = ValidatedChild(Pointer);
return @typeInfo(@TypeOf(Child.deinit)).@"fn".return_type.?;
}
fn ExistsDeinitReturn(comptime Pointer: type) type {
return @typeInfo(@TypeOf(Payload(Pointer).deinit)).@"fn".return_type.?;
}
//
test "deinitIfLive deinitializes an ArenaAllocator once" {