Skip to content
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

feat: add some additional memory helpers #29

Merged
merged 1 commit into from
Feb 22, 2024
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
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
Loading