]>
Commit | Line | Data |
---|---|---|
0bf4aa26 XL |
1 | #[cfg(test)] |
2 | use stdsimd_test::assert_instr; | |
3 | #[cfg(test)] | |
4 | use wasm_bindgen_test::wasm_bindgen_test; | |
5 | ||
6 | extern "C" { | |
7 | #[link_name = "llvm.wasm.memory.grow.i32"] | |
8 | fn llvm_memory_grow(mem: i32, pages: i32) -> i32; | |
9 | #[link_name = "llvm.wasm.memory.size.i32"] | |
10 | fn llvm_memory_size(mem: i32) -> i32; | |
11 | } | |
12 | ||
13 | /// Corresponding intrinsic to wasm's [`memory.size` instruction][instr] | |
14 | /// | |
15 | /// This function, when called, will return the current memory size in units of | |
0731742a | 16 | /// pages. The current WebAssembly page size is 65536 bytes (64 KB). |
0bf4aa26 XL |
17 | /// |
18 | /// The argument `mem` is the numerical index of which memory to return the | |
0731742a XL |
19 | /// size of. Note that currently the WebAssembly specification only supports one |
20 | /// memory, so it is required that zero is passed in. The argument is present to | |
21 | /// be forward-compatible with future WebAssembly revisions. If a nonzero | |
22 | /// argument is passed to this function it will currently unconditionally abort. | |
0bf4aa26 | 23 | /// |
0731742a | 24 | /// [instr]: http://webassembly.github.io/spec/core/exec/instructions.html#exec-memory-size |
0bf4aa26 XL |
25 | #[inline] |
26 | #[cfg_attr(test, assert_instr("memory.size", mem = 0))] | |
27 | #[rustc_args_required_const(0)] | |
0731742a XL |
28 | #[stable(feature = "simd_wasm32", since = "1.33.0")] |
29 | pub fn memory_size(mem: u32) -> usize { | |
30 | unsafe { | |
31 | if mem != 0 { | |
532ac7d7 | 32 | crate::intrinsics::abort(); |
0731742a XL |
33 | } |
34 | llvm_memory_size(0) as usize | |
0bf4aa26 | 35 | } |
0bf4aa26 XL |
36 | } |
37 | ||
38 | /// Corresponding intrinsic to wasm's [`memory.grow` instruction][instr] | |
39 | /// | |
40 | /// This function, when called, will attempt to grow the default linear memory | |
0731742a XL |
41 | /// by the specified `delta` of pages. The current WebAssembly page size is |
42 | /// 65536 bytes (64 KB). If memory is successfully grown then the previous size | |
43 | /// of memory, in pages, is returned. If memory cannot be grown then | |
44 | /// `usize::max_value()` is returned. | |
0bf4aa26 XL |
45 | /// |
46 | /// The argument `mem` is the numerical index of which memory to return the | |
0731742a XL |
47 | /// size of. Note that currently the WebAssembly specification only supports one |
48 | /// memory, so it is required that zero is passed in. The argument is present to | |
49 | /// be forward-compatible with future WebAssembly revisions. If a nonzero | |
50 | /// argument is passed to this function it will currently unconditionally abort. | |
0bf4aa26 | 51 | /// |
0731742a | 52 | /// [instr]: http://webassembly.github.io/spec/core/exec/instructions.html#exec-memory-grow |
0bf4aa26 XL |
53 | #[inline] |
54 | #[cfg_attr(test, assert_instr("memory.grow", mem = 0))] | |
55 | #[rustc_args_required_const(0)] | |
0731742a XL |
56 | #[stable(feature = "simd_wasm32", since = "1.33.0")] |
57 | pub fn memory_grow(mem: u32, delta: usize) -> usize { | |
58 | unsafe { | |
59 | if mem != 0 { | |
532ac7d7 | 60 | crate::intrinsics::abort(); |
0731742a XL |
61 | } |
62 | llvm_memory_grow(0, delta as i32) as isize as usize | |
0bf4aa26 | 63 | } |
0bf4aa26 | 64 | } |