From a6cfa88f798b6697d8b14c8b0bba11f0d585b2f7 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Mon, 16 Jan 2023 21:58:22 +0100 Subject: [PATCH] Context uses an allocator, currently a simple c_allocator. --- zig-impl/drop/src/main.zig | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/zig-impl/drop/src/main.zig b/zig-impl/drop/src/main.zig index 2fa8b1d..08bcf21 100644 --- a/zig-impl/drop/src/main.zig +++ b/zig-impl/drop/src/main.zig @@ -1,28 +1,27 @@ const std = @import("std"); const fmt = std.fmt; -const heap = std.heap; -const testing = std.testing; const print = std.debug.print; pub const Context = struct { rundir: [] u8, + allocator: std.mem.Allocator, const Self = @This(); - pub fn init() !Self { - var rundir = try std.heap.c_allocator.dupeZ(u8, "/tmp/libipc-run/"); - return Self { .rundir = rundir }; + pub fn init(allocator: std.mem.Allocator) !Self { + var rundir = try allocator.dupeZ(u8, "/tmp/libipc-run/"); + return Self { .rundir = rundir, .allocator = allocator }; } pub fn deinit(self: *Self) void { - std.heap.c_allocator.free(self.rundir); + self.allocator.free(self.rundir); } }; export fn ipc_context_init (ptr: **Context) callconv(.C) i32 { ptr.* = std.heap.c_allocator.create(Context) catch return 1; - ptr.*.* = Context.init() catch |err| { + ptr.*.* = Context.init(std.heap.c_allocator) catch |err| { print ("libipc: error while init context: {}\n", .{err}); return 1; };