From 6c3bf0b10f1ac888e1d13c9de22425cb2784d209 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Mon, 20 Jan 2020 14:07:47 +0100 Subject: [PATCH] Implement GlobalAlloc::dealloc --- src/allocator/fixed_size_block.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) 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); + } + } } }