authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 03:27:36 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 03:27:36 -07:00
loge0e95bedc45c574fef385e7f6a7a2253d1614108
treefc8c3e19c735fefa643dc49cd5f69e3b670b3694
parent0dacc15ec5f7780721bc6668dc81578706ad0d8c

phase out more usage of global allocator


5 files changed, 26 insertions(+), 20 deletions(-)

src/common.zig+7-2
......@@ -45,6 +45,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
4545 }
4646 }
4747 return zigmod.Module{
48 .alloc = gpa,
4849 .is_sys_lib = false,
4950 .id = "root",
5051 .name = "root",
......@@ -70,6 +71,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
7071 }
7172 }
7273 return zigmod.Module{
74 .alloc = gpa,
7375 .is_sys_lib = false,
7476 .id = m.id,
7577 .name = m.name,
......@@ -218,6 +220,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
218220 switch (d.type) {
219221 .system_lib => {
220222 return zigmod.Module{
223 .alloc = gpa,
221224 .is_sys_lib = true,
222225 .id = try u.do_hash(std.crypto.hash.sha3.Sha3_384, d.path),
223226 .name = d.path,
......@@ -234,7 +237,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
234237 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {
235238 error.FileNotFound => {
236239 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
237 var mod_from = try zigmod.Module.from(d.*, cachepath, options);
240 var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options);
238241 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
239242 if (mod_from.is_for_this()) return mod_from;
240243 return null;
......@@ -248,7 +251,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
248251 if (trymain) |_| {
249252 d.*.name = tryname;
250253 d.*.main = trymain.?;
251 var mod_from = try zigmod.Module.from(d.*, cachepath, options);
254 var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options);
252255 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
253256 if (mod_from.is_for_this()) return mod_from;
254257 return null;
......@@ -327,6 +330,7 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin
327330 );
328331
329332 var d: zigmod.Dep = .{
333 .alloc = gpa,
330334 .type = .local,
331335 .path = "files",
332336 .id = "",
......@@ -368,6 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {
368372 2 => {
369373 var iter = std.mem.split(u8, line, " ");
370374 const asdep = zigmod.Dep{
375 .alloc = gpa,
371376 .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?,
372377 .path = iter.next().?,
373378 .version = iter.next().?,
src/util/dep.zig+6-7
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43const builtin = std.builtin;
54
65const zigmod = @import("../lib.zig");
......@@ -13,9 +12,9 @@ const yaml = @import("./yaml.zig");
1312pub const Dep = struct {
1413 const Self = @This();
1514
15 alloc: *std.mem.Allocator,
1616 type: zigmod.DepType,
1717 path: string,
18
1918 id: string,
2019 name: string,
2120 main: string,
......@@ -37,16 +36,16 @@ pub const Dep = struct {
3736 p = u.trim_prefix(p, "https://");
3837 p = u.trim_prefix(p, "git://");
3938 p = u.trim_suffix(p, ".git");
40 p = try std.mem.join(gpa, "/", &.{ @tagName(self.type), p });
39 p = try std.mem.join(self.alloc, "/", &.{ @tagName(self.type), p });
4140 return p;
4241 }
4342
4443 pub fn clean_path_v(self: Dep) !string {
4544 if (self.type == .http and self.version.len > 0) {
4645 const i = std.mem.indexOf(u8, self.version, "-").?;
47 return std.mem.join(gpa, "/", &.{ "v", try self.clean_path(), self.version[i + 1 .. 15] });
46 return std.mem.join(self.alloc, "/", &.{ "v", try self.clean_path(), self.version[i + 1 .. 15] });
4847 }
49 return std.mem.join(gpa, "/", &.{ "v", try self.clean_path(), self.version });
48 return std.mem.join(self.alloc, "/", &.{ "v", try self.clean_path(), self.version });
5049 }
5150
5251 pub fn is_for_this(self: Dep) bool {
......@@ -62,13 +61,13 @@ pub const Dep = struct {
6261
6362 pub fn exact_version(self: Dep, dpath: string) !string {
6463 if (self.version.len == 0) {
65 return try self.type.exact_version(dpath);
64 return try self.type.exact_version(self.alloc, dpath);
6665 }
6766 return switch (self.type) {
6867 .git => blk: {
6968 const vers = try u.parse_split(zigmod.DepType.GitVersion, "-").do(self.version);
7069 if (vers.id.frozen()) break :blk self.version;
71 break :blk try self.type.exact_version(dpath);
70 break :blk try self.type.exact_version(self.alloc, dpath);
7271 },
7372 else => self.version,
7473 };
src/util/dep_type.zig+2-3
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43
54const u = @import("./index.zig");
65
......@@ -78,13 +77,13 @@ pub const DepType = enum {
7877 }
7978
8079 // zig fmt: on
81 pub fn exact_version(self: DepType, mpath: string) !string {
80 pub fn exact_version(self: DepType, alloc: *std.mem.Allocator, mpath: string) !string {
8281 var mdir = try std.fs.cwd().openDir(mpath, .{});
8382 defer mdir.close();
8483 return switch (self) {
8584 .local => "",
8685 .system_lib => "",
87 .git => try std.fmt.allocPrint(gpa, "commit-{s}", .{(try u.git_rev_HEAD(gpa, mdir))}),
86 .git => try std.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}),
8887 .hg => "",
8988 .http => "",
9089 };
src/util/modfile.zig+1
......@@ -108,6 +108,7 @@ pub const ModFile = struct {
108108 }
109109
110110 try dep_list.append(zigmod.Dep{
111 .alloc = alloc,
111112 .type = dep_type,
112113 .path = path,
113114 .id = item.mapping.get_string("id"),
src/util/module.zig+10-8
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43const builtin = @import("builtin");
54
65const zigmod = @import("../lib.zig");
......@@ -12,6 +11,7 @@ const common = @import("./../common.zig");
1211//
1312
1413pub const Module = struct {
14 alloc: *std.mem.Allocator,
1515 is_sys_lib: bool,
1616 id: string,
1717 name: string,
......@@ -26,15 +26,17 @@ pub const Module = struct {
2626 clean_path: string,
2727 dep: ?zigmod.Dep,
2828
29 pub fn from(dep: zigmod.Dep, dir: string, options: *common.CollectOptions) !Module {
30 var moddeps = std.ArrayList(Module).init(gpa);
29 pub fn from(alloc: *std.mem.Allocator, dep: zigmod.Dep, dir: string, options: *common.CollectOptions) !Module {
30 var moddeps = std.ArrayList(Module).init(alloc);
3131 defer moddeps.deinit();
32
3233 for (dep.deps) |*d| {
3334 if (try common.get_module_from_dep(d, dir, options)) |founddep| {
3435 try moddeps.append(founddep);
3536 }
3637 }
3738 return Module{
39 .alloc = alloc,
3840 .is_sys_lib = false,
3941 .id = if (dep.id.len > 0) dep.id else try u.random_string(48),
4042 .name = dep.name,
......@@ -56,9 +58,9 @@ pub const Module = struct {
5658 }
5759
5860 pub fn get_hash(self: Module, cdpath: string) !string {
59 const file_list_1 = try u.file_list(try std.mem.concat(gpa, u8, &.{ cdpath, "/", self.clean_path }));
61 const file_list_1 = try u.file_list(try std.mem.concat(self.alloc, u8, &.{ cdpath, "/", self.clean_path }));
6062
61 var file_list_2 = std.ArrayList(string).init(gpa);
63 var file_list_2 = std.ArrayList(string).init(self.alloc);
6264 defer file_list_2.deinit();
6365 for (file_list_1) |item| {
6466 const _a = u.trim_prefix(item, cdpath);
......@@ -76,15 +78,15 @@ pub const Module = struct {
7678
7779 const h = &std.crypto.hash.Blake3.init(.{});
7880 for (file_list_2.items) |item| {
79 const abs_path = try std.fs.path.join(gpa, &.{ cdpath, self.clean_path, item });
81 const abs_path = try std.fs.path.join(self.alloc, &.{ cdpath, self.clean_path, item });
8082 const file = try std.fs.cwd().openFile(abs_path, .{});
8183 defer file.close();
82 const input = try file.reader().readAllAlloc(gpa, u.mb * 100);
84 const input = try file.reader().readAllAlloc(self.alloc, u.mb * 100);
8385 h.update(input);
8486 }
8587 var out: [32]u8 = undefined;
8688 h.final(&out);
87 const hex = try std.fmt.allocPrint(gpa, "blake3-{x}", .{std.fmt.fmtSliceHexLower(out[0..])});
89 const hex = try std.fmt.allocPrint(self.alloc, "blake3-{x}", .{std.fmt.fmtSliceHexLower(out[0..])});
8890 return hex;
8991 }
9092