forked from M-Labs/zynq-rs
1
0
Fork 0

libsupport_zynq/ram: allow single allocator.

This commit is contained in:
pca006132 2020-08-03 14:42:17 +08:00
parent 84041a3154
commit 8328ffc66b
1 changed files with 26 additions and 29 deletions

View File

@ -1,30 +1,33 @@
use alloc::alloc::Layout; use alloc::alloc::Layout;
use core::alloc::GlobalAlloc; use core::alloc::GlobalAlloc;
use core::cell::UnsafeCell;
use core::ptr::NonNull; use core::ptr::NonNull;
use libboard_zynq::ddr::DdrRam; use libcortex_a9::{
use libcortex_a9::regs::MPIDR; mutex::Mutex,
regs::MPIDR
};
use libregister::RegisterR; use libregister::RegisterR;
use linked_list_allocator::Heap; use linked_list_allocator::Heap;
#[cfg(not(feature = "alloc_core"))]
use libboard_zynq::ddr::DdrRam;
#[global_allocator] #[global_allocator]
static mut ALLOCATOR: CortexA9Alloc = CortexA9Alloc( static ALLOCATOR: CortexA9Alloc = CortexA9Alloc(
UnsafeCell::new(Heap::empty()), Mutex::new(Heap::empty()),
UnsafeCell::new(Heap::empty()), Mutex::new(Heap::empty()),
); );
struct CortexA9Alloc(UnsafeCell<Heap>, UnsafeCell<Heap>); struct CortexA9Alloc(Mutex<Heap>, Mutex<Heap>);
unsafe impl Sync for CortexA9Alloc {} unsafe impl Sync for CortexA9Alloc {}
unsafe impl GlobalAlloc for CortexA9Alloc { unsafe impl GlobalAlloc for CortexA9Alloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 { if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 {
self.0.get().as_mut() &self.0
} else { } else {
self.1.get().as_mut() &self.1
} }
.unwrap() .lock()
.allocate_first_fit(layout) .allocate_first_fit(layout)
.ok() .ok()
.map_or(0 as *mut u8, |allocation| allocation.as_ptr()) .map_or(0 as *mut u8, |allocation| allocation.as_ptr())
@ -32,11 +35,11 @@ unsafe impl GlobalAlloc for CortexA9Alloc {
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 { if cfg!(not(feature = "alloc_core")) || MPIDR.read().cpu_id() == 0 {
self.0.get().as_mut() &self.0
} else { } else {
self.1.get().as_mut() &self.1
} }
.unwrap() .lock()
.deallocate(NonNull::new_unchecked(ptr), layout) .deallocate(NonNull::new_unchecked(ptr), layout)
} }
} }
@ -46,27 +49,25 @@ pub fn init_alloc_ddr(ddr: &mut DdrRam) {
unsafe { unsafe {
ALLOCATOR ALLOCATOR
.0 .0
.get() .lock()
.as_mut()
.unwrap()
.init(ddr.ptr::<u8>() as usize, ddr.size()); .init(ddr.ptr::<u8>() as usize, ddr.size());
} }
} }
#[cfg(feature = "alloc_core")]
extern "C" { extern "C" {
static __heap0_start: usize; static __heap0_start: usize;
static __heap0_end: usize; static __heap0_end: usize;
#[cfg(feature = "alloc_core")]
static __heap1_start: usize; static __heap1_start: usize;
#[cfg(feature = "alloc_core")]
static __heap1_end: usize; static __heap1_end: usize;
} }
#[cfg(feature = "alloc_core")]
pub fn init_alloc_core0() { pub fn init_alloc_core0() {
unsafe { unsafe {
let start = &__heap0_start as *const usize as usize; let start = &__heap0_start as *const usize as usize;
let end = &__heap0_end as *const usize as usize; let end = &__heap0_end as *const usize as usize;
ALLOCATOR.0.get().as_mut().unwrap().init(start, end - start); ALLOCATOR.0.lock().init(start, end - start);
} }
} }
@ -75,26 +76,22 @@ pub fn init_alloc_core1() {
unsafe { unsafe {
let start = &__heap1_start as *const usize as usize; let start = &__heap1_start as *const usize as usize;
let end = &__heap1_end as *const usize as usize; let end = &__heap1_end as *const usize as usize;
ALLOCATOR.1.get().as_mut().unwrap().init(start, end - start); ALLOCATOR.1.lock().init(start, end - start);
} }
} }
#[alloc_error_handler] #[alloc_error_handler]
fn alloc_error(layout: core::alloc::Layout) -> ! { fn alloc_error(layout: core::alloc::Layout) -> ! {
let id = MPIDR.read().cpu_id(); let id = MPIDR.read().cpu_id();
let heap = unsafe { let used = if cfg!(not(feature = "alloc_core")) || id == 0 {
if cfg!(not(feature = "alloc_core")) || id == 0 { ALLOCATOR.0.lock().used()
ALLOCATOR.0.get() } else {
} else { ALLOCATOR.1.lock().used()
ALLOCATOR.1.get()
}
.as_mut()
.unwrap()
}; };
panic!( panic!(
"Core {} alloc_error, layout: {:?}, used memory: {}", "Core {} alloc_error, layout: {:?}, used memory: {}",
id, id,
layout, layout,
heap.used() used
); );
} }