From 6ffcf7d4a4a8bfb549517aa68e9609b03eabcfaf Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 20 Nov 2019 17:25:30 +0100 Subject: [PATCH] ram: lock for concurrent use this may be reverted if ram allocation shall be more separate. --- src/ram.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/ram.rs b/src/ram.rs index 1d932e1..e03e52a 100644 --- a/src/ram.rs +++ b/src/ram.rs @@ -1,36 +1,35 @@ -use core::cell::RefCell; use core::alloc::GlobalAlloc; use core::ptr::NonNull; use alloc::alloc::Layout; use linked_list_allocator::Heap; +use crate::cortex_a9::mutex::Mutex; use crate::zynq::ddr::DdrRam; #[global_allocator] -static ALLOCATOR: HeapAlloc = HeapAlloc(RefCell::new(Heap::empty())); +static ALLOCATOR: CortexA9Alloc = CortexA9Alloc(Mutex::new(Heap::empty())); /// LockedHeap doesn't locking properly -struct HeapAlloc(RefCell); +struct CortexA9Alloc(Mutex); -/// FIXME: unsound; lock properly -unsafe impl Sync for HeapAlloc {} +unsafe impl Sync for CortexA9Alloc {} -unsafe impl GlobalAlloc for HeapAlloc { +unsafe impl GlobalAlloc for CortexA9Alloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - self.0.borrow_mut() + self.0.lock() .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.borrow_mut() + self.0.lock() .deallocate(NonNull::new_unchecked(ptr), layout) } } pub fn init_alloc(ddr: &mut DdrRam) { unsafe { - ALLOCATOR.0.borrow_mut() + ALLOCATOR.0.lock() .init(ddr.ptr::() as usize, ddr.size()); } }