Add rust bpf allocator (#4426)
This commit is contained in:
23
sdk/bpf/rust-utils/src/alloc.rs
Normal file
23
sdk/bpf/rust-utils/src/alloc.rs
Normal file
@ -0,0 +1,23 @@
|
||||
//! @brief Solana Rust-based BPF program memory allocator shim
|
||||
|
||||
use crate::log::*;
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
|
||||
pub struct Allocator;
|
||||
unsafe impl GlobalAlloc for Allocator {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
sol_alloc_free_(layout.size() as u64, 0)
|
||||
}
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
sol_alloc_free_(layout.size() as u64, ptr as u64);
|
||||
}
|
||||
}
|
||||
extern "C" {
|
||||
fn sol_alloc_free_(size: u64, ptr: u64) -> *mut u8;
|
||||
}
|
||||
|
||||
#[alloc_error_handler]
|
||||
fn my_alloc_error_handler(_: core::alloc::Layout) -> ! {
|
||||
sol_log("alloc_error_handler");
|
||||
panic!();
|
||||
}
|
Reference in New Issue
Block a user