1
0
Fork 0
zynq-rs/src/ram.rs

42 lines
1.0 KiB
Rust
Raw Normal View History

2019-11-11 09:37:06 +08:00
use core::alloc::GlobalAlloc;
use core::ptr::NonNull;
use alloc::alloc::Layout;
use linked_list_allocator::Heap;
use crate::cortex_a9::mutex::Mutex;
2019-11-11 09:37:06 +08:00
use crate::zynq::ddr::DdrRam;
#[global_allocator]
static ALLOCATOR: CortexA9Alloc = CortexA9Alloc(Mutex::new(Heap::empty()));
2019-11-11 09:37:06 +08:00
/// LockedHeap doesn't locking properly
struct CortexA9Alloc(Mutex<Heap>);
2019-11-11 09:37:06 +08:00
unsafe impl Sync for CortexA9Alloc {}
2019-11-11 09:37:06 +08:00
unsafe impl GlobalAlloc for CortexA9Alloc {
2019-11-11 09:37:06 +08:00
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.0.lock()
2019-11-11 09:37:06 +08:00
.allocate_first_fit(layout)
.ok()
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.0.lock()
2019-11-11 09:37:06 +08:00
.deallocate(NonNull::new_unchecked(ptr), layout)
}
}
pub fn init_alloc(ddr: &mut DdrRam) {
unsafe {
ALLOCATOR.0.lock()
2019-11-11 09:37:06 +08:00
.init(ddr.ptr::<u8>() as usize, ddr.size());
}
}
#[alloc_error_handler]
fn alloc_error(_: core::alloc::Layout) -> ! {
panic!("alloc_error")
}