Skip to content

Commit

Permalink
editor: fix potential memory leak
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
slimsag committed Jul 23, 2023
1 parent 835748a commit 095c779
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/editor/Builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ listen_port: u16 = 1717,
subscribers: std.ArrayListUnmanaged(std.net.Stream) = .{},
watch_paths: ?[][]const u8 = null,
mtimes: std.AutoHashMapUnmanaged(std.fs.File.INode, i128) = .{},
@"formated-www/index.html": []const u8 = undefined,
www_index_html: []const u8 = undefined,

const Status = union(enum) {
building,
Expand All @@ -33,7 +33,15 @@ const Status = union(enum) {
compile_error: []const u8,
};

fn deinit(self: *Builder) void {
allocator.free(self.steps);
allocator.free(self.zig_build_args);
if (self.watch_paths) |wp| allocator.free(wp);
}

pub fn run(self: *Builder) !void {
defer self.deinit();

var child = try self.runZigBuild(.Inherit);
switch (try child.wait()) {
.Exited => |code| {
Expand Down Expand Up @@ -62,7 +70,7 @@ pub fn run(self: *Builder) !void {
std.os.exit(1);
}

self.@"formated-www/index.html" = try std.fmt.allocPrint(
self.www_index_html = try std.fmt.allocPrint(
allocator,
@"www/index.html",
.{
Expand Down Expand Up @@ -94,23 +102,20 @@ pub fn run(self: *Builder) !void {
}

fn runZigBuild(self: Builder, stderr_behavior: std.process.Child.StdIo) !std.process.Child {
var args_arena = std.heap.ArenaAllocator.init(allocator);
defer args_arena.deinit();

const args = try self.buildArgs(args_arena.allocator());
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const arena_allocator = arena.allocator();

var child = std.process.Child.init(args, allocator);
const args = try self.buildArgs(arena_allocator);
var child = std.process.Child.init(args, arena_allocator);
child.stderr_behavior = stderr_behavior;

try child.spawn();

return child;
}

fn buildArgs(self: Builder, arena: std.mem.Allocator) ![]const []const u8 {
var argv = std.ArrayList([]const u8).init(arena);
try argv.ensureTotalCapacity(self.steps.len + self.zig_build_args.len + 6);

argv.appendAssumeCapacity(try arena.dupe(u8, self.zig_path));
argv.appendAssumeCapacity("build");

Expand All @@ -122,7 +127,7 @@ fn buildArgs(self: Builder, arena: std.mem.Allocator) ![]const []const u8 {
argv.appendAssumeCapacity("on");
argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-Doptimize={s}", .{@tagName(self.optimize)}));
if (self.target) |target| {
argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-Dtarget={s}", .{try target.toZigTriple()}));
argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-Dtarget={s}", .{try target.toZigTriple(arena)}));
}

for (self.zig_build_args) |arg| {
Expand All @@ -140,7 +145,7 @@ fn watch(self: *Builder) void {
const path_handlers = std.ComptimeStringMap(*const fn (*Builder, std.net.Stream) void, .{
.{ "/", struct {
fn h(self: *Builder, stream: std.net.Stream) void {
sendData(stream, .ok, .close, mime_map.get(".html").?, self.@"formated-www/index.html");
sendData(stream, .ok, .close, mime_map.get(".html").?, self.www_index_html);
}
}.h },
.{ "/notify", struct {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn main() !void {

if (std.mem.eql(u8, args[arg_i], "build")) {
arg_i += 1;

var builder = Builder{};
var steps = std.ArrayList([]const u8).init(allocator);
var build_args = std.ArrayList([]const u8).init(allocator);
Expand Down Expand Up @@ -102,7 +103,6 @@ pub fn main() !void {

builder.steps = try steps.toOwnedSlice();
builder.zig_build_args = try build_args.toOwnedSlice();

return builder.run();
} else if (std.mem.eql(u8, args[arg_i], "help") or std.mem.eql(u8, args[arg_i], "--help") or std.mem.eql(u8, args[arg_i], "-h")) {
arg_i += 1;
Expand Down
4 changes: 2 additions & 2 deletions src/editor/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const Target = enum {
std.meta.stringToEnum(Target, str) orelse return null;
}

pub fn toZigTriple(self: Target) ![]const u8 {
pub fn toZigTriple(self: Target, allocator2: std.mem.Allocator) ![]const u8 {
const zig_target = std.zig.CrossTarget{
.cpu_arch = switch (self) {
.@"linux-x86_64",
Expand All @@ -47,6 +47,6 @@ pub const Target = enum {
.wasm32 => .freestanding,
},
};
return zig_target.zigTriple(allocator);
return zig_target.zigTriple(allocator2);
}
};

0 comments on commit 095c779

Please sign in to comment.