From 2796b654e54e96db6f51824616eaaef22d0edeba Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 28 Apr 2022 00:08:52 -0300 Subject: [PATCH 1/2] port main() code path to latest zig --- .gitignore | 1 + src/image.zig | 2 +- src/main.zig | 15 +++++++-------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 44f7213..346c11d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ zig-cache/ +zig-out/ *.mp3 *.wav build_runner.zig diff --git a/src/image.zig b/src/image.zig index 073ffc4..45aa6da 100644 --- a/src/image.zig +++ b/src/image.zig @@ -161,7 +161,7 @@ pub const Image = struct { var in_fmt = mkSfInfo(); // clone sndfile var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); - std.testing.expectEqual(self.frames, @intCast(usize, in_fmt.frames)); + std.debug.assert(self.frames == @intCast(usize, in_fmt.frames)); var image = try self.allocator.create(Image); diff --git a/src/main.zig b/src/main.zig index 9045304..ee269c1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -41,7 +41,7 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void { var stdout_file = std.io.getStdOut(); const stdout = &stdout_file.writer(); - const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path")); + const scri_path = (args_it.next() orelse @panic("expected scri path")); errdefer allocator.free(scri_path); defer allocator.free(scri_path); @@ -246,8 +246,7 @@ fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void { var runner = runners.Runner.init(allocator, false); defer runner.deinit(); - const scri_path = try (args_it.next(allocator) orelse @panic("run: expected scri path")); - defer allocator.free(scri_path); + const scri_path = (args_it.next() orelse @panic("run: expected scri path")); var file = try std.fs.cwd().openFile(scri_path, .{}); defer file.close(); @@ -270,18 +269,18 @@ pub fn main() !void { defer { _ = allocator_instance.deinit(); } - const allocator = &allocator_instance.allocator; + const allocator = allocator_instance.allocator(); - var args_it = std.process.args(); + var args_it = try std.process.argsWithAllocator(allocator); + defer args_it.deinit(); _ = args_it.skip(); - const cli_command_opt = args_it.next(allocator); + const cli_command_opt = args_it.next(); if (cli_command_opt == null) { return doHelp(); } - const cli_command = try cli_command_opt.?; - defer allocator.free(cli_command); + const cli_command = cli_command_opt.?; if (std.mem.eql(u8, cli_command, "help")) { return doHelp(); From 6b2ce7e4250719ac4b15158ec289bd3fcf645fb5 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 28 Apr 2022 00:28:07 -0300 Subject: [PATCH 2/2] properly fix memleaks --- src/lang.zig | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 98b1e77..5ded695 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -479,25 +479,24 @@ pub const CommandList = struct { pub fn deinit(self: *Self) void { for (self.list.items) |cmd_ptr| { - //self.list.allocator.destroy(cmd_ptr); inline for (@typeInfo(Command.Tag).Enum.fields) |field| { if (cmd_ptr.tag == @field(Command.Tag, field.name)) { const actual_tag = @field(Command.Tag, field.name); // if we find a match on the tag, we can get the type const typ = Command.tagToType(actual_tag); - _ = typ; - // TODO fix - - //inline for (@typeInfo(typ).Struct.fields) |cmd_field| { - // switch (cmd_field.field_type) { - // []u8, []const u8 => self.list.allocator.destroy(@field(typ, cmd_field.name)), - // else => {}, - // } - //} + const inner_command = cmd_ptr.cast(typ).?; + inline for (@typeInfo(typ).Struct.fields) |cmd_field| { + switch (cmd_field.field_type) { + []u8, []const u8 => self.list.allocator.free(@field(inner_command, cmd_field.name)), + else => {}, + } + } } } + + self.list.allocator.destroy(cmd_ptr); } self.list.deinit(); }