core/stdarch/crates/core_arch/src/wasm32/
memory.rs

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