aoc2023

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4700fa924ab82ca2e41daa627a59f691d8177d5b
parent ecfca750dff292d5083465a2ca5b6106649b92af
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Sun,  3 Dec 2023 22:09:16 +0000

Fix the annoying freeze on .free
It was caused by duplicating GeneralPurposeAllocator,
by using it as a struct value. It has a mutex in it, so don't do that.

Diffstat:
Mcommon.zig | 11+++--------
Mday1.zig | 4+++-
Mday2.zig | 4+++-
3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/common.zig b/common.zig @@ -2,16 +2,13 @@ const std = @import("std"); pub const Setup = struct { pub const Part = enum { pt1, pt2 }; - - gpa: std.heap.GeneralPurposeAllocator(.{}), a: std.mem.Allocator, input: []const u8, part: Part, - pub fn get() !Setup { + pub fn get(a: std.mem.Allocator) !Setup { var res: Setup = undefined; - res.gpa = std.heap.GeneralPurposeAllocator(.{}){}; - res.a = res.gpa.allocator(); + res.a = a; var p = std.process.args(); var file: []const u8 = ""; if (!p.skip()) return error.NoProgramName; @@ -39,8 +36,6 @@ pub const Setup = struct { return res; } pub fn deinit(self: *Setup) void { - _ = self; - //self.a.free(self.input); // For some reason this is freezing - //_ = self.gpa.deinit(); // and if we don't free, then gpa.deinit whinges that we leaked memory :) + self.a.free(self.input); // For some reason this is freezing } }; diff --git a/day1.zig b/day1.zig @@ -49,7 +49,9 @@ const w2d = std.comptime_string_map.ComptimeStringMap(u8, .{ }); pub fn main() !void { - var s = try Setup.get(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + var s = try Setup.get(gpa.allocator()); defer s.deinit(); var l = std.mem.splitScalar(u8, s.input, '\n'); var t: u64 = 0; diff --git a/day2.zig b/day2.zig @@ -16,7 +16,9 @@ const Colour = enum { }; pub fn main() !void { - var setup = try Setup.get(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + var setup = try Setup.get(gpa.allocator()); defer setup.deinit(); var sum: u32 = 0; var lines = std.mem.splitScalar(u8, setup.input, '\n');