Binding example: works as expected (with c_allocator for now).
This commit is contained in:
parent
8ea70b51ee
commit
c5e3b7b901
@ -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
|
||||
|
@ -1,13 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user