Skip to main content

tidepool_codegen/
nursery.rs

1use crate::context::VMContext;
2
3/// Owned bump-allocator nursery for JIT-compiled code.
4///
5/// Provides the backing memory that VMContext's alloc_ptr/alloc_limit point into.
6/// No GC — panics on exhaustion.
7pub struct Nursery {
8    buffer: Vec<u8>,
9}
10
11impl Nursery {
12    /// Create a nursery with the given size in bytes.
13    pub fn new(size: usize) -> Self {
14        Self {
15            buffer: vec![0u8; size],
16        }
17    }
18
19    /// Create a VMContext pointing into this nursery.
20    ///
21    /// The returned VMContext is valid as long as this Nursery is alive
22    /// and not moved.
23    pub fn make_vmctx(&mut self, gc_trigger: extern "C" fn(*mut VMContext)) -> VMContext {
24        let start = self.buffer.as_mut_ptr();
25        let end = unsafe { start.add(self.buffer.len()) };
26        VMContext::new(start, end as *const u8, gc_trigger)
27    }
28}