use crate::{
sys::{mm_free, mm_realloc, mm_zalloc},
Result,
};
use raw_cstr::raw_cstr;
use std::{alloc::GlobalAlloc, ffi::c_void, mem::transmute};
#[macro_export]
macro_rules! simics_alloc {
($typ:ty, $sz:expr) => {
$crate::api::alloc($sz, stringify!($typ), file!(), line!() as i32)
};
}
pub fn alloc<T, S: AsRef<str>>(
size: usize,
typename: S,
filename: S,
line_number: i32,
) -> Result<*mut T> {
unsafe {
let res = mm_zalloc(
size,
size,
raw_cstr(typename)?,
raw_cstr(filename)?,
line_number,
);
Ok(transmute::<*mut std::ffi::c_void, *mut T>(res))
}
}
pub fn free<T>(ptr: *mut T) {
unsafe { mm_free(ptr as *mut c_void) };
}
pub struct SimicsAlloc;
const SIMICS_ALLOC_TYPE: &[u8; 8] = b"unknown\0";
const SIMICS_ALLOC_FILE: &[u8; 8] = b"unknown\0";
const SIMICS_ALLOC_LINE: i32 = 0;
unsafe impl GlobalAlloc for SimicsAlloc {
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
let size = layout.size();
unsafe {
mm_zalloc(
size,
1,
SIMICS_ALLOC_TYPE.as_ptr() as *const i8,
SIMICS_ALLOC_FILE.as_ptr() as *const i8,
SIMICS_ALLOC_LINE,
) as *mut u8
}
}
unsafe fn alloc_zeroed(&self, layout: std::alloc::Layout) -> *mut u8 {
self.alloc(layout)
}
unsafe fn realloc(
&self,
ptr: *mut u8,
_layout: std::alloc::Layout,
new_size: usize,
) -> *mut u8 {
unsafe {
mm_realloc(
ptr as *mut c_void,
new_size,
1,
SIMICS_ALLOC_TYPE.as_ptr() as *const i8,
SIMICS_ALLOC_FILE.as_ptr() as *const i8,
SIMICS_ALLOC_LINE,
) as *mut u8
}
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: std::alloc::Layout) {
unsafe { mm_free(ptr as *mut c_void) };
}
}