diff --git a/src/allocator/fixed_size_block.rs b/src/allocator/fixed_size_block.rs index c01573af..559199ce 100644 --- a/src/allocator/fixed_size_block.rs +++ b/src/allocator/fixed_size_block.rs @@ -1,6 +1,9 @@ use super::Locked; use alloc::alloc::{GlobalAlloc, Layout}; -use core::ptr; +use core::{ + mem, + ptr::{self, NonNull}, +}; /// The block sizes to use. /// @@ -77,6 +80,23 @@ unsafe impl GlobalAlloc for Locked { } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - todo!(); + let mut allocator = self.lock(); + match list_index(&layout) { + Some(index) => { + let new_node = ListNode { + next: allocator.list_heads[index].take(), + }; + // verify that block has size and alignment required for storing node + assert!(mem::size_of::() <= BLOCK_SIZES[index]); + assert!(mem::align_of::() <= BLOCK_SIZES[index]); + let new_node_ptr = ptr as *mut ListNode; + new_node_ptr.write(new_node); + allocator.list_heads[index] = Some(&mut *new_node_ptr); + } + None => { + let ptr = NonNull::new(ptr).unwrap(); + allocator.fallback_allocator.deallocate(ptr, layout); + } + } } }