Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
fengb committed Dec 2, 2019
1 parent f32555a commit aedfa7c
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,6 @@ extern const __heap_base: [*]u8;
extern fn @"llvm.wasm.memory.size.i32"(u32) u32;
extern fn @"llvm.wasm.memory.grow.i32"(u32, u32) i32;

test "" {
_ = WasmPageAllocator.realloc;
}

const WasmPageAllocator = struct {
// TODO: figure out why __heap_base cannot be found
var heap_base_wannabe: [256]u8 = undefined;
Expand All @@ -275,7 +271,7 @@ const WasmPageAllocator = struct {
}

// TODO: optimize this terribleness
fn alloc(self: *FreeBlock, num_pages: usize) ?[]u8 {
fn useRecycled(self: *FreeBlock, num_pages: usize) ?[*]u8 {
var found_idx: usize = 0;
var found_size: usize = 0;

Expand All @@ -290,15 +286,18 @@ const WasmPageAllocator = struct {
found_size += 1;

if (found_size >= num_pages) {
const page_ptr = @intToPtr([*]u8, (found_idx + self.offset) * std.mem.page_size);
return page_ptr[0 .. found_size * std.mem.page_size];
while (found_size > 0) {
found_size -= 1;
self.packed_data.set(found_idx + found_size, 0);
}
return @intToPtr([*]u8, (found_idx + self.offset) * std.mem.page_size);
}
}
}
return null;
}

fn reclaim(self: *FreeBlock, start_index: usize, end_index: usize) void {
fn recycle(self: *FreeBlock, start_index: usize, end_index: usize) void {
var i = start_index - self.offset;
while (i < end_index - self.offset) : (i += 1) {
std.debug.assert(self.packed_data.get(i) == 0);
Expand All @@ -315,23 +314,24 @@ const WasmPageAllocator = struct {
}

fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
if (alignment > std.mem.page_size) {
return error.OutOfMemory;
}

const n_pages = nPages(n);
return conventional.alloc(n_pages) orelse extended.alloc(n_pages) orelse {
const page = conventional.useRecycled(n_pages) orelse extended.useRecycled(n_pages) orelse blk: {
const prev_page_count = @"llvm.wasm.memory.grow.i32"(0, @intCast(u32, n_pages));
if (prev_page_count < 0) {
return error.OutOfMemory;
}

const start_ptr = @intToPtr([*]u8, @intCast(usize, prev_page_count) * std.mem.page_size);
return start_ptr[0..n];
break :blk @intToPtr([*]u8, @intCast(usize, prev_page_count) * std.mem.page_size);
};

return page[0..n];
}

pub fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) Allocator.Error![]u8 {
if (new_align > std.mem.page_size) {
return error.OutOfMemory;
}

if (old_mem.len == 0) {
return alloc(allocator, new_size, new_align);
} else if (new_size < old_mem.len) {
Expand All @@ -352,21 +352,22 @@ const WasmPageAllocator = struct {

if (free_end > free_start) {
if (conventional.totalPages() == 0) {
conventional.offset = 0;
//conventional.initData(__heap_base[0..@intCast(usize, @"llvm.wasm.memory.size.i32"(0) * std.mem.page_size)]);
conventional.initData(heap_base_wannabe[0..]);
}

if (free_start < conventional.totalPages()) {
conventional.reclaim(free_start, free_end);
conventional.recycle(free_start, free_end);
} else {
if (extended.totalPages() == 0) {
extended.offset = conventional.totalPages();
extended.offset = conventional.offset + conventional.totalPages();

// Steal the last page from the memory currently being reclaimed
// Steal the last page from the memory currently being recycled
free_end -= 1;
extended.initData(@intToPtr([*]u8, free_end)[0..std.mem.page_size]);
}
conventional.reclaim(free_start, free_end);
conventional.recycle(free_start, free_end);
}
}

Expand Down Expand Up @@ -932,7 +933,8 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi
fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
//Maybe a platform's page_size is actually the same as or
// very near usize?
if (mem.page_size << 2 > maxInt(usize)) return;
//if (mem.page_size << 2 > maxInt(usize)) return;
if (mem.page_size << 2 > 32768) return;

const USizeShift = @IntType(false, std.math.log2(usize.bit_count));
const large_align = @as(u29, mem.page_size << 2);
Expand Down

0 comments on commit aedfa7c

Please sign in to comment.