runtime: refactor Memory to always use Box<dyn RuntimeLinearMemory> (#4086)

While working with the runtime `Memory` object, it became clear that
some refactoring was needed. In order to implement shared memory from
the threads proposal, we must be able to atomically change the memory
size. Previously, the split into variants, `Memory::Static` and
`Memory::Dynamic`, made any attempt to lock forced us to duplicate logic
in various places.

This change moves `enum Memory { Static..., Dynamic... }` to simply
`struct Memory(Box<dyn RuntimeLinearMemory>)`. A new type,
`ExternalMemory`, takes the place of `Memory::Static` and also
implements the `RuntimeLinearMemory` trait, allowing `Memory` to contain
the same two options as before: `MmapMemory` for `Memory::Dynamic` and
`ExternalMemory` for `Memory::Static`. To interface with the
`PoolingAllocator`, this change also required the ability to downcast to
the internal representation.
This commit is contained in:
Andrew Brown
2022-04-29 08:12:38 -07:00
committed by GitHub
parent 5b7d56f6f7
commit 3dbdcfa220
3 changed files with 181 additions and 173 deletions

View File

@@ -42,7 +42,7 @@ impl RuntimeLinearMemory for LinearMemoryProxy {
self.mem.grow_to(new_size)
}
fn vmmemory(&self) -> VMMemoryDefinition {
fn vmmemory(&mut self) -> VMMemoryDefinition {
VMMemoryDefinition {
base: self.mem.as_ptr(),
current_length: self.mem.byte_size(),
@@ -52,6 +52,11 @@ impl RuntimeLinearMemory for LinearMemoryProxy {
fn needs_init(&self) -> bool {
true
}
#[cfg(feature = "pooling-allocator")]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
#[derive(Clone)]