| ... | @@ -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. |
| 274 | root: ?*Node = null, | 274 | root: ?*Node = null, |
| 275 | | 275 | |
| 276 | allocator: *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 nodes | 277 | /// you should call `finalize` first since the nodes |
| 280 | /// in this container are not guaranteed to not be stale | 278 | /// 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, |
| 294 | | 292 | |
| 295 | trie_dirty: bool = true, | 293 | trie_dirty: bool = true, |
| 296 | | 294 | |
| 297 | pub 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. |
| 302 | pub const ExportSymbol = struct { | 296 | pub 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 in | 309 | /// This operation may change the layout of the trie by splicing edges in |
| 316 | /// certain circumstances. | 310 | /// certain circumstances. |
| 317 | pub fn put(self: *Trie, symbol: ExportSymbol) !void { | 311 | pub 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 ensuring | 322 | /// 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. |
| 331 | pub fn finalize(self: *Trie) !void { | 325 | pub fn finalize(self: *Trie, allocator: *Allocator) !void { |
| 332 | if (!self.trie_dirty) return; | 326 | if (!self.trie_dirty) return; |
| 333 | | 327 | |
| 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); |
| 336 | | 330 | |
| 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(); |
| 339 | | 333 | |
| 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 | } |
| 385 | | 379 | |
| 386 | pub fn deinit(self: *Trie) void { | 380 | pub 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 | } |
| 393 | | 387 | |
| 394 | fn createRoot(self: *Trie) !void { | 388 | fn 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; |