diff --git a/Cargo.lock b/Cargo.lock index 774cca321e..98135e7b27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -353,7 +353,6 @@ dependencies = [ "hermit-entry", "hermit-sync", "include-transformed", - "linked_list_allocator", "lock_api", "log", "multiboot", @@ -367,6 +366,7 @@ dependencies = [ "rand_chacha", "shell-words", "smoltcp", + "talc", "time", "tock-registers", "uart_16550", @@ -375,12 +375,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "linked_list_allocator" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" - [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -854,6 +848,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "talc" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f5a67558ec5b9d8f6356bdf89b3b9087c705d6aed2196457fb3c0979e57fd9f" + [[package]] name = "tempfile" version = "3.6.0" diff --git a/Cargo.toml b/Cargo.toml index e4a00ce313..5777b91819 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,7 +76,6 @@ hashbrown = { version = "0.14", default-features = false } hermit-entry = { version = "0.9", features = ["kernel"] } hermit-sync = "0.1.3" include-transformed = { version = "0.2", optional = true } -linked_list_allocator = { version = "0.10", default-features = false } log = { version = "0.4", default-features = false } pci-ids = { version = "0.2", optional = true } pflock = "0.2" @@ -88,6 +87,7 @@ num = { version = "0.4", default-features = false } num-traits = { version = "0.2", default-features = false } num-derive = "0.4" zerocopy = "0.6" +talc = { version = "2", default-features = false } time = { version = "0.3", default-features = false } pci_types = { version = "0.5" } diff --git a/src/mm/allocator/mod.rs b/src/mm/allocator/mod.rs index 083660de64..a9c3b9f344 100644 --- a/src/mm/allocator/mod.rs +++ b/src/mm/allocator/mod.rs @@ -10,7 +10,7 @@ use core::ptr::NonNull; use align_address::Align; use hermit_sync::InterruptTicketMutex; -use linked_list_allocator::Heap; +use talc::{ErrOnOom, Span, Talc}; use self::bootstrap::BootstrapAllocator; use self::bump::BumpAllocator; @@ -26,7 +26,7 @@ struct GlobalAllocator { /// The heap allocator. /// /// This is not available immediately and must be initialized ([`Self::init`]). - heap: Option, + heap: Option>, } impl GlobalAllocator { @@ -44,7 +44,12 @@ impl GlobalAllocator { /// The memory starting from `heap_bottom` with a size of `heap_size` /// must be valid and ready to be managed and allocated from. unsafe fn init(&mut self, heap_bottom: *mut u8, heap_size: usize) { - self.heap = Some(unsafe { Heap::new(heap_bottom, heap_size) }); + self.heap = unsafe { + Some(Talc::with_arena( + ErrOnOom, + Span::from_base_size(heap_bottom, heap_size), + )) + } } fn align_layout(layout: Layout) -> Layout { @@ -56,7 +61,7 @@ impl GlobalAllocator { fn allocate(&mut self, layout: Layout) -> Result, AllocError> { let layout = Self::align_layout(layout); match &mut self.heap { - Some(heap) => heap.allocate_first_fit(layout).map_err(|()| AllocError), + Some(heap) => unsafe { heap.malloc(layout).map_err(|_| AllocError) }, None => self .bootstrap_allocator .get_or_insert_with(Default::default) @@ -76,7 +81,7 @@ impl GlobalAllocator { } } else { unsafe { - self.heap.as_mut().unwrap().deallocate(ptr, layout); + self.heap.as_mut().unwrap().free(ptr, layout); } } } diff --git a/src/mm/mod.rs b/src/mm/mod.rs index 771ead996d..1a51c6e7cf 100644 --- a/src/mm/mod.rs +++ b/src/mm/mod.rs @@ -181,9 +181,6 @@ pub fn init() { } heap_start_addr = virt_addr; - unsafe { - crate::ALLOCATOR.init(virt_addr.as_mut_ptr(), virt_size); - } map_addr = virt_addr + counter; map_size = virt_size - counter; @@ -215,6 +212,14 @@ pub fn init() { let heap_end_addr = map_addr; + #[cfg(not(feature = "newlib"))] + unsafe { + crate::ALLOCATOR.init( + heap_start_addr.as_mut_ptr(), + (heap_end_addr - heap_start_addr).into(), + ); + } + let heap_addr_range = heap_start_addr..heap_end_addr; info!("Heap is located at {heap_addr_range:#x?} ({map_size} Bytes unmapped)"); #[cfg(feature = "newlib")]