Refactor memory utilities: unify validation logic, adjust return types, and clean up main.zig formatting.
This commit is contained in:
@@ -3,7 +3,7 @@ const std = @import("std");
|
|||||||
pub fn build(b: *std.Build) void
|
pub fn build(b: *std.Build) void
|
||||||
{
|
{
|
||||||
const target = b.standardTargetOptions(.{
|
const target = b.standardTargetOptions(.{
|
||||||
.default_target = .{ .abi = .musl },
|
// .default_target = .{ .abi = .musl },
|
||||||
});
|
});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
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);
|
exe.root_module.addOptions("build_options", options);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ pub fn main(init: std.process.Init) !void
|
|||||||
|
|
||||||
const allocator = if (debug) gpa.allocator() else std.heap.smp_allocator;
|
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);
|
errdefer _ = zocket.memory.deinitIfExists(&tmp_arena);
|
||||||
|
|
||||||
const tmp_allocator = tmp_arena.?.allocator();
|
const tmp_allocator = tmp_arena.?.allocator();
|
||||||
|
|||||||
@@ -9,19 +9,8 @@ const std = @import("std");
|
|||||||
/// to deinitialize the same resource again.
|
/// to deinitialize the same resource again.
|
||||||
///
|
///
|
||||||
/// The return value of `resource.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())
|
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;
|
if (!live.*) return null;
|
||||||
|
|
||||||
live.* = false;
|
live.* = false;
|
||||||
@@ -40,7 +29,7 @@ pub fn deinitIfLive(resource: anytype, live: *bool) ?@TypeOf(resource.deinit())
|
|||||||
///
|
///
|
||||||
/// The return value of `resource.*.?.deinit()` is returned
|
/// The return value of `resource.*.?.deinit()` is returned
|
||||||
/// from the function.
|
/// from the function.
|
||||||
pub fn deinitIfExists(resource: anytype) ?DeinitReturn(@TypeOf(resource))
|
pub fn deinitIfExists(resource: anytype) ?ExistsDeinitReturn(@TypeOf(resource))
|
||||||
{
|
{
|
||||||
if (resource.*) |*payload|
|
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
|
fn Payload(comptime Pointer: type) type
|
||||||
{
|
{
|
||||||
if (@typeInfo(Pointer) != .pointer)
|
const Child = ValidatedChild(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)
|
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;
|
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" {
|
test "deinitIfLive deinitializes an ArenaAllocator once" {
|
||||||
|
|||||||
Reference in New Issue
Block a user