drop/zig-lib_no-sugar/test-gpa2.zig

40 lines
1.0 KiB
Zig

const std = @import("std");
const fmt = std.fmt;
const testing = std.testing;
const print = std.debug.print;
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
pub const Context = struct {
const Self = @This();
rundir: [:0]u8,
pub fn init() !Self {
var rundir = try gpa.allocator().dupeZ(u8, "/tmp/libipc-run/");
return Self{ .rundir = rundir };
}
pub fn deinit(self: *Self) void {
gpa.allocator().free(self.rundir);
}
};
export fn ipc_context_init() callconv(.C) ?*anyopaque {
var new_context = gpa.allocator().create(Context) catch return null;
new_context.* = Context.init() catch return null;
return new_context;
}
export fn ipc_context_deinit(opaque_ctx: *anyopaque) callconv(.C) void {
var ctx = @ptrCast(*Context, @alignCast(@alignOf(Context), opaque_ctx));
ctx.deinit();
gpa.allocator().destroy(ctx);
}
test "context_init_deinit" {
const ctx = ipc_context_init();
try std.testing.expect(ctx != null);
ipc_context_deinit(ctx.?);
}