pvmfw: heap: Implement malloc() and free()
Implement the function pair on top of our global_allocator, for C
dependencies.
Bug: 256148034
Bug: 256827715
Test: -
Change-Id: I800fc26a8b9b3656a13ffba083ee7f96ca86d879
diff --git a/pvmfw/src/heap.rs b/pvmfw/src/heap.rs
index bfa8320..eab3bc4 100644
--- a/pvmfw/src/heap.rs
+++ b/pvmfw/src/heap.rs
@@ -14,6 +14,14 @@
//! Heap implementation.
+use core::alloc::GlobalAlloc as _;
+use core::alloc::Layout;
+use core::ffi::c_void;
+use core::mem;
+use core::num::NonZeroUsize;
+use core::ptr;
+use core::ptr::NonNull;
+
use buddy_system_allocator::LockedHeap;
#[global_allocator]
@@ -24,3 +32,32 @@
pub unsafe fn init() {
HEAP_ALLOCATOR.lock().init(HEAP.as_mut_ptr() as usize, HEAP.len());
}
+
+#[no_mangle]
+unsafe extern "C" fn malloc(size: usize) -> *mut c_void {
+ malloc_(size).map_or(ptr::null_mut(), |p| p.cast::<c_void>().as_ptr())
+}
+
+#[no_mangle]
+unsafe extern "C" fn free(ptr: *mut c_void) {
+ if let Some(ptr) = NonNull::new(ptr).map(|p| p.cast::<usize>().as_ptr().offset(-1)) {
+ if let Some(size) = NonZeroUsize::new(*ptr) {
+ if let Some(layout) = malloc_layout(size) {
+ HEAP_ALLOCATOR.dealloc(ptr as *mut u8, layout);
+ }
+ }
+ }
+}
+
+unsafe fn malloc_(size: usize) -> Option<NonNull<usize>> {
+ let size = NonZeroUsize::new(size)?.checked_add(mem::size_of::<usize>())?;
+ let ptr = HEAP_ALLOCATOR.alloc(malloc_layout(size)?);
+ let ptr = NonNull::new(ptr)?.cast::<usize>().as_ptr();
+ *ptr = size.get();
+ NonNull::new(ptr.offset(1))
+}
+
+fn malloc_layout(size: NonZeroUsize) -> Option<Layout> {
+ const ALIGN: usize = mem::size_of::<u64>();
+ Layout::from_size_align(size.get(), ALIGN).ok()
+}