diff --git a/library/std/src/sys/pal/zkvm/abi.rs b/library/std/src/sys/pal/zkvm/abi.rs index 2c73268f555..e0a4d990c5c 100644 --- a/library/std/src/sys/pal/zkvm/abi.rs +++ b/library/std/src/sys/pal/zkvm/abi.rs @@ -1,3 +1,3 @@ extern "C" { - pub static _HEAP_PTR: u8; + pub fn sys_alloc(size: usize, align: usize) -> *mut u8; } diff --git a/library/std/src/sys/pal/zkvm/alloc.rs b/library/std/src/sys/pal/zkvm/alloc.rs index 76fe1212841..56904d4dbc2 100644 --- a/library/std/src/sys/pal/zkvm/alloc.rs +++ b/library/std/src/sys/pal/zkvm/alloc.rs @@ -1,51 +1,11 @@ -use crate::{alloc::{GlobalAlloc, Layout, System}, cell::UnsafeCell}; -use super::abi::_HEAP_PTR; - -static mut BUMP_ALLOC: BumpAllocator = BumpAllocator::new(); +use crate::alloc::{GlobalAlloc, Layout, System}; +use super::abi::sys_alloc; #[stable(feature = "alloc_system_type", since = "1.28.0")] unsafe impl GlobalAlloc for System { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - BUMP_ALLOC.alloc(layout) - } - - unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} -} - -pub struct BumpAllocator { - offset: UnsafeCell, -} - -impl BumpAllocator { - pub const fn new() -> Self { - Self { - offset: UnsafeCell::new(0), - } - } - - pub fn free_memory(&self) -> usize { - heap_start() + (self.offset.get() as usize) - } -} - -unsafe impl GlobalAlloc for BumpAllocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - let alloc_start = align_up(self.free_memory(), layout.align()); - let alloc_end = alloc_start + layout.size(); - *self.offset.get() = alloc_end - self.free_memory(); - - alloc_start as *mut u8 + sys_alloc(layout.size(), layout.align()) } unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} } - -unsafe impl Sync for BumpAllocator {} - -fn align_up(addr: usize, align: usize) -> usize { - (addr + align - 1) & !(align - 1) -} - -fn heap_start() -> usize { - unsafe { _HEAP_PTR as *const u8 as usize } -}