From 521aaf350185b5f01816b7a9ec604335edb3ac16 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 3 Aug 2019 15:56:25 +1000 Subject: [PATCH 1/2] std: return Elf object from constructors instead of filling in pointer --- std/debug.zig | 3 +-- std/elf.zig | 10 ++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/std/debug.zig b/std/debug.zig index 32f96d3e15..d1c17343ef 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1024,8 +1024,7 @@ pub fn openElfDebugInfo( elf_seekable_stream: *DwarfSeekableStream, elf_in_stream: *DwarfInStream, ) !DwarfInfo { - var efile: elf.Elf = undefined; - try efile.openStream(allocator, elf_seekable_stream, elf_in_stream); + var efile = try elf.Elf.openStream(allocator, elf_seekable_stream, elf_in_stream); errdefer efile.close(); var di = DwarfInfo{ diff --git a/std/elf.zig b/std/elf.zig index c605a177a5..0b3ea20398 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -371,21 +371,21 @@ pub const Elf = struct { prealloc_file: File, /// Call close when done. - pub fn openPath(elf: *Elf, allocator: *mem.Allocator, path: []const u8) !void { + pub fn openPath(allocator: *mem.Allocator, path: []const u8) !Elf { @compileError("TODO implement"); } /// Call close when done. - pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: File) !void { + pub fn openFile(allocator: *mem.Allocator, file: File) !Elf { @compileError("TODO implement"); } pub fn openStream( - elf: *Elf, allocator: *mem.Allocator, seekable_stream: *io.SeekableStream(anyerror, anyerror), in: *io.InStream(anyerror), - ) !void { + ) !Elf { + var elf: Elf = undefined; elf.auto_close_stream = false; elf.allocator = allocator; elf.seekable_stream = seekable_stream; @@ -523,6 +523,8 @@ pub const Elf = struct { // not a string table return error.InvalidFormat; } + + return elf; } pub fn close(elf: *Elf) void { From 887eac0219345763f1ae9c8d9efad6950f6bbfe6 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 4 Aug 2019 16:27:36 +1000 Subject: [PATCH 2/2] std: remove elf.auto_close_stream and elf.prealloc_file --- std/elf.zig | 5 ----- 1 file changed, 5 deletions(-) diff --git a/std/elf.zig b/std/elf.zig index 0b3ea20398..37635895fd 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -356,7 +356,6 @@ pub const SectionHeader = struct { pub const Elf = struct { seekable_stream: *io.SeekableStream(anyerror, anyerror), in_stream: *io.InStream(anyerror), - auto_close_stream: bool, is_64: bool, endian: builtin.Endian, file_type: FileType, @@ -368,7 +367,6 @@ pub const Elf = struct { string_section: *SectionHeader, section_headers: []SectionHeader, allocator: *mem.Allocator, - prealloc_file: File, /// Call close when done. pub fn openPath(allocator: *mem.Allocator, path: []const u8) !Elf { @@ -386,7 +384,6 @@ pub const Elf = struct { in: *io.InStream(anyerror), ) !Elf { var elf: Elf = undefined; - elf.auto_close_stream = false; elf.allocator = allocator; elf.seekable_stream = seekable_stream; elf.in_stream = in; @@ -529,8 +526,6 @@ pub const Elf = struct { pub fn close(elf: *Elf) void { elf.allocator.free(elf.section_headers); - - if (elf.auto_close_stream) elf.prealloc_file.close(); } pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {