1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::vmcontext::VMSharedSignatureIndex;
use loupe::MemoryUsage;
use more_asserts::{assert_lt, debug_assert_lt};
use std::collections::{hash_map, HashMap};
use std::convert::TryFrom;
use std::sync::RwLock;
use wasmer_types::FunctionType;
#[derive(Debug, MemoryUsage)]
pub struct SignatureRegistry {
inner: RwLock<Inner>,
}
#[derive(Debug, Default, MemoryUsage)]
struct Inner {
signature2index: HashMap<FunctionType, VMSharedSignatureIndex>,
index2signature: HashMap<VMSharedSignatureIndex, FunctionType>,
}
impl SignatureRegistry {
pub fn new() -> Self {
Self {
inner: Default::default(),
}
}
pub fn register(&self, sig: &FunctionType) -> VMSharedSignatureIndex {
let mut inner = self.inner.write().unwrap();
let len = inner.signature2index.len();
match inner.signature2index.entry(sig.clone()) {
hash_map::Entry::Occupied(entry) => *entry.get(),
hash_map::Entry::Vacant(entry) => {
debug_assert_lt!(
len,
std::u32::MAX as usize,
"Invariant check: signature_hash.len() < std::u32::MAX"
);
let sig_id = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
entry.insert(sig_id);
inner.index2signature.insert(sig_id, sig.clone());
sig_id
}
}
}
pub fn lookup(&self, idx: VMSharedSignatureIndex) -> Option<FunctionType> {
self.inner
.read()
.unwrap()
.index2signature
.get(&idx)
.cloned()
}
}