wasmtime/runtime/store/
data.rs1use crate::module::ModuleRegistry;
2use crate::prelude::*;
3use crate::runtime::vm::{self, VMStore};
4use crate::store::StoreOpaque;
5use crate::{Engine, StoreContext, StoreContextMut};
6use core::num::NonZeroU64;
7use core::ops::{Index, IndexMut};
8use core::pin::Pin;
9
10#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub struct InstanceId(u32);
16wasmtime_environ::entity_impl!(InstanceId);
17
18pub struct StoreData {
19 id: StoreId,
20 #[cfg(feature = "component-model")]
21 pub(crate) components: crate::component::ComponentStoreData,
22}
23
24impl StoreData {
25 pub fn new(engine: &Engine) -> Result<StoreData> {
26 #[cfg(not(feature = "component-model"))]
27 let _ = engine;
28 Ok(StoreData {
29 id: StoreId::allocate(),
30 #[cfg(feature = "component-model")]
31 components: crate::component::ComponentStoreData::new(engine)?,
32 })
33 }
34
35 pub fn id(&self) -> StoreId {
36 self.id
37 }
38
39 pub fn run_manual_drop_routines<T>(store: StoreContextMut<T>) {
40 #[cfg(feature = "component-model")]
41 crate::component::ComponentStoreData::run_manual_drop_routines(store);
42 #[cfg(not(feature = "component-model"))]
43 let _ = store;
44 }
45
46 pub fn decrement_allocator_resources(&mut self, allocator: &dyn vm::InstanceAllocator) {
47 #[cfg(feature = "component-model")]
48 self.components.decrement_allocator_resources(allocator);
49 #[cfg(not(feature = "component-model"))]
50 let _ = allocator;
51 }
52
53 pub unsafe fn publish_to_vm_store_context(
61 &mut self,
62 vm_store_context: &mut vm::VMStoreContext,
63 ) {
64 #[cfg(feature = "component-model")]
66 unsafe {
67 self.components.publish_lazy_lenders(vm_store_context);
68 }
69 #[cfg(not(feature = "component-model"))]
70 let _ = vm_store_context;
71 }
72}
73
74impl<I> Index<I> for StoreOpaque
76where
77 StoreData: Index<I>,
78{
79 type Output = <StoreData as Index<I>>::Output;
80
81 #[inline]
82 fn index(&self, index: I) -> &Self::Output {
83 self.store_data.index(index)
84 }
85}
86
87impl<I> IndexMut<I> for StoreOpaque
88where
89 StoreData: IndexMut<I>,
90{
91 #[inline]
92 fn index_mut(&mut self, index: I) -> &mut Self::Output {
93 self.store_data.index_mut(index)
94 }
95}
96
97impl<I, T> Index<I> for StoreContext<'_, T>
99where
100 StoreOpaque: Index<I>,
101{
102 type Output = <StoreOpaque as Index<I>>::Output;
103
104 #[inline]
105 fn index(&self, index: I) -> &Self::Output {
106 self.0.index(index)
107 }
108}
109
110impl<I, T> Index<I> for StoreContextMut<'_, T>
112where
113 StoreOpaque: Index<I>,
114{
115 type Output = <StoreOpaque as Index<I>>::Output;
116
117 #[inline]
118 fn index(&self, index: I) -> &Self::Output {
119 self.0.index(index)
120 }
121}
122
123impl<I, T> IndexMut<I> for StoreContextMut<'_, T>
124where
125 StoreOpaque: IndexMut<I>,
126{
127 #[inline]
128 fn index_mut(&mut self, index: I) -> &mut Self::Output {
129 self.0.index_mut(index)
130 }
131}
132
133impl<I> Index<I> for dyn VMStore + '_
135where
136 StoreOpaque: Index<I>,
137{
138 type Output = <StoreOpaque as Index<I>>::Output;
139
140 fn index(&self, index: I) -> &Self::Output {
141 (**self).index(index)
142 }
143}
144
145impl<I> IndexMut<I> for dyn VMStore + '_
146where
147 StoreOpaque: IndexMut<I>,
148{
149 fn index_mut(&mut self, index: I) -> &mut Self::Output {
150 (**self).index_mut(index)
151 }
152}
153
154#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
162#[repr(transparent)] pub struct StoreId(NonZeroU64);
164
165impl StoreId {
166 pub fn allocate() -> StoreId {
169 const OVERFLOW_THRESHOLD: u64 = 1 << 63;
175
176 #[cfg(target_has_atomic = "64")]
177 let id = {
178 use core::sync::atomic::{AtomicU64, Ordering::Relaxed};
179
180 static NEXT_ID: AtomicU64 = AtomicU64::new(0);
184 let id = NEXT_ID.fetch_add(1, Relaxed);
185 if id > OVERFLOW_THRESHOLD {
186 NEXT_ID.store(OVERFLOW_THRESHOLD, Relaxed);
187 panic!("store id allocator overflow");
188 }
189 id
190 };
191
192 #[cfg(not(target_has_atomic = "64"))]
197 let id = {
198 use crate::sync::RwLock;
199 static NEXT_ID: RwLock<u64> = RwLock::new(0);
200
201 let mut lock = NEXT_ID.write();
202 if *lock > OVERFLOW_THRESHOLD {
203 panic!("store id allocator overflow");
204 }
205 let ret = *lock;
206 *lock += 1;
207 ret
208 };
209
210 StoreId(NonZeroU64::new(id + 1).unwrap())
211 }
212
213 #[inline]
214 pub fn assert_belongs_to(&self, store: StoreId) {
215 if *self == store {
216 return;
217 }
218 store_id_mismatch();
219 }
220
221 pub fn as_raw(&self) -> NonZeroU64 {
223 self.0
224 }
225
226 pub fn from_raw(id: NonZeroU64) -> StoreId {
228 StoreId(id)
229 }
230}
231
232#[cold]
233fn store_id_mismatch() {
234 panic!("object used with the wrong store");
235}
236
237#[repr(C)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
247pub struct StoreInstanceId {
248 store_id: StoreId,
249 instance: InstanceId,
250}
251
252impl StoreInstanceId {
253 pub(crate) fn new(store_id: StoreId, instance: InstanceId) -> StoreInstanceId {
254 StoreInstanceId { store_id, instance }
255 }
256
257 #[inline]
258 pub fn assert_belongs_to(&self, store: StoreId) {
259 self.store_id.assert_belongs_to(store)
260 }
261
262 #[inline]
263 pub fn store_id(&self) -> StoreId {
264 self.store_id
265 }
266
267 #[inline]
268 pub(crate) fn instance(&self) -> InstanceId {
269 self.instance
270 }
271
272 #[inline]
278 pub(crate) fn get<'a>(&self, store: &'a StoreOpaque) -> &'a vm::Instance {
279 self.assert_belongs_to(store.id());
280 store.instance(self.instance)
281 }
282
283 #[inline]
289 pub(crate) fn get_mut<'a>(&self, store: &'a mut StoreOpaque) -> Pin<&'a mut vm::Instance> {
290 self.assert_belongs_to(store.id());
291 store.instance_mut(self.instance)
292 }
293
294 #[inline]
301 pub(crate) fn get_mut_and_module_registry<'a>(
302 &self,
303 store: &'a mut StoreOpaque,
304 ) -> (Pin<&'a mut vm::Instance>, &'a ModuleRegistry) {
305 self.assert_belongs_to(store.id());
306 store.instance_and_module_registry_mut(self.instance)
307 }
308}
309
310impl Index<StoreInstanceId> for StoreOpaque {
311 type Output = vm::Instance;
312
313 #[inline]
314 fn index(&self, id: StoreInstanceId) -> &Self::Output {
315 id.get(self)
316 }
317}