Skip to content

Commit a3be40a

Browse files
Grazfathermattnite
authored andcommitted
Don't alloc while loading elf
1 parent 23dc121 commit a3be40a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

sim/aviron/src/main.zig

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,18 @@ pub fn main() !u8 {
105105

106106
if (is_data) {
107107
// Load data segment via Bus interface
108-
// Read the segment data into a temporary buffer
109-
const segment_buf = try allocator.alloc(u8, phdr.p_filesz);
110-
defer allocator.free(segment_buf);
111-
try reader.interface.readSliceAll(segment_buf);
112-
113-
// Write each byte through the bus
114-
for (segment_buf, 0..) |byte, i| {
115-
data_bus.write(@intCast(target_addr + i), byte);
108+
// Use a stack buffer to read and write through the bus
109+
var read_buf: [256]u8 = undefined;
110+
var remaining = phdr.p_filesz;
111+
var offset: usize = 0;
112+
while (remaining > 0) {
113+
const to_read = @min(remaining, read_buf.len);
114+
try reader.interface.readSliceAll(read_buf[0..to_read]);
115+
for (read_buf[0..to_read], 0..) |byte, i| {
116+
data_bus.write(@intCast(target_addr + offset + i), byte);
117+
}
118+
offset += to_read;
119+
remaining -= to_read;
116120
}
117121

118122
// Zero-fill the remaining memory

0 commit comments

Comments
 (0)