authorgravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-02 12:26:14-06:00
committergravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-02 22:04:09-06:00
log45e04412786db37ecaf31a5fe6c4c1f4186e6d13
tree5b02145b92f44c95d451fd217093f5aaeeb5ae53
parentf32555aa087f69e1d2f4c214b180d945cca9f37b

Fix bugs


1 files changed, 23 insertions(+), 21 deletions(-)

lib/std/heap.zig+23-21
......@@ -252,10 +252,6 @@ extern const __heap_base: [*]u8;
252252extern fn @"llvm.wasm.memory.size.i32"(u32) u32;
253253extern fn @"llvm.wasm.memory.grow.i32"(u32, u32) i32;
254254
255test "" {
256 _ = WasmPageAllocator.realloc;
257}
258
259255const WasmPageAllocator = struct {
260256 // TODO: figure out why __heap_base cannot be found
261257 var heap_base_wannabe: [256]u8 = undefined;
......@@ -275,7 +271,7 @@ const WasmPageAllocator = struct {
275271 }
276272
277273 // TODO: optimize this terribleness
278 fn alloc(self: *FreeBlock, num_pages: usize) ?[]u8 {
274 fn useRecycled(self: *FreeBlock, num_pages: usize) ?[*]u8 {
279275 var found_idx: usize = 0;
280276 var found_size: usize = 0;
281277
......@@ -290,15 +286,18 @@ const WasmPageAllocator = struct {
290286 found_size += 1;
291287
292288 if (found_size >= num_pages) {
293 const page_ptr = @intToPtr([*]u8, (found_idx + self.offset) * std.mem.page_size);
294 return page_ptr[0 .. found_size * std.mem.page_size];
289 while (found_size > 0) {
290 found_size -= 1;
291 self.packed_data.set(found_idx + found_size, 0);
292 }
293 return @intToPtr([*]u8, (found_idx + self.offset) * std.mem.page_size);
295294 }
296295 }
297296 }
298297 return null;
299298 }
300299
301 fn reclaim(self: *FreeBlock, start_index: usize, end_index: usize) void {
300 fn recycle(self: *FreeBlock, start_index: usize, end_index: usize) void {
302301 var i = start_index - self.offset;
303302 while (i < end_index - self.offset) : (i += 1) {
304303 std.debug.assert(self.packed_data.get(i) == 0);
......@@ -315,23 +314,24 @@ const WasmPageAllocator = struct {
315314 }
316315
317316 fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
318 if (alignment > std.mem.page_size) {
319 return error.OutOfMemory;
320 }
321
322317 const n_pages = nPages(n);
323 return conventional.alloc(n_pages) orelse extended.alloc(n_pages) orelse {
318 const page = conventional.useRecycled(n_pages) orelse extended.useRecycled(n_pages) orelse blk: {
324319 const prev_page_count = @"llvm.wasm.memory.grow.i32"(0, @intCast(u32, n_pages));
325320 if (prev_page_count < 0) {
326321 return error.OutOfMemory;
327322 }
328323
329 const start_ptr = @intToPtr([*]u8, @intCast(usize, prev_page_count) * std.mem.page_size);
330 return start_ptr[0..n];
324 break :blk @intToPtr([*]u8, @intCast(usize, prev_page_count) * std.mem.page_size);
331325 };
326
327 return page[0..n];
332328 }
333329
334330 pub fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) Allocator.Error![]u8 {
331 if (new_align > std.mem.page_size) {
332 return error.OutOfMemory;
333 }
334
335335 if (old_mem.len == 0) {
336336 return alloc(allocator, new_size, new_align);
337337 } else if (new_size < old_mem.len) {
......@@ -352,21 +352,22 @@ const WasmPageAllocator = struct {
352352
353353 if (free_end > free_start) {
354354 if (conventional.totalPages() == 0) {
355 conventional.offset = 0;
355356 //conventional.initData(__heap_base[0..@intCast(usize, @"llvm.wasm.memory.size.i32"(0) * std.mem.page_size)]);
356357 conventional.initData(heap_base_wannabe[0..]);
357358 }
358359
359360 if (free_start < conventional.totalPages()) {
360 conventional.reclaim(free_start, free_end);
361 conventional.recycle(free_start, free_end);
361362 } else {
362363 if (extended.totalPages() == 0) {
363 extended.offset = conventional.totalPages();
364 extended.offset = conventional.offset + conventional.totalPages();
364365
365 // Steal the last page from the memory currently being reclaimed
366 // Steal the last page from the memory currently being recycled
366367 free_end -= 1;
367 extended.initData(@intToPtr([*]u8, free_end)[0..std.mem.page_size]);
368 extended.initData(@intToPtr([*]u8, free_end * std.mem.page_size)[0..std.mem.page_size]);
368369 }
369 conventional.reclaim(free_start, free_end);
370 extended.recycle(free_start, free_end);
370371 }
371372 }
372373
......@@ -932,7 +933,8 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi
932933fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
933934 //Maybe a platform's page_size is actually the same as or
934935 // very near usize?
935 if (mem.page_size << 2 > maxInt(usize)) return;
936 //if (mem.page_size << 2 > maxInt(usize)) return;
937 if (mem.page_size << 2 > 32768) return;
936938
937939 const USizeShift = @IntType(false, std.math.log2(usize.bit_count));
938940 const large_align = @as(u29, mem.page_size << 2);