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

Method allocate_instance

crates/wasmtime/src/runtime/store.rs:2290–2351  ·  view source on GitHub ↗

Constructs and executes an `InstanceAllocationRequest` and pushes the returned instance into the store. This is a helper method for invoking `InstanceAllocator::allocate_module` with the appropriate parameters from this store's own configuration. The `kind` provided is used to distinguish between "real" modules and dummy ones that are synthesized for embedder-created memories, globals, tables, et

(
        &mut self,
        limiter: Option<&mut StoreResourceLimiter<'_>>,
        kind: AllocateInstanceKind<'_>,
        runtime_info: &ModuleRuntimeInfo,
        imports: Imports<'_>,
    )

Source from the content-addressed store, hash-verified

2288 /// The `imports` provided must be correctly sized/typed for the module
2289 /// being allocated.
2290 pub(crate) async unsafe fn allocate_instance(
2291 &mut self,
2292 limiter: Option<&mut StoreResourceLimiter<'_>>,
2293 kind: AllocateInstanceKind<'_>,
2294 runtime_info: &ModuleRuntimeInfo,
2295 imports: Imports<'_>,
2296 ) -> Result<InstanceId> {
2297 self.instances.reserve(1)?;
2298
2299 let id = self.instances.next_key();
2300
2301 let allocator = match kind {
2302 AllocateInstanceKind::Module(_) => self.engine().allocator(),
2303 AllocateInstanceKind::Dummy { allocator } => allocator,
2304 };
2305 // SAFETY: this function's own contract is the same as
2306 // `allocate_module`, namely the imports provided are valid.
2307 let handle = unsafe {
2308 allocator
2309 .allocate_module(InstanceAllocationRequest {
2310 id,
2311 runtime_info,
2312 imports,
2313 store: self,
2314 limiter,
2315 })
2316 .await?
2317 };
2318
2319 let actual = match kind {
2320 AllocateInstanceKind::Module(module_id) => {
2321 log::trace!(
2322 "Adding instance to store: store={:?}, module={module_id:?}, instance={id:?}",
2323 self.id()
2324 );
2325 self.instances
2326 .push(StoreInstance {
2327 handle,
2328 kind: StoreInstanceKind::Real { module_id },
2329 })
2330 .expect("capacity was reserved above")
2331 }
2332 AllocateInstanceKind::Dummy { .. } => {
2333 log::trace!(
2334 "Adding dummy instance to store: store={:?}, instance={id:?}",
2335 self.id()
2336 );
2337 self.instances
2338 .push(StoreInstance {
2339 handle,
2340 kind: StoreInstanceKind::Dummy,
2341 })
2342 .expect("capacity was reserved above")
2343 }
2344 };
2345
2346 // double-check we didn't accidentally allocate two instances and our
2347 // prediction of what the id would be is indeed the id it should be.

Callers 5

new_rawMethod · 0.80
try_newMethod · 0.80
create_tagFunction · 0.80
create_memoryFunction · 0.80
create_tableFunction · 0.80

Calls 8

OkFunction · 0.85
allocate_moduleMethod · 0.80
reserveMethod · 0.45
next_keyMethod · 0.45
allocatorMethod · 0.45
engineMethod · 0.45
expectMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected