Skip to content

Commit f01cb8c

Browse files
authored
Merge pull request #2998 from daurnimator/return-elf
std: return Elf object from constructors instead of filling in pointer
2 parents 5687323 + 887eac0 commit f01cb8c

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

std/debug.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,7 @@ pub fn openElfDebugInfo(
10241024
elf_seekable_stream: *DwarfSeekableStream,
10251025
elf_in_stream: *DwarfInStream,
10261026
) !DwarfInfo {
1027-
var efile: elf.Elf = undefined;
1028-
try efile.openStream(allocator, elf_seekable_stream, elf_in_stream);
1027+
var efile = try elf.Elf.openStream(allocator, elf_seekable_stream, elf_in_stream);
10291028
errdefer efile.close();
10301029

10311030
var di = DwarfInfo{

std/elf.zig

+6-9
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ pub const SectionHeader = struct {
356356
pub const Elf = struct {
357357
seekable_stream: *io.SeekableStream(anyerror, anyerror),
358358
in_stream: *io.InStream(anyerror),
359-
auto_close_stream: bool,
360359
is_64: bool,
361360
endian: builtin.Endian,
362361
file_type: FileType,
@@ -368,25 +367,23 @@ pub const Elf = struct {
368367
string_section: *SectionHeader,
369368
section_headers: []SectionHeader,
370369
allocator: *mem.Allocator,
371-
prealloc_file: File,
372370

373371
/// Call close when done.
374-
pub fn openPath(elf: *Elf, allocator: *mem.Allocator, path: []const u8) !void {
372+
pub fn openPath(allocator: *mem.Allocator, path: []const u8) !Elf {
375373
@compileError("TODO implement");
376374
}
377375

378376
/// Call close when done.
379-
pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: File) !void {
377+
pub fn openFile(allocator: *mem.Allocator, file: File) !Elf {
380378
@compileError("TODO implement");
381379
}
382380

383381
pub fn openStream(
384-
elf: *Elf,
385382
allocator: *mem.Allocator,
386383
seekable_stream: *io.SeekableStream(anyerror, anyerror),
387384
in: *io.InStream(anyerror),
388-
) !void {
389-
elf.auto_close_stream = false;
385+
) !Elf {
386+
var elf: Elf = undefined;
390387
elf.allocator = allocator;
391388
elf.seekable_stream = seekable_stream;
392389
elf.in_stream = in;
@@ -523,12 +520,12 @@ pub const Elf = struct {
523520
// not a string table
524521
return error.InvalidFormat;
525522
}
523+
524+
return elf;
526525
}
527526

528527
pub fn close(elf: *Elf) void {
529528
elf.allocator.free(elf.section_headers);
530-
531-
if (elf.auto_close_stream) elf.prealloc_file.close();
532529
}
533530

534531
pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {

0 commit comments

Comments
 (0)