Skip to content

std: return Elf object from constructors instead of filling in pointer #2998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 6 additions & 9 deletions std/elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -368,25 +367,23 @@ pub const Elf = struct {
string_section: *SectionHeader,
section_headers: []SectionHeader,
allocator: *mem.Allocator,
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.auto_close_stream = false;
) !Elf {
var elf: Elf = undefined;
elf.allocator = allocator;
elf.seekable_stream = seekable_stream;
elf.in_stream = in;
Expand Down Expand Up @@ -523,12 +520,12 @@ pub const Elf = struct {
// not a string table
return error.InvalidFormat;
}

return elf;
}

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 {
Expand Down