MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / new

Method new

crates/wasmtime/src/runtime/vm/memory/malloc.rs:26–66  ·  view source on GitHub ↗
(
        _ty: &wasmtime_environ::Memory,
        memory_tunables: &MemoryTunables<'_>,
        minimum: usize,
    )

Source from the content-addressed store, hash-verified

24
25impl MallocMemory {
26 pub fn new(
27 _ty: &wasmtime_environ::Memory,
28 memory_tunables: &MemoryTunables<'_>,
29 minimum: usize,
30 ) -> Result<Self> {
31 if memory_tunables.guard_size() > 0 {
32 bail!("malloc memory is only compatible if guard pages aren't used");
33 }
34 if memory_tunables.reservation() > 0 {
35 bail!("malloc memory is only compatible with no ahead-of-time memory reservation");
36 }
37 if memory_tunables.tunables().memory_init_cow {
38 bail!("malloc memory cannot be used with CoW images");
39 }
40
41 let initial_allocation_byte_size = minimum
42 .checked_add(memory_tunables.reservation_for_growth().try_into()?)
43 .context("memory allocation size too large")?;
44
45 let initial_allocation_len = byte_size_to_element_len(initial_allocation_byte_size);
46 let mut storage = Vec::new();
47 storage
48 .try_reserve(initial_allocation_len)
49 .with_context(|| {
50 format!(
51 "failed to allocate {initial_allocation_byte_size:#x} \
52 bytes ({minimum:#x} minimum + {:#x} memory_reservation_for_growth)",
53 memory_tunables.reservation_for_growth(),
54 )
55 })?;
56
57 let initial_len = byte_size_to_element_len(minimum);
58 if initial_len > 0 {
59 grow_storage_to(&mut storage, initial_len);
60 }
61 Ok(MallocMemory {
62 base_ptr: SendSyncPtr::new(NonNull::new(storage.as_mut_ptr()).unwrap()).cast(),
63 storage,
64 byte_len: minimum,
65 })
66 }
67}
68
69impl RuntimeLinearMemory for MallocMemory {

Callers

nothing calls this directly

Calls 15

byte_size_to_element_lenFunction · 0.85
grow_storage_toFunction · 0.85
OkFunction · 0.85
guard_sizeMethod · 0.80
reservationMethod · 0.80
with_contextMethod · 0.80
try_reserveMethod · 0.80
newFunction · 0.50
tunablesMethod · 0.45
contextMethod · 0.45
checked_addMethod · 0.45

Tested by

no test coverage detected