authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-29 14:05:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-29 23:46:02-07:00
log7f063b2c52b5daf55b8b1184502a94d0def4cd22
tree5ea8feb7f21d19a618a5699dd1380f7b7a396de6
parent931261752d167ea52e0d1e938a0c2bd7fed85327

WasmAllocator: simplify thanks to new Allocator interface

Now it can refuse to resize when it would disturb the metadata tracking strategy, resulting in smaller code size, a simpler implementation, and less fragmentation.

1 files changed, 31 insertions(+), 55 deletions(-)

lib/std/heap/WasmAllocator.zig+31-55
......@@ -43,14 +43,13 @@ var frees = [1]usize{0} ** size_class_count;
4343/// For each big size class, points to the freed pointer.
4444var big_frees = [1]usize{0} ** big_size_class_count;
4545
46fn alloc(ctx: *anyopaque, len: usize, alignment: u29, len_align: u29, ra: usize) Error![]u8 {
46fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 {
4747 _ = ctx;
48 _ = len_align;
49 _ = ra;
50 if (alignment > wasm.page_size) return error.OutOfMemory; // calm down
48 _ = return_address;
5149 // Make room for the freelist next pointer.
50 const alignment = @as(usize, 1) << @intCast(Allocator.Log2Align, log2_align);
5251 const actual_len = @max(len +| @sizeOf(usize), alignment);
53 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return error.OutOfMemory;
52 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
5453 const class = math.log2(slot_size) - min_class;
5554 if (class < size_class_count) {
5655 const addr = a: {
......@@ -63,7 +62,8 @@ fn alloc(ctx: *anyopaque, len: usize, alignment: u29, len_align: u29, ra: usize)
6362
6463 const next_addr = next_addrs[class];
6564 if (next_addr % wasm.page_size == 0) {
66 const addr = try allocBigPages(1);
65 const addr = allocBigPages(1);
66 if (addr == 0) return null;
6767 //std.debug.print("allocated fresh slot_size={d} class={d} addr=0x{x}\n", .{
6868 // slot_size, class, addr,
6969 //});
......@@ -74,77 +74,50 @@ fn alloc(ctx: *anyopaque, len: usize, alignment: u29, len_align: u29, ra: usize)
7474 break :a next_addr;
7575 }
7676 };
77 return @intToPtr([*]u8, addr)[0..len];
77 return @intToPtr([*]u8, addr);
7878 }
7979 const bigpages_needed = bigPagesNeeded(actual_len);
80 const addr = try allocBigPages(bigpages_needed);
81 return @intToPtr([*]u8, addr)[0..len];
80 const addr = allocBigPages(bigpages_needed);
81 return @intToPtr([*]u8, addr);
8282}
8383
8484fn resize(
8585 ctx: *anyopaque,
8686 buf: []u8,
87 buf_align: u29,
87 log2_buf_align: u8,
8888 new_len: usize,
89 len_align: u29,
90 ra: usize,
91) ?usize {
89 return_address: usize,
90) bool {
9291 _ = ctx;
93 _ = len_align;
94 _ = ra;
95 // We don't want to move anything from one size class to another. But we can recover bytes
96 // in between powers of two.
92 _ = return_address;
93 // We don't want to move anything from one size class to another, but we
94 // can recover bytes in between powers of two.
95 const buf_align = @as(usize, 1) << @intCast(Allocator.Log2Align, log2_buf_align);
9796 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
9897 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
9998 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
10099 const old_small_class = math.log2(old_small_slot_size) - min_class;
101100 if (old_small_class < size_class_count) {
102 const new_small_slot_size = math.ceilPowerOfTwo(usize, new_actual_len) catch return null;
103 if (old_small_slot_size == new_small_slot_size) return new_len;
104 if (new_actual_len >= old_actual_len) return null;
105 const new_small_class = math.log2(new_small_slot_size) - min_class;
106 assert(new_small_class < old_small_class);
107 // Split the small allocation into frees.
108 var class = old_small_class - 1;
109 while (true) {
110 const slot_size = @as(usize, 1) << @intCast(ushift, class + min_class);
111 const upper_addr = @ptrToInt(buf.ptr) + slot_size;
112 const node = @intToPtr(*usize, upper_addr + (slot_size - @sizeOf(usize)));
113 node.* = frees[class];
114 frees[class] = upper_addr;
115 if (class == new_small_class) break;
116 class -= 1;
117 }
101 const new_small_slot_size = math.ceilPowerOfTwo(usize, new_actual_len) catch return false;
102 return old_small_slot_size == new_small_slot_size;
118103 } else {
119104 const old_bigpages_needed = bigPagesNeeded(old_actual_len);
120105 const old_big_slot_pages = math.ceilPowerOfTwoAssert(usize, old_bigpages_needed);
121106 const new_bigpages_needed = bigPagesNeeded(new_actual_len);
122 const new_big_slot_pages = math.ceilPowerOfTwo(usize, new_bigpages_needed) catch return null;
123 if (old_big_slot_pages == new_big_slot_pages) return new_len;
124 if (new_actual_len >= old_actual_len) return null;
125
126 const new_small_slot_size = math.ceilPowerOfTwoAssert(usize, new_actual_len);
127 if (new_small_slot_size < size_class_count) {
128 const new_small_class = math.log2(new_small_slot_size) - min_class;
129 // TODO: push the big allocation into the free list
130 _ = new_small_class;
131 } else {
132 const new_big_class = math.log2(new_big_slot_pages);
133 // TODO: push the upper area into the free list
134 _ = new_big_class;
135 }
107 const new_big_slot_pages = math.ceilPowerOfTwo(usize, new_bigpages_needed) catch return false;
108 return old_big_slot_pages == new_big_slot_pages;
136109 }
137 return new_len;
138110}
139111
140112fn free(
141113 ctx: *anyopaque,
142114 buf: []u8,
143 buf_align: u29,
115 log2_buf_align: u8,
144116 return_address: usize,
145117) void {
146118 _ = ctx;
147119 _ = return_address;
120 const buf_align = @as(usize, 1) << @intCast(Allocator.Log2Align, log2_buf_align);
148121 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
149122 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
150123 const class = math.log2(slot_size) - min_class;
......@@ -168,7 +141,7 @@ inline fn bigPagesNeeded(byte_count: usize) usize {
168141 return (byte_count + (bigpage_size + (@sizeOf(usize) - 1))) / bigpage_size;
169142}
170143
171fn allocBigPages(n: usize) !usize {
144fn allocBigPages(n: usize) usize {
172145 const pow2_pages = math.ceilPowerOfTwoAssert(usize, n);
173146 const slot_size_bytes = pow2_pages * bigpage_size;
174147 const class = math.log2(pow2_pages);
......@@ -181,7 +154,7 @@ fn allocBigPages(n: usize) !usize {
181154 }
182155
183156 const page_index = @wasmMemoryGrow(0, pow2_pages * pages_per_bigpage);
184 if (page_index <= 0) return error.OutOfMemory;
157 if (page_index <= 0) return 0;
185158 const addr = @intCast(u32, page_index) * wasm.page_size;
186159 return addr;
187160}
......@@ -259,13 +232,15 @@ test "shrink" {
259232
260233 mem.set(u8, slice, 0x11);
261234
262 slice = test_ally.shrink(slice, 17);
235 try std.testing.expect(test_ally.resize(slice, 17));
236 slice = slice[0..17];
263237
264238 for (slice) |b| {
265239 try std.testing.expect(b == 0x11);
266240 }
267241
268 slice = test_ally.shrink(slice, 16);
242 try std.testing.expect(test_ally.resize(slice, 16));
243 slice = slice[0..16];
269244
270245 for (slice) |b| {
271246 try std.testing.expect(b == 0x11);
......@@ -303,11 +278,12 @@ test "shrink large object to large object" {
303278 slice[0] = 0x12;
304279 slice[60] = 0x34;
305280
306 slice = test_ally.resize(slice, bigpage_size * 2 + 1) orelse return;
281 try std.testing.expect(test_ally.resize(slice, bigpage_size * 2 + 1));
282 slice = slice[0 .. bigpage_size * 2 + 1];
307283 try std.testing.expect(slice[0] == 0x12);
308284 try std.testing.expect(slice[60] == 0x34);
309285
310 slice = test_ally.shrink(slice, bigpage_size * 2 + 1);
286 try std.testing.expect(test_ally.resize(slice, bigpage_size * 2 + 1));
311287 try std.testing.expect(slice[0] == 0x12);
312288 try std.testing.expect(slice[60] == 0x34);
313289