Zig "C" library without sugar: renaming & grooming.
This commit is contained in:
parent
44a3f553bc
commit
ec78813c59
15
zig-lib_no-sugar/app.c
Normal file
15
zig-lib_no-sugar/app.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "./lib.h"
|
||||
|
||||
int main(void) {
|
||||
printf ("Calling ipc_hello\n");
|
||||
ipc_hello ();
|
||||
|
||||
printf ("Calling ipc_bye\n");
|
||||
ipc_bye ();
|
||||
|
||||
return 0;
|
||||
}
|
11
zig-lib_no-sugar/ipc.zig
Normal file
11
zig-lib_no-sugar/ipc.zig
Normal file
@ -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", .{});
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
9
zig-lib_no-sugar/lib.h
Normal file
9
zig-lib_no-sugar/lib.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef LIB
|
||||
#define LIB
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void ipc_hello (void);
|
||||
void ipc_bye (void);
|
||||
|
||||
#endif
|
@ -1,9 +0,0 @@
|
||||
#ifndef LIBIPC
|
||||
#define LIBIPC
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void* ipc_context_init (void);
|
||||
void ipc_context_deinit (void* ctx);
|
||||
|
||||
#endif
|
@ -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
|
||||
|
@ -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.?);
|
||||
}
|
@ -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.?);
|
||||
}
|
Loading…
Reference in New Issue
Block a user