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 {...@@ -20,6 +20,7 @@ comptime {
20pub const vtable: Allocator.VTable = .{20pub const vtable: Allocator.VTable = .{
21 .alloc = alloc,21 .alloc = alloc,
22 .resize = resize,22 .resize = resize,
23 .remap = remap,
23 .free = free,24 .free = free,
24};25};
2526
...@@ -46,12 +47,11 @@ var frees: [size_class_count]usize = @splat(0);...@@ -46,12 +47,11 @@ var frees: [size_class_count]usize = @splat(0);
46/// For each big size class, points to the freed pointer.47/// For each big size class, points to the freed pointer.
47var big_frees: [big_size_class_count]usize = @splat(0);48var 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 {
50 _ = ctx;51 _ = ctx;
51 _ = return_address;52 _ = return_address;
52 // Make room for the freelist next pointer.53 // 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.toByteUnits());
54 const actual_len = @max(len +| @sizeOf(usize), alignment);
55 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;55 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
56 const class = math.log2(slot_size) - min_class;56 const class = math.log2(slot_size) - min_class;
57 if (class < size_class_count) {57 if (class < size_class_count) {
...@@ -86,7 +86,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*...@@ -86,7 +86,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*
86fn resize(86fn resize(
87 ctx: *anyopaque,87 ctx: *anyopaque,
88 buf: []u8,88 buf: []u8,
89 log2_buf_align: u8,89 alignment: mem.Alignment,
90 new_len: usize,90 new_len: usize,
91 return_address: usize,91 return_address: usize,
92) bool {92) bool {
...@@ -94,7 +94,7 @@ fn resize(...@@ -94,7 +94,7 @@ fn resize(
94 _ = return_address;94 _ = return_address;
95 // We don't want to move anything from one size class to another, but we95 // We don't want to move anything from one size class to another, but we
96 // can recover bytes in between powers of two.96 // 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();
98 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);98 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
99 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);99 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
100 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);100 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
...@@ -111,15 +111,25 @@ fn resize(...@@ -111,15 +111,25 @@ fn resize(
111 }111 }
112}112}
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
114fn free(124fn free(
115 ctx: *anyopaque,125 ctx: *anyopaque,
116 buf: []u8,126 buf: []u8,
117 log2_buf_align: u8,127 alignment: mem.Alignment,
118 return_address: usize,128 return_address: usize,
119) void {129) void {
120 _ = ctx;130 _ = ctx;
121 _ = return_address;131 _ = return_address;
122 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));132 const buf_align = alignment.toByteUnits();
123 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);133 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
124 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);134 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
125 const class = math.log2(slot_size) - min_class;135 const class = math.log2(slot_size) - min_class;