mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 09:23:05 +00:00
std: xous: add alloc support
Basic alloc support on Xous is supported by the `dlmalloc` crate. This necessitates bumping the dlmalloc version to 0.2.4. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
dffc86472e
commit
823cba9cc8
@ -36,8 +36,8 @@ object = { version = "0.32.0", default-features = false, optional = true, featur
|
|||||||
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
|
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
|
||||||
rand_xorshift = "0.3.0"
|
rand_xorshift = "0.3.0"
|
||||||
|
|
||||||
[target.'cfg(any(all(target_family = "wasm", target_os = "unknown"), all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
|
[target.'cfg(any(all(target_family = "wasm", target_os = "unknown"), target_os = "xous", all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
|
||||||
dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }
|
dlmalloc = { version = "0.2.4", features = ['rustc-dep-of-std'] }
|
||||||
|
|
||||||
[target.x86_64-fortanix-unknown-sgx.dependencies]
|
[target.x86_64-fortanix-unknown-sgx.dependencies]
|
||||||
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public = true }
|
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public = true }
|
||||||
|
62
library/std/src/sys/xous/alloc.rs
Normal file
62
library/std/src/sys/xous/alloc.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use crate::alloc::{GlobalAlloc, Layout, System};
|
||||||
|
|
||||||
|
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new();
|
||||||
|
|
||||||
|
#[stable(feature = "alloc_system_type", since = "1.28.0")]
|
||||||
|
unsafe impl GlobalAlloc for System {
|
||||||
|
#[inline]
|
||||||
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
|
||||||
|
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
|
||||||
|
let _lock = lock::lock();
|
||||||
|
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||||
|
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
|
||||||
|
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
|
||||||
|
let _lock = lock::lock();
|
||||||
|
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
|
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
|
||||||
|
// Calling free() is safe because preconditions on this function match the trait method preconditions.
|
||||||
|
let _lock = lock::lock();
|
||||||
|
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||||
|
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
|
||||||
|
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
|
||||||
|
let _lock = lock::lock();
|
||||||
|
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod lock {
|
||||||
|
use crate::sync::atomic::{AtomicI32, Ordering::SeqCst};
|
||||||
|
|
||||||
|
static LOCKED: AtomicI32 = AtomicI32::new(0);
|
||||||
|
|
||||||
|
pub struct DropLock;
|
||||||
|
|
||||||
|
pub fn lock() -> DropLock {
|
||||||
|
loop {
|
||||||
|
if LOCKED.swap(1, SeqCst) == 0 {
|
||||||
|
return DropLock;
|
||||||
|
}
|
||||||
|
crate::os::xous::ffi::do_yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for DropLock {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let r = LOCKED.swap(0, SeqCst);
|
||||||
|
debug_assert_eq!(r, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
#![deny(unsafe_op_in_unsafe_fn)]
|
#![deny(unsafe_op_in_unsafe_fn)]
|
||||||
|
|
||||||
#[path = "../unsupported/alloc.rs"]
|
|
||||||
pub mod alloc;
|
pub mod alloc;
|
||||||
#[path = "../unsupported/args.rs"]
|
#[path = "../unsupported/args.rs"]
|
||||||
pub mod args;
|
pub mod args;
|
||||||
|
Loading…
Reference in New Issue
Block a user