authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-05-02 08:56:49-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-03 16:21:33-04:00
logf86b8e4535c6b7803b724e07d32b1cf72f6d2b2c
tree9cabd1fa12cc358bb414b4b9af05144cc680750e
parent2f39da7cddca3e3ae6a3c92f2a52eab77f311ccc

WasmAllocator: compileError on non-wasm arch


1 files changed, 13 insertions(+), 11 deletions(-)

std/heap.zig+13-11
...@@ -143,7 +143,7 @@ pub const DirectAllocator = struct {...@@ -143,7 +143,7 @@ pub const DirectAllocator = struct {
143 const result = try alloc(allocator, new_size, new_align);143 const result = try alloc(allocator, new_size, new_align);
144 if (old_mem.len != 0) {144 if (old_mem.len != 0) {
145 @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));145 @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
146 _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);146 _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
147 }147 }
148 return result;148 return result;
149 },149 },
...@@ -155,12 +155,12 @@ pub const DirectAllocator = struct {...@@ -155,12 +155,12 @@ pub const DirectAllocator = struct {
155 const old_record_addr = old_adjusted_addr + old_mem.len;155 const old_record_addr = old_adjusted_addr + old_mem.len;
156 const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;156 const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
157 const old_ptr = @intToPtr(*c_void, root_addr);157 const old_ptr = @intToPtr(*c_void, root_addr);
158 158
159 if(new_size == 0) {159 if (new_size == 0) {
160 if (os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;160 if (os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;
161 return old_mem[0..0];161 return old_mem[0..0];
162 }162 }
163 163
164 const amt = new_size + new_align + @sizeOf(usize);164 const amt = new_size + new_align + @sizeOf(usize);
165 const new_ptr = os.windows.HeapReAlloc(165 const new_ptr = os.windows.HeapReAlloc(
166 self.heap_handle.?,166 self.heap_handle.?,
...@@ -178,11 +178,7 @@ pub const DirectAllocator = struct {...@@ -178,11 +178,7 @@ pub const DirectAllocator = struct {
178 // or the memory starting at the old offset would be outside of the new allocation,178 // or the memory starting at the old offset would be outside of the new allocation,
179 // then we need to copy the memory to a valid aligned address and use that179 // then we need to copy the memory to a valid aligned address and use that
180 const new_aligned_addr = mem.alignForward(new_root_addr, new_align);180 const new_aligned_addr = mem.alignForward(new_root_addr, new_align);
181 @memcpy(181 @memcpy(@intToPtr([*]u8, new_aligned_addr), @intToPtr([*]u8, new_adjusted_addr), std.math.min(old_mem.len, new_size));
182 @intToPtr([*]u8, new_aligned_addr),
183 @intToPtr([*]u8, new_adjusted_addr),
184 std.math.min(old_mem.len, new_size),
185 );
186 new_adjusted_addr = new_aligned_addr;182 new_adjusted_addr = new_aligned_addr;
187 }183 }
188 const new_record_addr = new_adjusted_addr + new_size;184 const new_record_addr = new_adjusted_addr + new_size;
...@@ -364,6 +360,12 @@ const WasmAllocator = struct {...@@ -364,6 +360,12 @@ const WasmAllocator = struct {
364 num_pages: usize,360 num_pages: usize,
365 end_index: usize,361 end_index: usize,
366362
363 comptime {
364 if (builtin.arch != .wasm32) {
365 @compileError("WasmAllocator is only available for wasm32 arch");
366 }
367 }
368
367 fn alloc(allocator: *Allocator, size: usize, alignment: u29) ![]u8 {369 fn alloc(allocator: *Allocator, size: usize, alignment: u29) ![]u8 {
368 const self = @fieldParentPtr(WasmAllocator, "allocator", allocator);370 const self = @fieldParentPtr(WasmAllocator, "allocator", allocator);
369371
...@@ -375,12 +377,12 @@ const WasmAllocator = struct {...@@ -375,12 +377,12 @@ const WasmAllocator = struct {
375 if (new_end_index > self.num_pages * os.page_size) {377 if (new_end_index > self.num_pages * os.page_size) {
376 const required_memory = new_end_index - (self.num_pages * os.page_size);378 const required_memory = new_end_index - (self.num_pages * os.page_size);
377379
378 var num_pages: u32 = required_memory / os.page_size;380 var num_pages: usize = required_memory / os.page_size;
379 if (required_memory % os.page_size != 0) {381 if (required_memory % os.page_size != 0) {
380 num_pages += 1;382 num_pages += 1;
381 }383 }
382384
383 const prev_page = @"llvm.wasm.memory.grow.i32"(0, num_pages);385 const prev_page = @"llvm.wasm.memory.grow.i32"(0, @intCast(u32, num_pages));
384 if (prev_page == -1) {386 if (prev_page == -1) {
385 return error.OutOfMemory;387 return error.OutOfMemory;
386 }388 }