From c5e3b7b90141ddf0bd354e73946859f7f96ccaa3 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Mon, 16 Jan 2023 20:34:23 +0100 Subject: [PATCH] Binding example: works as expected (with c_allocator for now). --- zig-impl/drop/Makefile | 6 +++++ zig-impl/drop/src/main.c | 23 +++++++++++++----- zig-impl/drop/src/main.zig | 48 ++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/zig-impl/drop/Makefile b/zig-impl/drop/Makefile index 6b68f35..e7bbbff 100644 --- a/zig-impl/drop/Makefile +++ b/zig-impl/drop/Makefile @@ -30,3 +30,9 @@ zigcompilation: build.zig src/*.zig compilation: src/main.c @echo the following compilation will produce errors despite actually working $(CC) -o app -o main build/libipc.so $(CFLAGS) $^ $(LDFLAGS) + +run: + LD_LIBRARY_PATH=build ./main + +valgrind: + LD_LIBRARY_PATH=build valgrind --suppressions=./suppress-stuff.suppr --gen-suppressions=all -v --leak-check=full ./main diff --git a/zig-impl/drop/src/main.c b/zig-impl/drop/src/main.c index 690feb9..03ac160 100644 --- a/zig-impl/drop/src/main.c +++ b/zig-impl/drop/src/main.c @@ -1,13 +1,24 @@ #include +#include int main(void) { - int ret = someipc(20,10); - printf("hello %d\n", ret); + int ret = 0; - void *somestructure = NULL; - some_struct_bidouillage_init(&somestructure); - int value = some_struct_bidouillage_update(&somestructure); - printf("value %d\n", value); + printf ("Init context.\n"); + void *ctx = NULL; + ret = ipc_context_init (&ctx); + if (ret != 0) { + printf ("Cannot init context.\n"); + return 1; + } + + // TODO: do stuff + + printf ("Deinit context\n"); + ipc_context_deinit (ctx); + printf ("Context deinit.\n"); + free(ctx); + printf ("Context completely freed.\n"); return 0; } diff --git a/zig-impl/drop/src/main.zig b/zig-impl/drop/src/main.zig index 9cf93f3..2fa8b1d 100644 --- a/zig-impl/drop/src/main.zig +++ b/zig-impl/drop/src/main.zig @@ -1,42 +1,34 @@ const std = @import("std"); +const fmt = std.fmt; +const heap = std.heap; const testing = std.testing; const print = std.debug.print; -const SOMESTRUCT = packed struct { - somevalue: i32, +pub const Context = struct { + rundir: [] u8, const Self = @This(); - fn update(self: *Self) callconv(.C) i32 { - self.somevalue += 1; - return self.somevalue; + pub fn init() !Self { + var rundir = try std.heap.c_allocator.dupeZ(u8, "/tmp/libipc-run/"); + return Self { .rundir = rundir }; + } + + pub fn deinit(self: *Self) void { + std.heap.c_allocator.free(self.rundir); } }; -export fn some_struct_bidouillage_init(ptr: *anyopaque) callconv(.C) void { - var pointer = @ptrCast(*SOMESTRUCT, @alignCast(@alignOf(SOMESTRUCT),ptr)); - var somestruct = std.heap.c_allocator.create(SOMESTRUCT) catch null; +export fn ipc_context_init (ptr: **Context) callconv(.C) i32 { + ptr.* = std.heap.c_allocator.create(Context) catch return 1; - print ("hello we just did something\n", .{}); - if (somestruct) |s| { - s.somevalue = 2; - print ("just changed a value\n", .{}); - pointer.* = s.*; - } - print ("hello again\n", .{}); -// else { -// pointer.* = null; -// } + ptr.*.* = Context.init() catch |err| { + print ("libipc: error while init context: {}\n", .{err}); + return 1; + }; + return 0; } -export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 { - return s.update(); -} - -export fn someipc(a: i32, b: i32) callconv(.C) i32 { - return a + b; -} - -test "basic add functionality" { - try testing.expect(someipc(3, 7) == 10); +export fn ipc_context_deinit (ctx: *Context) callconv(.C) void { + ctx.deinit(); }