From 51cad2c07d475b87c82cffef1e990673b272ae93 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 21 Jan 2023 19:06:44 +0100 Subject: [PATCH] API change: ipc_context_deinit now frees context memory. --- zig-impl/libipc.h | 2 +- zig-impl/src/bindings.zig | 6 ++++-- zig-impl/test-bindings/pong.c | 29 +++++++++-------------------- zig-impl/test-bindings/pongd.c | 4 +--- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/zig-impl/libipc.h b/zig-impl/libipc.h index 717fb24..59358c9 100644 --- a/zig-impl/libipc.h +++ b/zig-impl/libipc.h @@ -26,7 +26,7 @@ enum cb_event_types { int ipc_context_init (void** ptr); int ipc_service_init (void* ctx, int* servicefd, const char* service_name, uint16_t service_name_len); int ipc_connect_service (void* ctx, int* servicefd, const char* service_name, uint16_t service_name_len); -void ipc_context_deinit (void* ctx); +void ipc_context_deinit (void** ctx); int ipc_write (void* ctx, int servicefd, char* mcontent, uint32_t mlen); int ipc_schedule (void* ctx, int servicefd, const char* mcontent, uint32_t mlen); int ipc_read_fd (void* ctx, int fd, char* buffer, size_t* buflen); diff --git a/zig-impl/src/bindings.zig b/zig-impl/src/bindings.zig index b68d178..2ce2700 100644 --- a/zig-impl/src/bindings.zig +++ b/zig-impl/src/bindings.zig @@ -29,8 +29,10 @@ export fn ipc_connect_service (ctx: *Context, servicefd: *i32, service_name: [*] return 0; } -export fn ipc_context_deinit (ctx: *Context) callconv(.C) void { - ctx.deinit(); +export fn ipc_context_deinit (ctx: **Context) callconv(.C) void { + var ptr: *Context = ctx.*; + ptr.deinit(); + std.heap.c_allocator.destroy(ptr); } /// Write a message (no waiting). diff --git a/zig-impl/test-bindings/pong.c b/zig-impl/test-bindings/pong.c index 130cc89..f075c21 100644 --- a/zig-impl/test-bindings/pong.c +++ b/zig-impl/test-bindings/pong.c @@ -36,8 +36,7 @@ int direct_write_then_read(void) { if (ret != 0) { printf ("Cannot connect to a service.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -47,8 +46,7 @@ int direct_write_then_read(void) { if (ret != 0) { printf ("Cannot write to the service.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -60,16 +58,14 @@ int direct_write_then_read(void) { if (ret != 0) { printf ("Cannot read from the service fd: %d.\n", ret); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } if (size == 0) { printf ("No message returned.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -77,9 +73,7 @@ int direct_write_then_read(void) { printf ("Response: %s.\n", message); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - - free(ctx); + ipc_context_deinit (&ctx); printf ("Context freed.\n"); return 0; @@ -102,8 +96,7 @@ int wait_event(void) { if (ret != 0) { printf ("Cannot init context.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -113,8 +106,7 @@ int wait_event(void) { if (ret != 0) { printf ("Cannot connect to a service.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -124,8 +116,7 @@ int wait_event(void) { if (ret != 0) { printf ("Cannot schedule a message.\n"); printf ("Deinit context\n"); - ipc_context_deinit (ctx); - free(ctx); + ipc_context_deinit (&ctx); return 1; } @@ -194,9 +185,7 @@ int wait_event(void) { } printf ("Deinit context\n"); - ipc_context_deinit (ctx); - - free(ctx); + ipc_context_deinit (&ctx); printf ("Context freed.\n"); return 0; diff --git a/zig-impl/test-bindings/pongd.c b/zig-impl/test-bindings/pongd.c index 29ec64a..53126c7 100644 --- a/zig-impl/test-bindings/pongd.c +++ b/zig-impl/test-bindings/pongd.c @@ -121,9 +121,7 @@ int main(int argc, char**argv) { } printf ("Deinit context\n"); - ipc_context_deinit (ctx); - - free(ctx); + ipc_context_deinit (&ctx); printf ("Context freed.\n"); return 0;