Skip to main content

tidepool_codegen/
context.rs

1use std::mem;
2
3/// VM context passed as implicit first argument to all JIT-compiled functions.
4///
5/// Layout is frozen: gc_trigger reads fields by offset.
6/// alloc_ptr at 0, alloc_limit at 8, gc_trigger at 16.
7#[repr(C, align(16))]
8pub struct VMContext {
9    /// Current bump-pointer allocation cursor.
10    pub alloc_ptr: *mut u8,
11    /// End of the current nursery region.
12    pub alloc_limit: *const u8,
13    /// Host function called when alloc_ptr exceeds alloc_limit.
14    pub gc_trigger: extern "C" fn(*mut VMContext),
15}
16
17impl VMContext {
18    /// Create a new VMContext with the given nursery region and GC trigger.
19    pub fn new(
20        nursery_start: *mut u8,
21        nursery_end: *const u8,
22        gc_trigger: extern "C" fn(*mut VMContext),
23    ) -> Self {
24        Self {
25            alloc_ptr: nursery_start,
26            alloc_limit: nursery_end,
27            gc_trigger,
28        }
29    }
30}
31
32// Compile-time offset assertions
33const _: () = {
34    assert!(mem::offset_of!(VMContext, alloc_ptr) == 0);
35    assert!(mem::offset_of!(VMContext, alloc_limit) == 8);
36    assert!(mem::offset_of!(VMContext, gc_trigger) == 16);
37    assert!(mem::align_of::<VMContext>() == 16);
38};