Struct wasmtime_runtime::MemoryImageSlot
source · pub struct MemoryImageSlot { /* private fields */ }
Expand description
Slot management of a copy-on-write image which can be reused for the pooling allocator.
This data structure manages a slot of linear memory, primarily in the pooling allocator, which optionally has a contiguous memory image in the middle of it. Pictorially this data structure manages a virtual memory region that looks like:
+--------------------+-------------------+--------------+--------------+
| anonymous | optional | anonymous | PROT_NONE |
| zero | memory | zero | memory |
| memory | image | memory | |
+--------------------+-------------------+--------------+--------------+
| <------+---------->
|<-----+------------> \
| \ image.len
| \
| image.linear_memory_offset
|
\
self.base is this virtual address
<------------------+------------------------------------------------>
\
static_size
<------------------+---------------------------------->
\
accessible
When a MemoryImageSlot
is created it’s told what the static_size
and
accessible
limits are. Initially there is assumed to be no image in linear
memory.
When MemoryImageSlot::instantiate
is called then the method will perform
a “synchronization” to take the image from its prior state to the new state
for the image specified. The first instantiation for example will mmap the
heap image into place. Upon reuse of a slot nothing happens except possibly
shrinking self.accessible
. When a new image is used then the old image is
mapped to anonymous zero memory and then the new image is mapped in place.
A MemoryImageSlot
is either dirty
or it isn’t. When a MemoryImageSlot
is dirty then it is assumed that any memory beneath self.accessible
could
have any value. Instantiation cannot happen into a dirty
slot, however, so
the MemoryImageSlot::clear_and_remain_ready
returns this memory back to
its original state to mark dirty = false
. This is done by resetting all
anonymous memory back to zero and the image itself back to its initial
contents.
On Linux this is achieved with the madvise(MADV_DONTNEED)
syscall. This
syscall will release the physical pages back to the OS but retain the
original mappings, effectively resetting everything back to its initial
state. Non-linux platforms will replace all memory below self.accessible
with a fresh zero’d mmap, meaning that reuse is effectively not supported.