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 {
31263126}
31273127
31283128fn writeExportInfoZld(self: *MachO) !void {
3129 var trie = Trie.init(self.base.allocator);
3130 defer trie.deinit();
3129 var trie: Trie = .{};
3130 defer trie.deinit(self.base.allocator);
31313131
31323132 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
31333133 const base_address = text_segment.inner.vmaddr;
......@@ -3139,14 +3139,14 @@ fn writeExportInfoZld(self: *MachO) !void {
31393139 const sym_name = self.getString(sym.n_strx);
31403140 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, .{
31433143 .name = sym_name,
31443144 .vmaddr_offset = sym.n_value - base_address,
31453145 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
31463146 });
31473147 }
31483148
3149 try trie.finalize();
3149 try trie.finalize(self.base.allocator);
31503150
31513151 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
31523152 defer self.base.allocator.free(buffer);
......@@ -5269,8 +5269,8 @@ fn writeExportInfo(self: *MachO) !void {
52695269 const tracy = trace(@src());
52705270 defer tracy.end();
52715271
5272 var trie = Trie.init(self.base.allocator);
5273 defer trie.deinit();
5272 var trie: Trie = .{};
5273 defer trie.deinit(self.base.allocator);
52745274
52755275 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
52765276 const base_address = text_segment.inner.vmaddr;
......@@ -5282,14 +5282,14 @@ fn writeExportInfo(self: *MachO) !void {
52825282 const sym_name = self.getString(sym.n_strx);
52835283 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, .{
52865286 .name = sym_name,
52875287 .vmaddr_offset = sym.n_value - base_address,
52885288 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
52895289 });
52905290 }
5291 try trie.finalize(self.base.allocator);
52915292
5292 try trie.finalize();
52935293 var buffer = try self.base.allocator.alloc(u8, @intCast(usize, trie.size));
52945294 defer self.base.allocator.free(buffer);
52955295 var stream = std.io.fixedBufferStream(buffer);
src/link/MachO/Trie.zig+12-18
......@@ -273,8 +273,6 @@ pub const Node = struct {
273273/// The root node of the trie.
274274root: ?*Node = null,
275275
276allocator: *Allocator,
277
278276/// If you want to access nodes ordered in DFS fashion,
279277/// you should call `finalize` first since the nodes
280278/// in this container are not guaranteed to not be stale
......@@ -294,10 +292,6 @@ node_count: usize = 0,
294292
295293trie_dirty: bool = true,
296294
297pub fn init(allocator: *Allocator) Trie {
298 return .{ .allocator = allocator };
299}
300
301295/// Export symbol that is to be placed in the trie.
302296pub const ExportSymbol = struct {
303297 /// Name of the symbol.
......@@ -314,9 +308,9 @@ pub const ExportSymbol = struct {
314308/// Insert a symbol into the trie, updating the prefixes in the process.
315309/// This operation may change the layout of the trie by splicing edges in
316310/// certain circumstances.
317pub fn put(self: *Trie, symbol: ExportSymbol) !void {
318 try self.createRoot();
319 const node = try self.root.?.put(self.allocator, symbol.name);
311pub fn put(self: *Trie, allocator: *Allocator, symbol: ExportSymbol) !void {
312 try self.createRoot(allocator);
313 const node = try self.root.?.put(allocator, symbol.name);
320314 node.terminal_info = .{
321315 .vmaddr_offset = symbol.vmaddr_offset,
322316 .export_flags = symbol.export_flags,
......@@ -328,13 +322,13 @@ pub fn put(self: *Trie, symbol: ExportSymbol) !void {
328322/// This step performs multiple passes through the trie ensuring
329323/// there are no gaps after every `Node` is ULEB128 encoded.
330324/// 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 {
332326 if (!self.trie_dirty) return;
333327
334328 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);
338332 defer fifo.deinit();
339333
340334 try fifo.writeItem(self.root.?);
......@@ -383,17 +377,17 @@ pub fn write(self: Trie, writer: anytype) !u64 {
383377 return counting_writer.bytes_written;
384378}
385379
386pub fn deinit(self: *Trie) void {
380pub fn deinit(self: *Trie, allocator: *Allocator) void {
387381 if (self.root) |root| {
388 root.deinit(self.allocator);
389 self.allocator.destroy(root);
382 root.deinit(allocator);
383 allocator.destroy(root);
390384 }
391 self.ordered_nodes.deinit(self.allocator);
385 self.ordered_nodes.deinit(allocator);
392386}
393387
394fn createRoot(self: *Trie) !void {
388fn createRoot(self: *Trie, allocator: *Allocator) !void {
395389 if (self.root == null) {
396 const root = try self.allocator.create(Node);
390 const root = try allocator.create(Node);
397391 root.* = .{ .base = self };
398392 self.root = root;
399393 self.node_count += 1;