From 2688cacfa706018c876aed4c7380639d17fce24d Mon Sep 17 00:00:00 2001 From: Overlord Date: Mon, 3 Aug 2026 12:43:34 +0200 Subject: [PATCH] Refactor memory utilities: unify validation logic, adjust return types, and clean up `main.zig` formatting. --- build.zig | 4 ++-- src/main.zig | 2 +- src/memory/util.zig | 49 ++++++++++++++++++++++----------------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/build.zig b/build.zig index a446dbb..9da463e 100644 --- a/build.zig +++ b/build.zig @@ -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); // diff --git a/src/main.zig b/src/main.zig index 729ad99..43744f4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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(); diff --git a/src/memory/util.zig b/src/memory/util.zig index 5ec8266..044c8c4 100644 --- a/src/memory/util.zig +++ b/src/memory/util.zig @@ -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" {