| ... | ... | @@ -285,7 +285,7 @@ const WasmPageAllocator = struct { |
| 285 | 285 | // Revisit if this is settled: https://github.com/ziglang/zig/issues/3806 |
| 286 | 286 | const not_found = std.math.maxInt(usize); |
| 287 | 287 | |
| 288 | | fn useRecycled(self: FreeBlock, num_pages: usize) usize { |
| 288 | fn useRecycled(self: FreeBlock, num_pages: usize, alignment: u29) usize { |
| 289 | 289 | @setCold(true); |
| 290 | 290 | for (self.data) |segment, i| { |
| 291 | 291 | const spills_into_next = @bitCast(i128, segment) < 0; |
| ... | ... | @@ -298,7 +298,8 @@ const WasmPageAllocator = struct { |
| 298 | 298 | var count: usize = 0; |
| 299 | 299 | while (j + count < self.totalPages() and self.getBit(j + count) == .free) { |
| 300 | 300 | count += 1; |
| 301 | | if (count >= num_pages) { |
| 301 | const addr = j * mem.page_size; |
| 302 | if (count >= num_pages and mem.isAligned(addr, alignment)) { |
| 302 | 303 | self.setBits(j, num_pages, .used); |
| 303 | 304 | return j; |
| 304 | 305 | } |
| ... | ... | @@ -329,29 +330,36 @@ const WasmPageAllocator = struct { |
| 329 | 330 | |
| 330 | 331 | fn alloc(allocator: *Allocator, len: usize, alignment: u29, len_align: u29) error{OutOfMemory}![]u8 { |
| 331 | 332 | const page_count = nPages(len); |
| 332 | | const page_idx = try allocPages(page_count); |
| 333 | const page_idx = try allocPages(page_count, alignment); |
| 333 | 334 | return @intToPtr([*]u8, page_idx * mem.page_size) |
| 334 | 335 | [0..alignPageAllocLen(page_count * mem.page_size, len, len_align)]; |
| 335 | 336 | } |
| 336 | | fn allocPages(page_count: usize) !usize { |
| 337 | fn allocPages(page_count: usize, alignment: u29) !usize { |
| 337 | 338 | { |
| 338 | | const idx = conventional.useRecycled(page_count); |
| 339 | const idx = conventional.useRecycled(page_count, alignment); |
| 339 | 340 | if (idx != FreeBlock.not_found) { |
| 340 | 341 | return idx; |
| 341 | 342 | } |
| 342 | 343 | } |
| 343 | 344 | |
| 344 | | const idx = extended.useRecycled(page_count); |
| 345 | const idx = extended.useRecycled(page_count, alignment); |
| 345 | 346 | if (idx != FreeBlock.not_found) { |
| 346 | 347 | return idx + extendedOffset(); |
| 347 | 348 | } |
| 348 | 349 | |
| 349 | | const prev_page_count = @wasmMemoryGrow(0, @intCast(u32, page_count)); |
| 350 | | if (prev_page_count <= 0) { |
| 350 | const next_page_idx = @wasmMemorySize(0); |
| 351 | const next_page_addr = next_page_idx * mem.page_size; |
| 352 | const aligned_addr = mem.alignForward(next_page_addr, alignment); |
| 353 | const drop_page_count = @divExact(aligned_addr - next_page_addr, mem.page_size); |
| 354 | const result = @wasmMemoryGrow(0, @intCast(u32, drop_page_count + page_count)); |
| 355 | if (result <= 0) |
| 351 | 356 | return error.OutOfMemory; |
| 357 | assert(result == next_page_idx); |
| 358 | const aligned_page_idx = next_page_idx + drop_page_count; |
| 359 | if (drop_page_count > 0) { |
| 360 | freePages(next_page_idx, aligned_page_idx); |
| 352 | 361 | } |
| 353 | | |
| 354 | | return @intCast(usize, prev_page_count); |
| 362 | return @intCast(usize, aligned_page_idx); |
| 355 | 363 | } |
| 356 | 364 | |
| 357 | 365 | fn freePages(start: usize, end: usize) void { |