forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limits.rs
31 lines (28 loc) · 826 Bytes
/
limits.rs
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
use wasmtime::*;
pub struct StoreLimits {
pub remaining_memory: usize,
pub oom: bool,
}
impl StoreLimits {
fn alloc(&mut self, amt: usize) -> bool {
match self.remaining_memory.checked_sub(amt) {
Some(mem) => {
self.remaining_memory = mem;
true
}
None => {
self.oom = true;
false
}
}
}
}
impl ResourceLimiter for StoreLimits {
fn memory_growing(&mut self, current: usize, desired: usize, _maximum: Option<usize>) -> bool {
self.alloc(desired - current)
}
fn table_growing(&mut self, current: u32, desired: u32, _maximum: Option<u32>) -> bool {
let delta = (desired - current) as usize * std::mem::size_of::<usize>();
self.alloc(delta)
}
}