authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-31 21:37:46+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-01 09:06:56+02:00
log58bc713c171e74b3c4f8283636561e6521edbeef
treeefad0804339f522a5b33d12b59398758009003a9
parentd19fdf09ae1686ae5f7eb5f2386dd6de1f441db5

macho: make Trie accept allocator as a param

instead of storing it as a member of Trie struct.

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

src/link/MachO.zig+8-8
...@@ -3126,8 +3126,8 @@ fn writeLazyBindInfoTableZld(self: *MachO) !void {...@@ -3126,8 +3126,8 @@ fn writeLazyBindInfoTableZld(self: *MachO) !void {
3126}3126}
31273127
3128fn writeExportInfoZld(self: *MachO) !void {3128fn writeExportInfoZld(self: *MachO) !void {
3129 var trie = Trie.init(self.base.allocator);3129 var trie: Trie = .{};
3130 defer trie.deinit();3130 defer trie.deinit(self.base.allocator);
31313131
3132 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;3132 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
3133 const base_address = text_segment.inner.vmaddr;3133 const base_address = text_segment.inner.vmaddr;
...@@ -3139,14 +3139,14 @@ fn writeExportInfoZld(self: *MachO) !void {...@@ -3139,14 +3139,14 @@ fn writeExportInfoZld(self: *MachO) !void {
3139 const sym_name = self.getString(sym.n_strx);3139 const sym_name = self.getString(sym.n_strx);
3140 log.debug(" | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });3140 log.debug(" | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });
31413141
3142 try trie.put(.{3142 try trie.put(self.base.allocator, .{
3143 .name = sym_name,3143 .name = sym_name,
3144 .vmaddr_offset = sym.n_value - base_address,3144 .vmaddr_offset = sym.n_value - base_address,
3145 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,3145 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
3146 });3146 });
3147 }3147 }
31483148
3149 try trie.finalize();3149 try trie.finalize(self.base.allocator);
31503150
3151 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));3151 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
3152 defer self.base.allocator.free(buffer);3152 defer self.base.allocator.free(buffer);
...@@ -5269,8 +5269,8 @@ fn writeExportInfo(self: *MachO) !void {...@@ -5269,8 +5269,8 @@ fn writeExportInfo(self: *MachO) !void {
5269 const tracy = trace(@src());5269 const tracy = trace(@src());
5270 defer tracy.end();5270 defer tracy.end();
52715271
5272 var trie = Trie.init(self.base.allocator);5272 var trie: Trie = .{};
5273 defer trie.deinit();5273 defer trie.deinit(self.base.allocator);
52745274
5275 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;5275 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
5276 const base_address = text_segment.inner.vmaddr;5276 const base_address = text_segment.inner.vmaddr;
...@@ -5282,14 +5282,14 @@ fn writeExportInfo(self: *MachO) !void {...@@ -5282,14 +5282,14 @@ fn writeExportInfo(self: *MachO) !void {
5282 const sym_name = self.getString(sym.n_strx);5282 const sym_name = self.getString(sym.n_strx);
5283 log.debug(" | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });5283 log.debug(" | putting '{s}' defined at 0x{x}", .{ sym_name, sym.n_value });
52845284
5285 try trie.put(.{5285 try trie.put(self.base.allocator, .{
5286 .name = sym_name,5286 .name = sym_name,
5287 .vmaddr_offset = sym.n_value - base_address,5287 .vmaddr_offset = sym.n_value - base_address,
5288 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,5288 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
5289 });5289 });
5290 }5290 }
5291 try trie.finalize(self.base.allocator);
52915292
5292 try trie.finalize();
5293 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));5293 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
5294 defer self.base.allocator.free(buffer);5294 defer self.base.allocator.free(buffer);
5295 var stream = std.io.fixedBufferStream(buffer);5295 var stream = std.io.fixedBufferStream(buffer);
src/link/MachO/Trie.zig+12-18
...@@ -273,8 +273,6 @@ pub const Node = struct {...@@ -273,8 +273,6 @@ pub const Node = struct {
273/// The root node of the trie.273/// The root node of the trie.
274root: ?*Node = null,274root: ?*Node = null,
275275
276allocator: *Allocator,
277
278/// If you want to access nodes ordered in DFS fashion,276/// If you want to access nodes ordered in DFS fashion,
279/// you should call `finalize` first since the nodes277/// you should call `finalize` first since the nodes
280/// in this container are not guaranteed to not be stale278/// in this container are not guaranteed to not be stale
...@@ -294,10 +292,6 @@ node_count: usize = 0,...@@ -294,10 +292,6 @@ node_count: usize = 0,
294292
295trie_dirty: bool = true,293trie_dirty: bool = true,
296294
297pub fn init(allocator: *Allocator) Trie {
298 return .{ .allocator = allocator };
299}
300
301/// Export symbol that is to be placed in the trie.295/// Export symbol that is to be placed in the trie.
302pub const ExportSymbol = struct {296pub const ExportSymbol = struct {
303 /// Name of the symbol.297 /// Name of the symbol.
...@@ -314,9 +308,9 @@ pub const ExportSymbol = struct {...@@ -314,9 +308,9 @@ pub const ExportSymbol = struct {
314/// Insert a symbol into the trie, updating the prefixes in the process.308/// Insert a symbol into the trie, updating the prefixes in the process.
315/// This operation may change the layout of the trie by splicing edges in309/// This operation may change the layout of the trie by splicing edges in
316/// certain circumstances.310/// certain circumstances.
317pub fn put(self: *Trie, symbol: ExportSymbol) !void {311pub fn put(self: *Trie, allocator: *Allocator, symbol: ExportSymbol) !void {
318 try self.createRoot();312 try self.createRoot(allocator);
319 const node = try self.root.?.put(self.allocator, symbol.name);313 const node = try self.root.?.put(allocator, symbol.name);
320 node.terminal_info = .{314 node.terminal_info = .{
321 .vmaddr_offset = symbol.vmaddr_offset,315 .vmaddr_offset = symbol.vmaddr_offset,
322 .export_flags = symbol.export_flags,316 .export_flags = symbol.export_flags,
...@@ -328,13 +322,13 @@ pub fn put(self: *Trie, symbol: ExportSymbol) !void {...@@ -328,13 +322,13 @@ pub fn put(self: *Trie, symbol: ExportSymbol) !void {
328/// This step performs multiple passes through the trie ensuring322/// This step performs multiple passes through the trie ensuring
329/// there are no gaps after every `Node` is ULEB128 encoded.323/// there are no gaps after every `Node` is ULEB128 encoded.
330/// Call this method before trying to `write` the trie to a byte stream.324/// Call this method before trying to `write` the trie to a byte stream.
331pub fn finalize(self: *Trie) !void {325pub fn finalize(self: *Trie, allocator: *Allocator) !void {
332 if (!self.trie_dirty) return;326 if (!self.trie_dirty) return;
333327
334 self.ordered_nodes.shrinkRetainingCapacity(0);328 self.ordered_nodes.shrinkRetainingCapacity(0);
335 try self.ordered_nodes.ensureCapacity(self.allocator, self.node_count);329 try self.ordered_nodes.ensureCapacity(allocator, self.node_count);
336330
337 var fifo = std.fifo.LinearFifo(*Node, .Dynamic).init(self.allocator);331 var fifo = std.fifo.LinearFifo(*Node, .Dynamic).init(allocator);
338 defer fifo.deinit();332 defer fifo.deinit();
339333
340 try fifo.writeItem(self.root.?);334 try fifo.writeItem(self.root.?);
...@@ -383,17 +377,17 @@ pub fn write(self: Trie, writer: anytype) !u64 {...@@ -383,17 +377,17 @@ pub fn write(self: Trie, writer: anytype) !u64 {
383 return counting_writer.bytes_written;377 return counting_writer.bytes_written;
384}378}
385379
386pub fn deinit(self: *Trie) void {380pub fn deinit(self: *Trie, allocator: *Allocator) void {
387 if (self.root) |root| {381 if (self.root) |root| {
388 root.deinit(self.allocator);382 root.deinit(allocator);
389 self.allocator.destroy(root);383 allocator.destroy(root);
390 }384 }
391 self.ordered_nodes.deinit(self.allocator);385 self.ordered_nodes.deinit(allocator);
392}386}
393387
394fn createRoot(self: *Trie) !void {388fn createRoot(self: *Trie, allocator: *Allocator) !void {
395 if (self.root == null) {389 if (self.root == null) {
396 const root = try self.allocator.create(Node);390 const root = try allocator.create(Node);
397 root.* = .{ .base = self };391 root.* = .{ .base = self };
398 self.root = root;392 self.root = root;
399 self.node_count += 1;393 self.node_count += 1;