Further improve OS support

This commit is contained in:
Mathieu Strypsteen 2024-12-24 22:08:05 +01:00
parent 606b8f634b
commit 36471393dc
4 changed files with 37 additions and 0 deletions

View file

@ -90,5 +90,7 @@ cfg_if::cfg_if! {
mod xous;
} else if #[cfg(target_os = "zkvm")] {
mod zkvm;
} else if #[cfg(target_os = "os")] {
mod os;
}
}

View file

@ -0,0 +1,32 @@
use crate::{
alloc::{GlobalAlloc, Layout, System},
ptr,
sync::atomic::{AtomicUsize, Ordering},
};
#[repr(align(16))]
struct HeapData([u8; 4 * 1024 * 1024]);
static mut HEAP_DATA: HeapData = HeapData([0; 4 * 1024 * 1024]);
static HEAP_USED: AtomicUsize = AtomicUsize::new(0);
#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if layout.align() > 16 {
return ptr::null_mut();
}
let num_blocks =
if layout.size() % 16 == 0 { layout.size() / 16 } else { (layout.size() / 16) + 1 };
HEAP_USED.fetch_add(num_blocks, Ordering::Relaxed);
let ptr = unsafe {
ptr::addr_of_mut!(HEAP_DATA.0[HEAP_USED.load(Ordering::Relaxed) - num_blocks])
as *mut u8
};
ptr
}
#[inline]
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

View file

@ -73,6 +73,7 @@ cfg_if::cfg_if! {
} else if #[cfg(any(
all(target_family = "wasm", target_os = "unknown"),
target_os = "xous",
target_os = "os",
))] {
// FIXME: finally remove std support for wasm32-unknown-unknown
// FIXME: add random data generation to xous
@ -86,6 +87,7 @@ cfg_if::cfg_if! {
target_os = "android",
all(target_family = "wasm", target_os = "unknown"),
target_os = "xous",
target_os = "os",
)))]
pub fn hashmap_random_keys() -> (u64, u64) {
let mut buf = [0; 16];

View file

@ -90,6 +90,7 @@ pub(crate) mod guard {
target_family = "wasm",
target_os = "uefi",
target_os = "zkvm",
target_os = "os",
))] {
pub(crate) fn enable() {
// FIXME: Right now there is no concept of "thread exit" on