2020-07-20 11:09:04 +08:00
|
|
|
use alloc::alloc::Layout;
|
2019-11-11 09:37:06 +08:00
|
|
|
use core::alloc::GlobalAlloc;
|
|
|
|
use core::ptr::NonNull;
|
2020-08-03 14:42:17 +08:00
|
|
|
use libcortex_a9::{
|
|
|
|
mutex::Mutex,
|
|
|
|
regs::MPIDR
|
|
|
|
};
|
2020-07-20 11:09:04 +08:00
|
|
|
use libregister::RegisterR;
|
|
|
|
use linked_list_allocator::Heap;
|
2020-08-03 14:42:17 +08:00
|
|
|
#[cfg(not(feature = "alloc_core"))]
|
|
|
|
use libboard_zynq::ddr::DdrRam;
|
2019-11-11 09:37:06 +08:00
|
|
|
|
|
|
|
#[global_allocator]
|
2020-08-03 14:42:17 +08:00
|
|
|
static ALLOCATOR: CortexA9Alloc = CortexA9Alloc(
|
|
|
|
Mutex::new(Heap::empty()),
|
|
|
|
Mutex::new(Heap::empty()),
|
2020-07-20 11:09:04 +08:00
|
|
|
);
|
2019-11-11 09:37:06 +08:00
|
|
|
|
2020-08-03 14:42:17 +08:00
|
|
|
struct CortexA9Alloc(Mutex<Heap>, Mutex<Heap>);
|
2019-11-11 09:37:06 +08:00
|
|
|
|
2019-11-21 00:25:30 +08:00
|
|
|
unsafe impl Sync for CortexA9Alloc {}
|
2019-11-11 09:37:06 +08:00
|
|
|
|
2019-11-21 00:25:30 +08:00
|
|
|
unsafe impl GlobalAlloc for CortexA9Alloc {
|
2019-11-11 09:37:06 +08:00
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
2020-07-28 12:20:25 +08:00
|
|
|
if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 {
|
2020-08-03 14:42:17 +08:00
|
|
|
&self.0
|
2020-07-20 11:09:04 +08:00
|
|
|
} else {
|
2020-08-03 14:42:17 +08:00
|
|
|
&self.1
|
2020-07-20 11:09:04 +08:00
|
|
|
}
|
2020-08-03 14:42:17 +08:00
|
|
|
.lock()
|
2020-07-20 11:09:04 +08:00
|
|
|
.allocate_first_fit(layout)
|
|
|
|
.ok()
|
|
|
|
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
|
2019-11-11 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
2020-07-28 12:20:25 +08:00
|
|
|
if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 {
|
2020-08-03 14:42:17 +08:00
|
|
|
&self.0
|
2020-07-20 11:09:04 +08:00
|
|
|
} else {
|
2020-08-03 14:42:17 +08:00
|
|
|
&self.1
|
2020-07-20 11:09:04 +08:00
|
|
|
}
|
2020-08-03 14:42:17 +08:00
|
|
|
.lock()
|
2020-07-20 11:09:04 +08:00
|
|
|
.deallocate(NonNull::new_unchecked(ptr), layout)
|
2019-11-11 09:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:09:04 +08:00
|
|
|
#[cfg(not(feature = "alloc_core"))]
|
2020-04-27 10:06:55 +08:00
|
|
|
pub fn init_alloc_ddr(ddr: &mut DdrRam) {
|
2019-11-11 09:37:06 +08:00
|
|
|
unsafe {
|
2020-07-20 11:09:04 +08:00
|
|
|
ALLOCATOR
|
|
|
|
.0
|
2020-08-03 14:42:17 +08:00
|
|
|
.lock()
|
2019-11-11 09:37:06 +08:00
|
|
|
.init(ddr.ptr::<u8>() as usize, ddr.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:06:55 +08:00
|
|
|
extern "C" {
|
2020-07-20 11:09:04 +08:00
|
|
|
static __heap0_start: usize;
|
|
|
|
static __heap0_end: usize;
|
2020-08-03 14:42:17 +08:00
|
|
|
#[cfg(feature = "alloc_core")]
|
2020-07-20 11:09:04 +08:00
|
|
|
static __heap1_start: usize;
|
2020-08-03 14:42:17 +08:00
|
|
|
#[cfg(feature = "alloc_core")]
|
2020-07-20 11:09:04 +08:00
|
|
|
static __heap1_end: usize;
|
2020-04-27 10:06:55 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 11:09:04 +08:00
|
|
|
pub fn init_alloc_core0() {
|
2020-04-27 10:06:55 +08:00
|
|
|
unsafe {
|
2020-07-20 11:09:04 +08:00
|
|
|
let start = &__heap0_start as *const usize as usize;
|
|
|
|
let end = &__heap0_end as *const usize as usize;
|
2020-08-03 14:42:17 +08:00
|
|
|
ALLOCATOR.0.lock().init(start, end - start);
|
2020-04-27 10:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:09:04 +08:00
|
|
|
#[cfg(feature = "alloc_core")]
|
|
|
|
pub fn init_alloc_core1() {
|
|
|
|
unsafe {
|
|
|
|
let start = &__heap1_start as *const usize as usize;
|
|
|
|
let end = &__heap1_end as *const usize as usize;
|
2020-08-03 14:42:17 +08:00
|
|
|
ALLOCATOR.1.lock().init(start, end - start);
|
2020-07-20 11:09:04 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-11 09:37:06 +08:00
|
|
|
|
|
|
|
#[alloc_error_handler]
|
2020-07-20 11:09:04 +08:00
|
|
|
fn alloc_error(layout: core::alloc::Layout) -> ! {
|
|
|
|
let id = MPIDR.read().cpu_id();
|
2020-08-03 14:42:17 +08:00
|
|
|
let used = if cfg!(not(feature = "alloc_core")) || id == 0 {
|
|
|
|
ALLOCATOR.0.lock().used()
|
|
|
|
} else {
|
|
|
|
ALLOCATOR.1.lock().used()
|
2020-07-20 11:09:04 +08:00
|
|
|
};
|
|
|
|
panic!(
|
|
|
|
"Core {} alloc_error, layout: {:?}, used memory: {}",
|
|
|
|
id,
|
|
|
|
layout,
|
2020-08-03 14:42:17 +08:00
|
|
|
used
|
2020-07-20 11:09:04 +08:00
|
|
|
);
|
2019-11-11 09:37:06 +08:00
|
|
|
}
|