diff --git a/agb/src/agb_alloc/block_allocator.rs b/agb/src/agb_alloc/block_allocator.rs index 1d2221ae..2c60114a 100644 --- a/agb/src/agb_alloc/block_allocator.rs +++ b/agb/src/agb_alloc/block_allocator.rs @@ -76,7 +76,7 @@ impl BlockAllocator { } /// Requests a brand new block from the inner bump allocator - fn new_block(&self, layout: Layout, cs: &CriticalSection) -> *mut u8 { + fn new_block(&self, layout: Layout, cs: &CriticalSection) -> Option> { let overall_layout = Block::either_layout(layout); self.inner_allocator.alloc_critical(overall_layout, cs) } @@ -111,10 +111,8 @@ impl BlockAllocator { } }); } -} -unsafe impl GlobalAlloc for BlockAllocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + unsafe fn alloc(&self, layout: Layout) -> Option> { // find a block that this current request fits in let full_layout = Block::either_layout(layout); @@ -133,7 +131,7 @@ unsafe impl GlobalAlloc for BlockAllocator { let curr_block = curr.as_mut(); if curr_block.size == full_layout.size() { *list_ptr = curr_block.next; - return curr.as_ptr().cast(); + return Some(curr.cast()); } else if curr_block.size >= block_after_layout.size() { // can split block let split_block = Block { @@ -148,7 +146,7 @@ unsafe impl GlobalAlloc for BlockAllocator { *split_ptr = split_block; *list_ptr = NonNull::new(split_ptr).map(SendNonNull); - return curr.as_ptr().cast(); + return Some(curr.cast()); } current_block = curr_block.next; list_ptr = &mut curr_block.next; @@ -200,3 +198,16 @@ unsafe impl GlobalAlloc for BlockAllocator { self.normalise(); } } + +unsafe impl GlobalAlloc for BlockAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + match self.alloc(layout) { + None => core::ptr::null_mut(), + Some(p) => p.as_ptr(), + } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.dealloc(ptr, layout); + } +} diff --git a/agb/src/agb_alloc/bump_allocator.rs b/agb/src/agb_alloc/bump_allocator.rs index 5b6ca15b..0be21bdd 100644 --- a/agb/src/agb_alloc/bump_allocator.rs +++ b/agb/src/agb_alloc/bump_allocator.rs @@ -28,7 +28,7 @@ impl BumpAllocator { } impl BumpAllocator { - pub fn alloc_critical(&self, layout: Layout, cs: &CriticalSection) -> *mut u8 { + pub fn alloc_critical(&self, layout: Layout, cs: &CriticalSection) -> Option> { let mut current_ptr = self.current_ptr.borrow(*cs).borrow_mut(); let ptr = if let Some(c) = *current_ptr { @@ -46,21 +46,24 @@ impl BumpAllocator { let new_current_ptr = resulting_ptr + layout.size(); if new_current_ptr as usize >= self.start_end.borrow(*cs).end.0() { - return core::ptr::null_mut(); + return None; } *current_ptr = NonNull::new(new_current_ptr as *mut _).map(SendNonNull); - resulting_ptr as *mut _ + NonNull::new(resulting_ptr as *mut _) } - pub fn alloc_safe(&self, layout: Layout) -> *mut u8 { + pub fn alloc_safe(&self, layout: Layout) -> Option> { free(|key| self.alloc_critical(layout, key)) } } unsafe impl GlobalAlloc for BumpAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - self.alloc_safe(layout) + match self.alloc_safe(layout) { + None => core::ptr::null_mut(), + Some(p) => p.as_ptr(), + } } unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}