From 2b3bda43e352152f0150bf2e795419cf1bcfcd90 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 1 Jul 2021 17:25:51 +0200 Subject: zld: abstract Symbol creation logic --- src/link/MachO/StringTable.zig | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) (limited to 'src/link/MachO/StringTable.zig') diff --git a/src/link/MachO/StringTable.zig b/src/link/MachO/StringTable.zig index 5437c70476..43770afdc1 100644 --- a/src/link/MachO/StringTable.zig +++ b/src/link/MachO/StringTable.zig @@ -8,7 +8,6 @@ const Allocator = mem.Allocator; allocator: *Allocator, buffer: std.ArrayListUnmanaged(u8) = .{}, -used_offsets: std.ArrayListUnmanaged(u32) = .{}, cache: std.StringHashMapUnmanaged(u32) = .{}, pub const Error = error{OutOfMemory}; @@ -22,8 +21,13 @@ pub fn init(allocator: *Allocator) Error!StringTable { } pub fn deinit(self: *StringTable) void { + { + var it = self.cache.keyIterator(); + while (it.next()) |key| { + self.allocator.free(key.*); + } + } self.cache.deinit(self.allocator); - self.used_offsets.deinit(self.allocator); self.buffer.deinit(self.allocator); } @@ -33,8 +37,6 @@ pub fn getOrPut(self: *StringTable, string: []const u8) Error!u32 { return off; } - const invalidate_cache = self.needsToGrow(string.len + 1); - try self.buffer.ensureUnusedCapacity(self.allocator, string.len + 1); const new_off = @intCast(u32, self.buffer.items.len); @@ -43,25 +45,7 @@ pub fn getOrPut(self: *StringTable, string: []const u8) Error!u32 { self.buffer.appendSliceAssumeCapacity(string); self.buffer.appendAssumeCapacity(0); - if (invalidate_cache) { - log.debug("invalidating cache", .{}); - // Re-create the cache. - self.cache.clearRetainingCapacity(); - for (self.used_offsets.items) |off| { - try self.cache.putNoClobber(self.allocator, self.get(off).?, off); - } - } - - { - log.debug("cache:", .{}); - var it = self.cache.iterator(); - while (it.next()) |entry| { - log.debug(" | {s} => {}", .{ entry.key_ptr.*, entry.value_ptr.* }); - } - } - - try self.cache.putNoClobber(self.allocator, self.get(new_off).?, new_off); - try self.used_offsets.append(self.allocator, new_off); + try self.cache.putNoClobber(self.allocator, try self.allocator.dupe(u8, string), new_off); return new_off; } @@ -78,7 +62,3 @@ pub fn asSlice(self: StringTable) []const u8 { pub fn size(self: StringTable) u64 { return self.buffer.items.len; } - -fn needsToGrow(self: StringTable, needed_space: u64) bool { - return self.buffer.capacity < needed_space + self.size(); -} -- cgit v1.2.3