~gpanders/wk

8f249886d374dc1080dc2ebb3970e072a233f6ed — Greg Anders 7 months ago c228fff
Don't free memory allocated by arena allocator

Memory allocated by the arena allocator doesn't need to be freed (in
fact, the 'free' function of the ArenaAllocator doesn't actually do
anything except set the memory to 'undefined') and since zet is only a
short lived command line tool, there's no worry about memory leaks.
M src/cmd.zig => src/cmd.zig +0 -4
@@ 90,7 90,6 @@ pub fn handleCommand(allocator: *mem.Allocator, command: []const u8, args: ?[][]
    }

    var zettels = try getZettels(allocator);
    defer zettels.deinit();

    switch (try parseCommand(command)) {
        .Backlinks => {


@@ 161,7 160,6 @@ pub fn handleCommand(allocator: *mem.Allocator, command: []const u8, args: ?[][]

fn commitChanges(allocator: *mem.Allocator, cmd: Command, zettels: []const Zettel) !void {
    var modified = try std.ArrayList(Zettel).initCapacity(allocator, zettels.len);
    defer modified.deinit();

    for (zettels) |*zet| if (try zet.modified()) modified.appendAssumeCapacity(zet.*);



@@ 179,7 177,6 @@ fn commitChanges(allocator: *mem.Allocator, cmd: Command, zettels: []const Zette
            ),
            .Open => blk: {
                var msg_lines = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len + 1);
                defer msg_lines.deinit();

                var line = try fmt.allocPrint(allocator, "Updated {} note(s):\n", .{modified.items.len});
                msg_lines.appendAssumeCapacity(line);


@@ 194,7 191,6 @@ fn commitChanges(allocator: *mem.Allocator, cmd: Command, zettels: []const Zette
        };

        var files = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len);
        defer files.deinit();

        for (modified.items) |item| files.appendAssumeCapacity(item.fname);


M src/cmd/backlinks.zig => src/cmd/backlinks.zig +0 -5
@@ 16,7 16,6 @@ const link_template = "- [[{}]] {}";
pub fn run(allocator: *mem.Allocator, all_zettels: []const Zettel, zettels: []const Zettel) !void {
    // Map from zettel ID to list of IDs that link to this zettel
    var links = std.StringHashMap(std.ArrayList([]const u8)).init(allocator);
    defer links.deinit();

    // Search each zettel in the given list for links to other zettels
    for (zettels) |zet| {


@@ 25,7 24,6 @@ pub fn run(allocator: *mem.Allocator, all_zettels: []const Zettel, zettels: []co

        const size = try file.getEndPos();
        const contents = try file.inStream().readAllAlloc(allocator, size);
        defer allocator.free(contents);

        // Ignore any links beneath a preexisting backlinks header
        const end_index = mem.indexOf(u8, contents, section_header) orelse size;


@@ 76,7 74,6 @@ pub fn run(allocator: *mem.Allocator, all_zettels: []const Zettel, zettels: []co

        const size = try file.getEndPos();
        const contents = try file.inStream().readAllAlloc(allocator, size);
        defer allocator.free(contents);

        // Find the index of the end of the YAML metadata block, since that
        // interferes with detection of horizontal rules ("---") in the actual


@@ 103,7 100,6 @@ pub fn run(allocator: *mem.Allocator, all_zettels: []const Zettel, zettels: []co
        const start_index = if (mem.indexOf(u8, contents, section_header)) |s| s - 1 else end_index;

        const backlinks = try mem.join(allocator, "\n", links_list.items);
        defer allocator.free(backlinks);

        const new_contents = try fmt.allocPrint(allocator, "{}\n\n{}\n\n{}\n{}", .{
            contents[0 .. start_index - 1],


@@ 111,7 107,6 @@ pub fn run(allocator: *mem.Allocator, all_zettels: []const Zettel, zettels: []co
            backlinks,
            contents[end_index..],
        });
        defer allocator.free(new_contents);

        if (!mem.eql(u8, new_contents, zet.contents)) {
            try file.seekTo(0);

M src/cmd/preview.zig => src/cmd/preview.zig +1 -6
@@ 35,12 35,7 @@ pub fn run(allocator: *mem.Allocator, zettels: []const Zettel) !void {
    };

    var output_files = std.ArrayList([]const u8).init(allocator);
    defer {
        for (output_files.items) |file| {
            fs.cwd().deleteFile(file) catch {};
        }
        output_files.deinit();
    }
    defer for (output_files.items) |file| fs.cwd().deleteFile(file) catch {};

    for (zettels) |zet| {
        var html_file = try fmt.allocPrint(allocator, "{}{}", .{ zet.basename, ".html" });

M src/cmd/show.zig => src/cmd/show.zig +0 -1
@@ 15,7 15,6 @@ pub fn run(allocator: *mem.Allocator, zettels: []const Zettel) !void {

        const size = try file.getEndPos();
        const contents = try file.inStream().readAllAlloc(allocator, size);
        defer allocator.free(contents);

        try stdout.print("{}", .{contents});
    }

M src/cmd/tags.zig => src/cmd/tags.zig +0 -1
@@ 23,7 23,6 @@ pub fn run(allocator: *mem.Allocator, tags: ?[][]const u8, zettels: []const Zett
    } else {
        // List tags
        var existing_tags = std.ArrayList([]const u8).init(allocator);
        defer existing_tags.deinit();

        for (zettels) |zet| {
            try existing_tags.appendSlice(zet.tags);

M src/main.zig => src/main.zig +0 -4
@@ 29,8 29,6 @@ pub fn main() anyerror!void {
    else
        unreachable; // $HOME should always be defined

    defer allocator.free(dir);

    try fs.cwd().makePath(dir);
    try std.process.changeCurDir(dir);



@@ 63,10 61,8 @@ fn readFromStdin(allocator: *mem.Allocator) !?[][]const u8 {
    }

    const input = try std.io.getStdIn().inStream().readAllAlloc(allocator, 1 * 1024 * 1024);
    defer allocator.free(input);

    var args = std.ArrayList([]const u8).init(allocator);
    defer args.deinit();

    var it = mem.split(input, "\n");
    while (it.next()) |line| {

M src/util.zig => src/util.zig +0 -1
@@ 60,7 60,6 @@ pub fn commit(allocator: *mem.Allocator, files: []const []const u8, msg: []const
    fs.cwd().access(".git", .{}) catch return;

    var add_args = std.ArrayList([]const u8).init(allocator);
    defer add_args.deinit();

    try add_args.appendSlice(&[_][]const u8{ "git", "add" });
    try add_args.appendSlice(files);

M src/zettel.zig => src/zettel.zig +0 -12
@@ 25,13 25,8 @@ pub const Zettel = struct {
    /// Create a new Zettel with the given title.
    pub fn new(allocator: *mem.Allocator, title: []const u8) !Zettel {
        const date = try strftime(allocator, "%B %d, %Y");
        defer allocator.free(date);

        const id = try strftime(allocator, "%Y%m%d%H%M%S");
        defer allocator.free(id);

        var fname = try fmt.allocPrint(allocator, "{}-{}" ++ EXT, .{ id, title });
        errdefer allocator.free(fname);

        for (fname) |*char| {
            if (ascii.isSpace(char.*)) {


@@ 46,7 41,6 @@ pub const Zettel = struct {
        defer file.close();

        const contents = try fmt.allocPrint(allocator, TEMPLATE, .{ title, date });
        defer allocator.free(contents);
        try file.writeAll(contents);

        var zettel = Zettel{


@@ 76,7 70,6 @@ pub const Zettel = struct {
        }

        const contents = try file.inStream().readAllAlloc(self.allocator, 1 * 1024 * 1024);
        defer self.allocator.free(contents);

        if (!mem.eql(u8, self.contents, contents)) {
            return true;


@@ 88,7 81,6 @@ pub const Zettel = struct {

pub fn getZettels(allocator: *mem.Allocator) !std.ArrayList(Zettel) {
    var zettels = std.ArrayList(Zettel).init(allocator);
    errdefer zettels.deinit();

    var dir = try fs.cwd().openDir(".", .{ .iterate = true });
    defer dir.close();


@@ 118,7 110,6 @@ pub fn getZettels(allocator: *mem.Allocator) !std.ArrayList(Zettel) {

pub fn findZettels(allocator: *mem.Allocator, zettels: []const Zettel, keywords: [][]const u8) ![]const Zettel {
    var found = std.ArrayList(Zettel).init(allocator);
    defer found.deinit();

    for (keywords) |keyword| {
        if (findZettel(zettels, keyword)) |zet| {


@@ 201,7 192,6 @@ fn fromEntry(allocator: *mem.Allocator, entry: fs.Dir.Entry) !Zettel {
    defer file.close();

    const contents = try file.inStream().readAllAlloc(allocator, 1 * 1024 * 1024);
    errdefer allocator.free(contents);

    var line_it = mem.split(contents, "\n");
    if (line_it.next()) |line| {


@@ 216,7 206,6 @@ fn fromEntry(allocator: *mem.Allocator, entry: fs.Dir.Entry) !Zettel {
    var title: ?[]const u8 = null;

    var tags = std.ArrayList([]const u8).init(allocator);
    defer tags.deinit();

    outer: while (line_it.next()) |line| {
        if (mem.eql(u8, line, "---") or mem.eql(u8, line, "...")) break;


@@ 259,7 248,6 @@ fn fromEntry(allocator: *mem.Allocator, entry: fs.Dir.Entry) !Zettel {
    _ = title orelse return error.InvalidFormat;

    const fname = try mem.dupe(allocator, u8, entry.name);
    errdefer allocator.free(fname);

    var zettel = Zettel{
        .allocator = allocator,