rune_alloc/limit/
std.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 core::cell::Cell;

std::thread_local!(static MEMORY: Cell<usize> = const { Cell::new(usize::MAX) });

pub(super) fn rune_memory_take(amount: usize) -> bool {
    MEMORY.with(|tls| {
        let v = tls.get();

        if v >= amount {
            tls.set(v.wrapping_sub(amount));
            true
        } else {
            false
        }
    })
}

pub(super) fn rune_memory_release(amount: usize) {
    MEMORY.with(|tls| {
        let v = tls.get();
        tls.set(v.saturating_add(amount));
    })
}

pub(super) fn rune_memory_get() -> usize {
    MEMORY.with(|tls| tls.get())
}

pub(super) fn rune_memory_replace(value: usize) -> usize {
    MEMORY.with(|tls| tls.replace(value))
}