pub struct Memory {
pub idx_type: IndexType,
pub limits: Limits,
pub shared: bool,
pub page_size_log2: u8,
}
Expand description
WebAssembly linear memory.
Fields§
§idx_type: IndexType
The type of the index used to access the memory.
limits: Limits
The limits constrain the minimum and optionally the maximum size of a memory. The limits are given in units of page size.
Whether the memory may be shared between multiple threads.
page_size_log2: u8
The log2 of this memory’s page size, in bytes.
By default the page size is 64KiB (0x10000; 2**16; 1<<16; 65536) but the
custom-page-sizes proposal allows opting into a page size of 1
.
Implementations§
source§impl Memory
impl Memory
sourcepub const DEFAULT_PAGE_SIZE: u32 = 65_536u32
pub const DEFAULT_PAGE_SIZE: u32 = 65_536u32
WebAssembly page sizes are 64KiB by default.
sourcepub const DEFAULT_PAGE_SIZE_LOG2: u8 = 16u8
pub const DEFAULT_PAGE_SIZE_LOG2: u8 = 16u8
WebAssembly page sizes are 64KiB (or 2**16
) by default.
sourcepub fn minimum_byte_size(&self) -> Result<u64, SizeOverflow>
pub fn minimum_byte_size(&self) -> Result<u64, SizeOverflow>
Returns the minimum size, in bytes, that this memory must be.
§Errors
Returns an error if the calculation of the minimum size overflows the
u64
return type. This means that the memory can’t be allocated but
it’s deferred to the caller to how to deal with that.
sourcepub fn maximum_byte_size(&self) -> Result<u64, SizeOverflow>
pub fn maximum_byte_size(&self) -> Result<u64, SizeOverflow>
Returns the maximum size, in bytes, that this memory is allowed to be.
Note that the return value here is not an Option
despite the maximum
size of a linear memory being optional in wasm. If a maximum size
is not present in the memory’s type then a maximum size is selected for
it. For example the maximum size of a 32-bit memory is 1<<32
. The
maximum size of a 64-bit linear memory is chosen to be a value that
won’t ever be allowed at runtime.
§Errors
Returns an error if the calculation of the maximum size overflows the
u64
return type. This means that the memory can’t be allocated but
it’s deferred to the caller to how to deal with that.
sourcepub fn max_size_based_on_index_type(&self) -> u64
pub fn max_size_based_on_index_type(&self) -> u64
Returns the maximum size memory is allowed to be only based on the index type used by this memory.
For example 32-bit linear memories return 1<<32
from this method.
sourcepub fn can_use_virtual_memory(
&self,
tunables: &Tunables,
host_page_size_log2: u8,
) -> bool
pub fn can_use_virtual_memory( &self, tunables: &Tunables, host_page_size_log2: u8, ) -> bool
Returns whether this memory can be implemented with virtual memory on
a host with host_page_size_log2
.
When this function returns true
then it means that signals such as
SIGSEGV on the host are compatible with wasm and can be used to
represent out-of-bounds memory accesses.
When this function returns false
then it means that this memory must,
for example, have explicit bounds checks. This additionally means that
virtual memory traps (e.g. SIGSEGV) cannot be relied on to implement
linear memory semantics.
sourcepub fn can_elide_bounds_check(
&self,
tunables: &Tunables,
host_page_size_log2: u8,
) -> bool
pub fn can_elide_bounds_check( &self, tunables: &Tunables, host_page_size_log2: u8, ) -> bool
Returns whether this memory is a candidate for bounds check elision given the configuration and host page size.
This function determines whether the given compilation configuration and
hos enables possible bounds check elision for this memory. Bounds checks
can only be elided if Memory::can_use_virtual_memory
returns true
for example but there are additionally requirements on the index size of
this memory and the memory reservation in tunables
.
Currently the only case that supports bounds check elision is when all of these apply:
- When
Memory::can_use_virtual_memory
returnstrue
. - This is a 32-bit linear memory (e.g. not 64-bit)
tunables.memory_reservation
is in excess of 4GiB
In this situation all computable addresses fall within the reserved space (modulo static offsets factoring in guard pages) so bounds checks may be elidable.
sourcepub fn static_heap_size(&self) -> Option<u64>
pub fn static_heap_size(&self) -> Option<u64>
Returns the static size of this heap in bytes at runtime, if available.
This is only computable when the minimum size equals the maximum size.
sourcepub fn memory_may_move(&self, tunables: &Tunables) -> bool
pub fn memory_may_move(&self, tunables: &Tunables) -> bool
Returs whether or not the base pointer of this memory is allowed to be relocated at runtime.
When this function returns false
then it means that after the initial
allocation the base pointer is constant for the entire lifetime of a
memory. This can enable compiler optimizations, for example.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Memory
impl<'de> Deserialize<'de> for Memory
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Copy for Memory
impl Eq for Memory
impl StructuralPartialEq for Memory
Auto Trait Implementations§
impl Freeze for Memory
impl RefUnwindSafe for Memory
impl Send for Memory
impl Sync for Memory
impl Unpin for Memory
impl UnwindSafe for Memory
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.