authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-05 01:04:24-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 14:23:23-08:00
log00b723dc9dec6a85e2018c9875aeae32ea331ad7
treeb680e0004ef28e6cd0eb7e1c359d1a1f7791c81b
parent82b5a1d313754037ee03d7cd20ee9bc8a64a1ba2

std.heap.WasmAllocator: update to new Allocator API


1 files changed, 17 insertions(+), 7 deletions(-)

lib/std/heap/WasmAllocator.zig+17-7
......@@ -20,6 +20,7 @@ comptime {
2020pub const vtable: Allocator.VTable = .{
2121 .alloc = alloc,
2222 .resize = resize,
23 .remap = remap,
2324 .free = free,
2425};
2526
......@@ -46,12 +47,11 @@ var frees: [size_class_count]usize = @splat(0);
4647/// For each big size class, points to the freed pointer.
4748var big_frees: [big_size_class_count]usize = @splat(0);
4849
49fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 {
50fn alloc(ctx: *anyopaque, len: usize, alignment: mem.Alignment, return_address: usize) ?[*]u8 {
5051 _ = ctx;
5152 _ = return_address;
5253 // Make room for the freelist next pointer.
53 const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
54 const actual_len = @max(len +| @sizeOf(usize), alignment);
54 const actual_len = @max(len +| @sizeOf(usize), alignment.toByteUnits());
5555 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
5656 const class = math.log2(slot_size) - min_class;
5757 if (class < size_class_count) {
......@@ -86,7 +86,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*
8686fn resize(
8787 ctx: *anyopaque,
8888 buf: []u8,
89 log2_buf_align: u8,
89 alignment: mem.Alignment,
9090 new_len: usize,
9191 return_address: usize,
9292) bool {
......@@ -94,7 +94,7 @@ fn resize(
9494 _ = return_address;
9595 // We don't want to move anything from one size class to another, but we
9696 // can recover bytes in between powers of two.
97 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
97 const buf_align = alignment.toByteUnits();
9898 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
9999 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
100100 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
......@@ -111,15 +111,25 @@ fn resize(
111111 }
112112}
113113
114fn remap(
115 context: *anyopaque,
116 memory: []u8,
117 alignment: mem.Alignment,
118 new_len: usize,
119 return_address: usize,
120) ?[*]u8 {
121 return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null;
122}
123
114124fn free(
115125 ctx: *anyopaque,
116126 buf: []u8,
117 log2_buf_align: u8,
127 alignment: mem.Alignment,
118128 return_address: usize,
119129) void {
120130 _ = ctx;
121131 _ = return_address;
122 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
132 const buf_align = alignment.toByteUnits();
123133 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
124134 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
125135 const class = math.log2(slot_size) - min_class;