From 61d8b0ff32d55a4828dc8b32c152606bc4556c8e Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jeremy@system76.com> Date: Wed, 27 Oct 2021 20:29:00 -0600 Subject: [PATCH] Fix allocator race condition --- src/allocator/linked_list.rs | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index aaa3a078..e7a371fc 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -17,32 +17,17 @@ impl Allocator { unsafe impl GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - loop { - let res = if let Some(ref mut heap) = *HEAP.lock() { - heap.allocate_first_fit(layout) - } else { - panic!("__rust_allocate: heap not initialized"); - }; - - match res { + while let Some(ref mut heap) = *HEAP.lock() { + match heap.allocate_first_fit(layout) { Err(()) => { - let size = if let Some(ref heap) = *HEAP.lock() { - heap.size() - } else { - panic!("__rust_allocate: heap not initialized"); - }; - + let size = heap.size(); super::map_heap(&mut ActivePageTable::new(TableKind::Kernel), crate::KERNEL_HEAP_OFFSET + size, crate::KERNEL_HEAP_SIZE); - - if let Some(ref mut heap) = *HEAP.lock() { - heap.extend(crate::KERNEL_HEAP_SIZE); - } else { - panic!("__rust_allocate: heap not initialized"); - } + heap.extend(crate::KERNEL_HEAP_SIZE); }, other => return other.ok().map_or(ptr::null_mut(), |allocation| allocation.as_ptr()), } } + panic!("__rust_allocate: heap not initialized"); } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { -- GitLab