Skip to content

Commit

Permalink
feat: add Memory.loadAlloc (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored Feb 22, 2024
1 parent 6839dd7 commit 336af87
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn load(self: Self, buf: []u8) void {
}
}

pub fn loadAlloc(self: Self, allocator: std.mem.Allocator) ![]u8 {
const out = try allocator.alloc(u8, @intCast(self.length));
self.load(out);
return out;
}

pub fn store(self: Self, buf: []const u8) void {
const len = buf.len;
var i: usize = 0;
Expand Down
9 changes: 3 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ pub const Plugin = struct {
}
const memory = Memory.init(offset, c_len);
defer memory.free();
const value = try self.allocator.alloc(u8, @intCast(c_len));
errdefer self.allocator.free(value);
memory.load(value);
const value = try memory.loadAlloc(self.allocator);
return value;
}

Expand Down Expand Up @@ -166,9 +164,8 @@ pub const Plugin = struct {
}
const memory = Memory.init(offset, c_len);
defer memory.free();
const value = try self.allocator.alloc(u8, @intCast(c_len));
errdefer self.allocator.free(value);
memory.load(value);
defer memory.free();
const value = try memory.loadAlloc(self.allocator);
return value;
}

Expand Down

0 comments on commit 336af87

Please sign in to comment.