WIP: libipc compiles with Zig 0.15.2. TODO: tests.

This commit is contained in:
Philippe Pittoli 2025-10-17 14:16:12 +02:00
parent 11a98afe94
commit 354efc8b1a
3 changed files with 31 additions and 31 deletions

View file

@ -74,10 +74,10 @@ pub const Context = struct {
}; };
return Self{ .rundir = rundir return Self{ .rundir = rundir
, .connections = Connections.init(allocator) , .connections = Connections{}
, .pollfd = PollFD.init(allocator) , .pollfd = PollFD{}
, .tx = Messages.init(allocator) , .tx = Messages{}
, .switchdb = SwitchDB.init(allocator) , .switchdb = SwitchDB.init(allocator)
, .allocator = allocator }; , .allocator = allocator };
} }
@ -88,12 +88,12 @@ pub const Context = struct {
}, },
}; };
self.allocator.free(self.rundir); self.allocator.free(self.rundir);
self.connections.deinit(); self.connections.deinit(self.allocator);
self.pollfd.deinit(); self.pollfd.deinit(self.allocator);
for (self.tx.items) |m| { for (self.tx.items) |m| {
m.deinit(); m.deinit();
} }
self.tx.deinit(); self.tx.deinit(self.allocator);
self.switchdb.deinit(); self.switchdb.deinit();
} }
@ -173,8 +173,8 @@ pub const Context = struct {
/// in case one of the arrays cannot sustain another entry, the other /// in case one of the arrays cannot sustain another entry, the other
/// won't be added. /// won't be added.
fn add_(self: *Self, new_connection: Connection, fd: posix.socket_t) !void { fn add_(self: *Self, new_connection: Connection, fd: posix.socket_t) !void {
try self.connections.append(new_connection); try self.connections.append(self.allocator, new_connection);
try self.pollfd.append(.{ .fd = fd, .events = std.os.linux.POLL.IN, .revents = 0 }); try self.pollfd.append(self.allocator, .{ .fd = fd, .events = std.os.linux.POLL.IN, .revents = 0 });
} }
fn fd_to_index(self: Self, fd: i32) !usize { fn fd_to_index(self: Self, fd: i32) !usize {
@ -253,7 +253,7 @@ pub const Context = struct {
// While the program is running, the lock is enabled. // While the program is running, the lock is enabled.
// Once the program stops (even if it crashes), the lock is then disabled. // Once the program stops (even if it crashes), the lock is then disabled.
// Quit if the lock is still active. // Quit if the lock is still active.
const lock_opts = .{ .lock = .exclusive, .lock_nonblocking = true }; const lock_opts : std.fs.File.CreateFlags = .{ .lock = .exclusive, .lock_nonblocking = true };
_ = std.fs.createFileAbsolute(lock, lock_opts) catch |err| { _ = std.fs.createFileAbsolute(lock, lock_opts) catch |err| {
log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{ path, lock, err }); log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{ path, lock, err });
log.err("you may have lauched the service twice.", .{}); log.err("you may have lauched the service twice.", .{});
@ -298,7 +298,7 @@ pub const Context = struct {
} }
pub fn schedule(self: *Self, m: Message) !void { pub fn schedule(self: *Self, m: Message) !void {
try self.tx.append(m); try self.tx.append(self.allocator, m);
} }
/// Read from a client (indexed by a FD). /// Read from a client (indexed by a FD).
@ -317,8 +317,8 @@ pub const Context = struct {
} }
pub fn set_switch_callbacks(self: *Self, fd: i32, pub fn set_switch_callbacks(self: *Self, fd: i32,
in: ?*const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8, in: ?*const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.c) u8,
out: ?*const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) !void { out: ?*const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.c) u8) !void {
try self.switchdb.set_callbacks(fd, in, out); try self.switchdb.set_callbacks(fd, in, out);
} }

View file

@ -1,17 +1,17 @@
pub const CBEvent = @import("./callback.zig").CBEvent; // pub const CBEvent = @import("./callback.zig").CBEvent;
pub const Connection = @import("./connection.zig").Connection; // pub const Connection = @import("./connection.zig").Connection;
pub const Message = @import("./message.zig").Message; // pub const Message = @import("./message.zig").Message;
pub const Event = @import("./event.zig").Event; // pub const Event = @import("./event.zig").Event;
pub const Switch = @import("./switch.zig").Switch; // pub const Switch = @import("./switch.zig").Switch;
//
pub const Messages = @import("./message.zig").Messages; // pub const Messages = @import("./message.zig").Messages;
pub const Switches = @import("./switch.zig").Switches; // pub const Switches = @import("./switch.zig").Switches;
pub const Connections = @import("./connection.zig").Connections; // pub const Connections = @import("./connection.zig").Connections;
pub const Context = @import("./context.zig").Context; // pub const Context = @import("./context.zig").Context;
//
pub const util = @import("./util.zig"); // pub const util = @import("./util.zig");
pub const hexdump = @import("./hexdump.zig"); // pub const hexdump = @import("./hexdump.zig");
pub const exchangefd = @import("./exchange-fd.zig"); // pub const exchangefd = @import("./exchange-fd.zig");
// PING source code // PING source code
const std = @import("std"); const std = @import("std");

View file

@ -191,7 +191,7 @@ const ManagedConnection = struct {
test "creation and display" { test "creation and display" {
const config = .{ .safety = true }; const config = .{ .safety = true };
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.DebugAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
@ -221,7 +221,7 @@ fn successful_out(_: i32, _: [*]const u8, _: usize) CBEventType {
test "successful exchanges" { test "successful exchanges" {
const config = .{ .safety = true }; const config = .{ .safety = true };
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.DebugAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
@ -264,7 +264,7 @@ fn unsuccessful_out(_: i32, _: [*]const u8, _: usize) CBEventType {
test "unsuccessful exchanges" { test "unsuccessful exchanges" {
const config = .{ .safety = true }; const config = .{ .safety = true };
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.DebugAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
@ -295,7 +295,7 @@ test "unsuccessful exchanges" {
test "nuke 'em" { test "nuke 'em" {
const config = .{ .safety = true }; const config = .{ .safety = true };
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.DebugAllocator(config){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();