Skip to content

Commit

Permalink
Merge pull request #809 from hermitcore/new-talc
Browse files Browse the repository at this point in the history
Migrate to talc memory allocator
  • Loading branch information
mkroening authored Jul 28, 2023
2 parents 163415a + 095abe6 commit 24219de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }

Expand Down
15 changes: 10 additions & 5 deletions src/mm/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@ struct GlobalAllocator {
/// The heap allocator.
///
/// This is not available immediately and must be initialized ([`Self::init`]).
heap: Option<Heap>,
heap: Option<Talc<ErrOnOom>>,
}

impl GlobalAllocator {
Expand All @@ -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 {
Expand All @@ -56,7 +61,7 @@ impl GlobalAllocator {
fn allocate(&mut self, layout: Layout) -> Result<NonNull<u8>, 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)
Expand All @@ -76,7 +81,7 @@ impl GlobalAllocator {
}
} else {
unsafe {
self.heap.as_mut().unwrap().deallocate(ptr, layout);
self.heap.as_mut().unwrap().free(ptr, layout);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 24219de

Please sign in to comment.