macho+wasm: unify and clean up closing file handles

This commit is contained in:
Jakub Konka 2022-09-07 19:16:30 +02:00
parent 639237c7b4
commit 678e07b924
6 changed files with 6 additions and 5 deletions

View File

@ -1437,7 +1437,6 @@ fn parseArchive(self: *MachO, path: []const u8, force_load: bool) !bool {
if (force_load) {
defer archive.deinit(gpa);
defer file.close();
// Get all offsets from the ToC
var offsets = std.AutoArrayHashMap(u32, void).init(gpa);
defer offsets.deinit();
@ -3015,7 +3014,6 @@ pub fn deinit(self: *MachO) void {
}
if (self.d_sym) |*d_sym| {
d_sym.file.close();
d_sym.deinit(gpa);
}
@ -3044,7 +3042,6 @@ pub fn deinit(self: *MachO) void {
self.objects.deinit(gpa);
for (self.archives.items) |*archive| {
archive.file.close();
archive.deinit(gpa);
}
self.archives.deinit(gpa);

View File

@ -88,6 +88,7 @@ const ar_hdr = extern struct {
};
pub fn deinit(self: *Archive, allocator: Allocator) void {
self.file.close();
for (self.toc.keys()) |*key| {
allocator.free(key.*);
}

View File

@ -306,6 +306,7 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
}
pub fn deinit(self: *DebugSymbols, allocator: Allocator) void {
self.file.close();
self.segments.deinit(allocator);
self.sections.deinit(allocator);
self.dwarf.deinit();

View File

@ -648,12 +648,10 @@ pub fn deinit(self: *Wasm) void {
gpa.free(segment_info.name);
}
for (self.objects.items) |*object| {
object.file.?.close();
object.deinit(gpa);
}
for (self.archives.items) |*archive| {
archive.file.close();
archive.deinit(gpa);
}

View File

@ -95,6 +95,7 @@ const ar_hdr = extern struct {
};
pub fn deinit(archive: *Archive, allocator: Allocator) void {
archive.file.close();
for (archive.toc.keys()) |*key| {
allocator.free(key.*);
}

View File

@ -141,6 +141,9 @@ pub fn create(gpa: Allocator, file: std.fs.File, name: []const u8, maybe_max_siz
/// Frees all memory of `Object` at once. The given `Allocator` must be
/// the same allocator that was used when `init` was called.
pub fn deinit(self: *Object, gpa: Allocator) void {
if (self.file) |file| {
file.close();
}
for (self.func_types) |func_ty| {
gpa.free(func_ty.params);
gpa.free(func_ty.returns);