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