Refactor memory utilities: unify validation logic, adjust return types, and clean up main.zig formatting.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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" {
|
||||
|
||||
Reference in New Issue
Block a user