pub const MEM_BITS: usize = 29;
pub const MEM_SIZE: usize = 1 << MEM_BITS;
pub const GUEST_MIN_MEM: usize = 0x0000_0400;
pub const GUEST_MAX_MEM: usize = MEM_SIZE;
pub const STACK_TOP: u32 = 0x0020_0400;
pub const TEXT_START: u32 = 0x0020_0800;
pub fn is_guest_memory(addr: u32) -> bool {
GUEST_MIN_MEM <= (addr as usize) && (addr as usize) < GUEST_MAX_MEM
}
#[cfg(feature = "rust-runtime")]
#[no_mangle]
pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 {
use crate::print::println;
#[cfg(target_os = "zkvm")]
extern "C" {
static _end: u8;
}
static mut HEAP_POS: usize = 0;
let mut heap_pos = unsafe { HEAP_POS };
#[cfg(target_os = "zkvm")]
if heap_pos == 0 {
heap_pos = unsafe { (&_end) as *const u8 as usize };
}
let align = usize::max(align, super::WORD_SIZE);
let offset = heap_pos & (align - 1);
if offset != 0 {
heap_pos += align - offset;
}
match heap_pos.checked_add(bytes) {
Some(new_heap_pos) if new_heap_pos <= GUEST_MAX_MEM => {
unsafe { HEAP_POS = new_heap_pos };
}
_ => {
println("ERROR: Maximum memory exceeded, program terminating.");
super::rust_rt::terminate::<1>();
}
}
heap_pos as *mut u8
}