From ec78813c5978bbfdc9d0b53e57bb2fe989697fe0 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 29 Apr 2023 01:33:32 +0200 Subject: [PATCH] Zig "C" library without sugar: renaming & grooming. --- zig-lib_no-sugar/app.c | 15 ++++++++++++ zig-lib_no-sugar/ipc.zig | 11 +++++++++ zig-lib_no-sugar/joke.c | 21 ---------------- zig-lib_no-sugar/lib.h | 9 +++++++ zig-lib_no-sugar/libipc.h | 9 ------- zig-lib_no-sugar/makefile | 11 ++++----- zig-lib_no-sugar/test-gpa.zig | 45 ---------------------------------- zig-lib_no-sugar/test-gpa2.zig | 39 ----------------------------- 8 files changed, 40 insertions(+), 120 deletions(-) create mode 100644 zig-lib_no-sugar/app.c create mode 100644 zig-lib_no-sugar/ipc.zig delete mode 100644 zig-lib_no-sugar/joke.c create mode 100644 zig-lib_no-sugar/lib.h delete mode 100644 zig-lib_no-sugar/libipc.h delete mode 100644 zig-lib_no-sugar/test-gpa.zig delete mode 100644 zig-lib_no-sugar/test-gpa2.zig diff --git a/zig-lib_no-sugar/app.c b/zig-lib_no-sugar/app.c new file mode 100644 index 0000000..26504d9 --- /dev/null +++ b/zig-lib_no-sugar/app.c @@ -0,0 +1,15 @@ +#include +#include +#include + +#include "./lib.h" + +int main(void) { + printf ("Calling ipc_hello\n"); + ipc_hello (); + + printf ("Calling ipc_bye\n"); + ipc_bye (); + + return 0; +} diff --git a/zig-lib_no-sugar/ipc.zig b/zig-lib_no-sugar/ipc.zig new file mode 100644 index 0000000..5a54ac8 --- /dev/null +++ b/zig-lib_no-sugar/ipc.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const print = std.debug.print; + +export fn ipc_hello() callconv(.C) void { + print("Hello!\n", .{}); +} + +export fn ipc_bye() callconv(.C) void { + print("Bye!\n", .{}); +} diff --git a/zig-lib_no-sugar/joke.c b/zig-lib_no-sugar/joke.c deleted file mode 100644 index 0e80e9d..0000000 --- a/zig-lib_no-sugar/joke.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include - -#include "./libipc.h" - -int main(void) { - printf ("Init context.\n"); - void *ctx = ipc_context_init (); - - if (ctx == NULL) { - printf ("Cannot init context.\n"); - return 1; - } - - printf ("Deinit context\n"); - ipc_context_deinit (ctx); - - printf ("Context freed.\n"); - return 0; -} diff --git a/zig-lib_no-sugar/lib.h b/zig-lib_no-sugar/lib.h new file mode 100644 index 0000000..c5357ca --- /dev/null +++ b/zig-lib_no-sugar/lib.h @@ -0,0 +1,9 @@ +#ifndef LIB +#define LIB + +#include + +void ipc_hello (void); +void ipc_bye (void); + +#endif diff --git a/zig-lib_no-sugar/libipc.h b/zig-lib_no-sugar/libipc.h deleted file mode 100644 index b580181..0000000 --- a/zig-lib_no-sugar/libipc.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef LIBIPC -#define LIBIPC - -#include - -void* ipc_context_init (void); -void ipc_context_deinit (void* ctx); - -#endif diff --git a/zig-lib_no-sugar/makefile b/zig-lib_no-sugar/makefile index c2e20f8..1460bec 100644 --- a/zig-lib_no-sugar/makefile +++ b/zig-lib_no-sugar/makefile @@ -1,17 +1,16 @@ # No sugar for you! build: lib - gcc -Os -I. -static -o joke joke.c libtest-gpa.a + gcc -Os -I. -static -o app app.c libipc.a lib: - zig build-lib test-gpa.zig -OReleaseSmall + zig build-lib ipc.zig -OReleaseSmall run: - $(VALGRIND) ./joke + $(VALGRIND) ./app include ../mk/makefile.valgrind -# Another library. -lib-alt: - zig build-lib test-gpa2.zig -OReleaseSmall +clean: + rm *.a *.o app diff --git a/zig-lib_no-sugar/test-gpa.zig b/zig-lib_no-sugar/test-gpa.zig deleted file mode 100644 index 5615d31..0000000 --- a/zig-lib_no-sugar/test-gpa.zig +++ /dev/null @@ -1,45 +0,0 @@ -const std = @import("std"); -const fmt = std.fmt; -const testing = std.testing; - -const print = std.debug.print; -const GPA = std.heap.GeneralPurposeAllocator(.{ .safety = true }); - -pub const Context = struct { - const Self = @This(); - rundir: [:0]u8, - gpa: GPA, - - pub fn init(gpa: *GPA) !Self { - var rundir = try gpa.allocator().dupeZ(u8, "/tmp/libipc-run/"); - return Self{ .rundir = rundir, .gpa = gpa.* }; - } - - pub fn deinit(self: *Self) void { - self.gpa.allocator().free(self.rundir); - } -}; - -export fn ipc_context_init() callconv(.C) ?*anyopaque { - std.log.err("Let's use GPA!", .{}); - var gpa = GPA{}; - var new_context = gpa.allocator().create(Context) catch return null; - new_context.* = Context.init(&gpa) 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(); - var gpa = ctx.gpa; // save the gpa before destroying ctx - std.log.err("Let's destroy Context!", .{}); - gpa.allocator().destroy(ctx); - _ = gpa.deinit(); // now deinit the gpa -} - -test "context_init_deinit" { - const ctx = ipc_context_init(); - try std.testing.expect(ctx != null); - - ipc_context_deinit(ctx.?); -} diff --git a/zig-lib_no-sugar/test-gpa2.zig b/zig-lib_no-sugar/test-gpa2.zig deleted file mode 100644 index d00581c..0000000 --- a/zig-lib_no-sugar/test-gpa2.zig +++ /dev/null @@ -1,39 +0,0 @@ -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.?); -}