Keep track of the Zig GPA test.

This commit is contained in:
Philippe Pittoli 2023-04-29 01:38:56 +02:00
parent 7bb90ff3de
commit ffa7f943d7
5 changed files with 134 additions and 0 deletions

21
drop/app.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./ipc.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
drop/ipc.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef LIBIPC
#define LIBIPC
#include <stdint.h>
void* ipc_context_init (void);
void ipc_context_deinit (void* ctx);
#endif

45
drop/ipc.zig Normal file
View File

@ -0,0 +1,45 @@
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.?);
}

20
drop/makefile Normal file
View File

@ -0,0 +1,20 @@
# No sugar for you!
build: lib
gcc -Os -I. -static -o app app.c libipc.a
lib:
zig build-lib ipc.zig -OReleaseSmall
run:
$(VALGRIND) ./app
include ../mk/makefile.valgrind
# Another library.
lib-alt:
zig build-lib test-gpa2.zig -OReleaseSmall
clean:
rm app *.a *.o

39
drop/test-gpa2.zig Normal file
View File

@ -0,0 +1,39 @@
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.?);
}