Binding example: works as expected (with c_allocator for now).
parent
8ea70b51ee
commit
c5e3b7b901
|
@ -30,3 +30,9 @@ zigcompilation: build.zig src/*.zig
|
||||||
compilation: src/main.c
|
compilation: src/main.c
|
||||||
@echo the following compilation will produce errors despite actually working
|
@echo the following compilation will produce errors despite actually working
|
||||||
$(CC) -o app -o main build/libipc.so $(CFLAGS) $^ $(LDFLAGS)
|
$(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
|
||||||
|
|
|
@ -1,13 +1,24 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int ret = someipc(20,10);
|
int ret = 0;
|
||||||
printf("hello %d\n", ret);
|
|
||||||
|
|
||||||
void *somestructure = NULL;
|
printf ("Init context.\n");
|
||||||
some_struct_bidouillage_init(&somestructure);
|
void *ctx = NULL;
|
||||||
int value = some_struct_bidouillage_update(&somestructure);
|
ret = ipc_context_init (&ctx);
|
||||||
printf("value %d\n", value);
|
|
||||||
|
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +1,34 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const fmt = std.fmt;
|
||||||
|
const heap = std.heap;
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
const print = std.debug.print;
|
const print = std.debug.print;
|
||||||
|
|
||||||
const SOMESTRUCT = packed struct {
|
pub const Context = struct {
|
||||||
somevalue: i32,
|
rundir: [] u8,
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
fn update(self: *Self) callconv(.C) i32 {
|
pub fn init() !Self {
|
||||||
self.somevalue += 1;
|
var rundir = try std.heap.c_allocator.dupeZ(u8, "/tmp/libipc-run/");
|
||||||
return self.somevalue;
|
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 {
|
export fn ipc_context_init (ptr: **Context) callconv(.C) i32 {
|
||||||
var pointer = @ptrCast(*SOMESTRUCT, @alignCast(@alignOf(SOMESTRUCT),ptr));
|
ptr.* = std.heap.c_allocator.create(Context) catch return 1;
|
||||||
var somestruct = std.heap.c_allocator.create(SOMESTRUCT) catch null;
|
|
||||||
|
|
||||||
print ("hello we just did something\n", .{});
|
ptr.*.* = Context.init() catch |err| {
|
||||||
if (somestruct) |s| {
|
print ("libipc: error while init context: {}\n", .{err});
|
||||||
s.somevalue = 2;
|
return 1;
|
||||||
print ("just changed a value\n", .{});
|
};
|
||||||
pointer.* = s.*;
|
return 0;
|
||||||
}
|
|
||||||
print ("hello again\n", .{});
|
|
||||||
// else {
|
|
||||||
// pointer.* = null;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 {
|
export fn ipc_context_deinit (ctx: *Context) callconv(.C) void {
|
||||||
return s.update();
|
ctx.deinit();
|
||||||
}
|
|
||||||
|
|
||||||
export fn someipc(a: i32, b: i32) callconv(.C) i32 {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
test "basic add functionality" {
|
|
||||||
try testing.expect(someipc(3, 7) == 10);
|
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue