Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
make a thread safe arena allocator and fix project file serialization (
Browse files Browse the repository at this point in the history
…#142)

* fix thread safe arena allocator issue and project file serialization

* thread safe arena allocator

* fixup search and replace
  • Loading branch information
mattnite authored Nov 19, 2021
1 parent 54af3ee commit be7846c
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 146 deletions.
57 changes: 29 additions & 28 deletions src/Dependency.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const zzz = @import("zzz");
const uri = @import("uri");
const api = @import("api.zig");
const utils = @import("utils.zig");
const ThreadSafeArenaAllocator = @import("ThreadSafeArenaAllocator.zig");

const Self = @This();
const Allocator = std.mem.Allocator;
Expand Down Expand Up @@ -110,7 +111,7 @@ pub const Source = union(enum) {
/// url: <url>
/// root: <root file>
/// ```
pub fn fromZNode(arena: *std.heap.ArenaAllocator, node: *zzz.ZNode) !Self {
pub fn fromZNode(arena: *ThreadSafeArenaAllocator, node: *zzz.ZNode) !Self {
const allocator = arena.child_allocator;
if (node.*.child == null) return error.NoChildren;

Expand Down Expand Up @@ -240,7 +241,7 @@ pub fn fromZNode(arena: *std.heap.ArenaAllocator, node: *zzz.ZNode) !Self {
}

/// for testing
fn fromString(arena: *std.heap.ArenaAllocator, str: []const u8) !Self {
fn fromString(arena: *ThreadSafeArenaAllocator, str: []const u8) !Self {
var tree = zzz.ZTree(1, 1000){};
const root = try tree.appendText(str);
return Self.fromZNode(arena, root.*.child.?);
Expand Down Expand Up @@ -289,7 +290,7 @@ fn expectDepEqual(expected: Self, actual: Self) !void {
}

test "default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

try expectDepEqual(Self{
Expand All @@ -306,7 +307,7 @@ test "default repo pkg" {
}

test "legacy aliased, default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -334,7 +335,7 @@ test "legacy aliased, default repo pkg" {
}

test "aliased, default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -361,7 +362,7 @@ test "aliased, default repo pkg" {
}

test "legacy non-default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -389,7 +390,7 @@ test "legacy non-default repo pkg" {
}

test "non-default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -416,7 +417,7 @@ test "non-default repo pkg" {
}

test "legacy aliased, non-default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -445,7 +446,7 @@ test "legacy aliased, non-default repo pkg" {
}

test "aliased, non-default repo pkg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -473,7 +474,7 @@ test "aliased, non-default repo pkg" {
}

test "legacy github default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -500,7 +501,7 @@ test "legacy github default root" {
}

test "github default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -526,7 +527,7 @@ test "github default root" {
}

test "legacy github explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -554,7 +555,7 @@ test "legacy github explicit root" {
}

test "legacy github explicit root, incorrect indent" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -582,7 +583,7 @@ test "legacy github explicit root, incorrect indent" {
}

test "legacy github explicit root, mixed with newer root indent" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -610,7 +611,7 @@ test "legacy github explicit root, mixed with newer root indent" {
}

test "github explicit root, incorrect indent" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -637,7 +638,7 @@ test "github explicit root, incorrect indent" {
}

test "github explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -664,7 +665,7 @@ test "github explicit root" {
}

test "legacy raw default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -687,7 +688,7 @@ test "legacy raw default root" {
}

test "raw default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -709,7 +710,7 @@ test "raw default root" {
}

test "legacy raw explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -733,7 +734,7 @@ test "legacy raw explicit root" {
}

test "legacy raw explicit root, incorrect indent" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -757,7 +758,7 @@ test "legacy raw explicit root, incorrect indent" {
}

test "raw explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -780,7 +781,7 @@ test "raw explicit root" {
}

test "legacy local with default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -803,7 +804,7 @@ test "legacy local with default root" {
}

test "local with default root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -825,7 +826,7 @@ test "local with default root" {
}

test "legacy local with explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -849,7 +850,7 @@ test "legacy local with explicit root" {
}

test "legacy local with explicit root, incorrect indent" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand All @@ -873,7 +874,7 @@ test "legacy local with explicit root, incorrect indent" {
}

test "local with explicit root" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();

const actual = try fromString(&arena,
Expand Down Expand Up @@ -910,7 +911,7 @@ test "github can't take an integrity " {
/// serializes dependency information back into zzz format
pub fn addToZNode(
self: Self,
arena: *std.heap.ArenaAllocator,
arena: *ThreadSafeArenaAllocator,
tree: *zzz.ZTree(1, 1000),
parent: *zzz.ZNode,
explicit: bool,
Expand Down Expand Up @@ -1004,7 +1005,7 @@ fn expectZzzEqual(expected: *zzz.ZNode, actual: *zzz.ZNode) !void {
}

fn serializeTest(from: []const u8, to: []const u8, explicit: bool) !void {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
var arena = ThreadSafeArenaAllocator.init(testing.allocator);
defer arena.deinit();
const dep = try fromString(&arena, from);
var actual = zzz.ZTree(1, 1000){};
Expand Down
2 changes: 1 addition & 1 deletion src/Display.zig
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn deinit(self: *Self) void {
ansi.render_thread.join();

const stderr = std.io.getStdErr().writer();
if (ansi.logs.items.len > 0) {
if (ansi.logs.items.len > 1) {
stderr.writeByteNTimes('-', ansi.size.cols) catch {};
stderr.writeByte('\n') catch {};
stderr.writeAll("logs captured during fetch:\n") catch {};
Expand Down
Loading

0 comments on commit be7846c

Please sign in to comment.