Skip to content
Merged
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
19 changes: 15 additions & 4 deletions src/zemscripten.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub const EmmalocAllocator = struct {
.vtable = &.{
.alloc = &alloc,
.resize = &resize,
.remap = &remap,
.free = &free,
},
};
Expand All @@ -94,12 +95,12 @@ pub const EmmalocAllocator = struct {
fn alloc(
ctx: *anyopaque,
len: usize,
ptr_align_log2: u8,
ptr_align_log2: std.mem.Alignment,
return_address: usize,
) ?[*]u8 {
_ = ctx;
_ = return_address;
const ptr_align: u32 = @as(u32, 1) << @as(u5, @intCast(ptr_align_log2));
const ptr_align: u32 = @as(u32, 1) << @as(u5, @intFromEnum(ptr_align_log2));
if (!std.math.isPowerOfTwo(ptr_align)) unreachable;
const ptr = emmalloc_memalign(ptr_align, len) orelse return null;
return @ptrCast(ptr);
Expand All @@ -108,7 +109,7 @@ pub const EmmalocAllocator = struct {
fn resize(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: u8,
buf_align_log2: std.mem.Alignment,
new_len: usize,
return_address: usize,
) bool {
Expand All @@ -118,10 +119,20 @@ pub const EmmalocAllocator = struct {
return emmalloc_realloc_try(buf.ptr, new_len) != null;
}

fn remap(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: std.mem.Alignment,
new_len: usize,
return_address: usize,
) ?[*]u8 {
return if (resize(ctx, buf, buf_align_log2, new_len, return_address)) buf.ptr else null;
}

fn free(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: u8,
buf_align_log2: std.mem.Alignment,
return_address: usize,
) void {
_ = ctx;
Expand Down
Loading