Commit 58bc713c17

Jakub Konka <kubkon@jakubkonka.com>
2021-07-31 21:37:46
macho: make Trie accept allocator as a param
instead of storing it as a member of Trie struct.
1 parent d19fdf0
Changed files (2)
src
src/link/MachO/Trie.zig
@@ -273,8 +273,6 @@ pub const Node = struct {
 /// The root node of the trie.
 root: ?*Node = null,
 
-allocator: *Allocator,
-
 /// If you want to access nodes ordered in DFS fashion,
 /// you should call `finalize` first since the nodes
 /// in this container are not guaranteed to not be stale
@@ -294,10 +292,6 @@ node_count: usize = 0,
 
 trie_dirty: bool = true,
 
-pub fn init(allocator: *Allocator) Trie {
-    return .{ .allocator = allocator };
-}
-
 /// Export symbol that is to be placed in the trie.
 pub const ExportSymbol = struct {
     /// Name of the symbol.
@@ -314,9 +308,9 @@ pub const ExportSymbol = struct {
 /// Insert a symbol into the trie, updating the prefixes in the process.
 /// This operation may change the layout of the trie by splicing edges in
 /// certain circumstances.
-pub fn put(self: *Trie, symbol: ExportSymbol) !void {
-    try self.createRoot();
-    const node = try self.root.?.put(self.allocator, symbol.name);
+pub fn put(self: *Trie, allocator: *Allocator, symbol: ExportSymbol) !void {
+    try self.createRoot(allocator);
+    const node = try self.root.?.put(allocator, symbol.name);
     node.terminal_info = .{
         .vmaddr_offset = symbol.vmaddr_offset,
         .export_flags = symbol.export_flags,
@@ -328,13 +322,13 @@ pub fn put(self: *Trie, symbol: ExportSymbol) !void {
 /// This step performs multiple passes through the trie ensuring
 /// there are no gaps after every `Node` is ULEB128 encoded.
 /// Call this method before trying to `write` the trie to a byte stream.
-pub fn finalize(self: *Trie) !void {
+pub fn finalize(self: *Trie, allocator: *Allocator) !void {
     if (!self.trie_dirty) return;
 
     self.ordered_nodes.shrinkRetainingCapacity(0);
-    try self.ordered_nodes.ensureCapacity(self.allocator, self.node_count);
+    try self.ordered_nodes.ensureCapacity(allocator, self.node_count);
 
-    var fifo = std.fifo.LinearFifo(*Node, .Dynamic).init(self.allocator);
+    var fifo = std.fifo.LinearFifo(*Node, .Dynamic).init(allocator);
     defer fifo.deinit();
 
     try fifo.writeItem(self.root.?);
@@ -383,17 +377,17 @@ pub fn write(self: Trie, writer: anytype) !u64 {
     return counting_writer.bytes_written;
 }
 
-pub fn deinit(self: *Trie) void {
+pub fn deinit(self: *Trie, allocator: *Allocator) void {
     if (self.root) |root| {
-        root.deinit(self.allocator);
-        self.allocator.destroy(root);
+        root.deinit(allocator);
+        allocator.destroy(root);
     }
-    self.ordered_nodes.deinit(self.allocator);
+    self.ordered_nodes.deinit(allocator);
 }
 
-fn createRoot(self: *Trie) !void {
+fn createRoot(self: *Trie, allocator: *Allocator) !void {
     if (self.root == null) {
-        const root = try self.allocator.create(Node);
+        const root = try allocator.create(Node);
         root.* = .{ .base = self };
         self.root = root;
         self.node_count += 1;
src/link/MachO.zig
@@ -3126,8 +3126,8 @@ fn writeLazyBindInfoTableZld(self: *MachO) !void {
 }
 
 fn writeExportInfoZld(self: *MachO) !void {
-    var trie = Trie.init(self.base.allocator);
-    defer trie.deinit();
+    var trie: Trie = .{};
+    defer trie.deinit(self.base.allocator);
 
     const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
     const base_address = text_segment.inner.vmaddr;
@@ -3139,14 +3139,14 @@ fn writeExportInfoZld(self: *MachO) !void {
         const sym_name = self.getString(sym.n_strx);
         log.debug("  | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });
 
-        try trie.put(.{
+        try trie.put(self.base.allocator, .{
             .name = sym_name,
             .vmaddr_offset = sym.n_value - base_address,
             .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
         });
     }
 
-    try trie.finalize();
+    try trie.finalize(self.base.allocator);
 
     var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
     defer self.base.allocator.free(buffer);
@@ -5269,8 +5269,8 @@ fn writeExportInfo(self: *MachO) !void {
     const tracy = trace(@src());
     defer tracy.end();
 
-    var trie = Trie.init(self.base.allocator);
-    defer trie.deinit();
+    var trie: Trie = .{};
+    defer trie.deinit(self.base.allocator);
 
     const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
     const base_address = text_segment.inner.vmaddr;
@@ -5282,14 +5282,14 @@ fn writeExportInfo(self: *MachO) !void {
         const sym_name = self.getString(sym.n_strx);
         log.debug("  | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });
 
-        try trie.put(.{
+        try trie.put(self.base.allocator, .{
             .name = sym_name,
             .vmaddr_offset = sym.n_value - base_address,
             .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
         });
     }
+    try trie.finalize(self.base.allocator);
 
-    try trie.finalize();
     var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
     defer self.base.allocator.free(buffer);
     var stream = std.io.fixedBufferStream(buffer);