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!();
|
||||
}
|
@ -1,7 +1,13 @@
|
||||
//! @brief Solana Rust-based BPF program utility functions and types
|
||||
|
||||
#![no_std]
|
||||
#![feature(allocator_api)]
|
||||
#![feature(alloc_error_handler)]
|
||||
|
||||
pub mod alloc;
|
||||
pub mod entrypoint;
|
||||
pub mod log;
|
||||
pub mod panic;
|
||||
|
||||
#[global_allocator]
|
||||
static A: alloc::Allocator = alloc::Allocator;
|
||||
|
@ -1,6 +1,5 @@
|
||||
//! @brief Solana Rust-based BPF program panic handling
|
||||
|
||||
// #[cfg(not(test))]
|
||||
use crate::log::*;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
@ -18,9 +17,15 @@ fn panic(info: &PanicInfo) -> ! {
|
||||
sol_panic_();
|
||||
}
|
||||
}
|
||||
sol_log_64(0, 0, 0, u64::from(location.line()), u64::from(location.column()));
|
||||
sol_log_64(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
u64::from(location.line()),
|
||||
u64::from(location.column()),
|
||||
);
|
||||
} else {
|
||||
sol_log("Panic! but could not get location information");
|
||||
sol_log("Panic!, but could not get location information");
|
||||
}
|
||||
unsafe {
|
||||
sol_panic_();
|
||||
|
Reference in New Issue
Block a user