From 7349abae79c76b514353f0b6a001bba0eacb4c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 6 May 2025 16:22:57 +0200 Subject: [PATCH] feat(core): WIP deinit arena allocator --- src/core/allocator.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/core/allocator.zig diff --git a/src/core/allocator.zig b/src/core/allocator.zig new file mode 100644 index 0000000..dbe0ca8 --- /dev/null +++ b/src/core/allocator.zig @@ -0,0 +1,32 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; + +pub const Callback = struct { + context: *anyopaque, + invokeFn: *const fn (ctx: *anyopaque) void, + + pub fn call(self: *const @This()) void { + self.invokeFn(self.context); + } + + pub fn create(obj: anytype) Callback { + return Callback { + .context = @ptrCast(@alignCast(obj)), + .invokeFn = @TypeOf(obj.*).deinit, + }; + } +}; + +pub const DeinitArena = struct { + arena: std.heap.ArenaAllocator, + deiniters: std.ArrayList(Callback), + + pub fn deinit(self: *DeinitArena) void { + for (self.deiniters.items) |deiniter| { + deiniter.deinit(deiniter.context); + } + + self.arena.deinit(); + } +};