Skip to content

Commit

Permalink
step3: support linked-list-allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Feb 24, 2024
1 parent f1fbf8a commit db7a9ae
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions api/axfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ alloc = ["axalloc", "axruntime/alloc"]
alloc-tlsf = ["axalloc/tlsf"]
alloc-slab = ["axalloc/slab"]
alloc-buddy = ["axalloc/buddy"]
alloc-new = ["axalloc/new"]
paging = ["alloc", "axhal/paging", "axruntime/paging"]
tls = ["alloc", "axhal/tls", "axruntime/tls", "axtask?/tls"]

Expand Down
4 changes: 3 additions & 1 deletion crates/allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ documentation = "https://rcore-os.github.io/arceos/allocator/index.html"

[features]
default = []
full = ["bitmap", "tlsf", "slab", "buddy", "allocator_api"]
full = ["bitmap", "tlsf", "slab", "buddy", "allocator_api", "new"]

bitmap = ["dep:bitmap-allocator"]

tlsf = ["dep:rlsf"]
slab = ["dep:slab_allocator"]
buddy = ["dep:buddy_system_allocator"]
new = ["dep:linked_list_allocator"]

allocator_api = []

Expand All @@ -26,6 +27,7 @@ buddy_system_allocator = { version = "0.9", default-features = false, optional =
slab_allocator = { path = "../slab_allocator", optional = true }
rlsf = { version = "0.2", optional = true }
bitmap-allocator = { git = "https://github.com/rcore-os/bitmap-allocator.git", rev = "88e871a", optional = true }
linked_list_allocator = { version = "0.10.5", optional = true }

[dev-dependencies]
allocator = { path = ".", features = ["full"] }
Expand Down
5 changes: 5 additions & 0 deletions crates/allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ mod tlsf;
#[cfg(feature = "tlsf")]
pub use tlsf::TlsfByteAllocator;

#[cfg(feature = "new")]
mod linked_list;
#[cfg(feature = "new")]
pub use linked_list::LinkedListByteAllocator;

use core::alloc::Layout;
use core::ptr::NonNull;

Expand Down
60 changes: 60 additions & 0 deletions crates/allocator/src/linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Lineked list memory allocation.

use linked_list_allocator::Heap;

use core::alloc::Layout;
use core::ptr::NonNull;

use crate::{AllocError, AllocResult, BaseAllocator, ByteAllocator};

/// A byte-granularity memory allocator based on the [linked_list_allocator].
///
/// [linked_list_allocator]: https://docs.rs/linked_list_allocator/0.10.5/linked_list_allocator/
pub struct LinkedListByteAllocator {
inner: Heap,
}

impl LinkedListByteAllocator {
/// Creates a new empty `LinkedListByteAllocator`.
pub const fn new() -> Self {
Self {
inner: Heap::empty(),
}
}
}

impl BaseAllocator for LinkedListByteAllocator {
fn init(&mut self, start: usize, size: usize) {
unsafe { self.inner.init(start as *mut u8, size) };
}

fn add_memory(&mut self, _start: usize, _size: usize) -> AllocResult {
// self.inner.extend(by)
// unsafe { self.inner.add_to_heap(start, start + size) };
Ok(())
}
}

impl ByteAllocator for LinkedListByteAllocator {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>> {
self.inner
.allocate_first_fit(layout)
.map_err(|_| AllocError::NoMemory)
}

fn dealloc(&mut self, pos: NonNull<u8>, layout: Layout) {
unsafe { self.inner.deallocate(pos, layout) }
}

fn total_bytes(&self) -> usize {
self.inner.size()
}

fn used_bytes(&self) -> usize {
self.inner.used()
}

fn available_bytes(&self) -> usize {
self.inner.size() - self.inner.used()
}
}
1 change: 1 addition & 0 deletions modules/axalloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ default = ["tlsf"]
tlsf = ["allocator/tlsf"]
slab = ["allocator/slab"]
buddy = ["allocator/buddy"]
new = ["allocator/new"]

[dependencies]
log = "0.4"
Expand Down
1 change: 1 addition & 0 deletions ulib/axstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ alloc = ["arceos_api/alloc", "axfeat/alloc", "axio/alloc"]
alloc-tlsf = ["axfeat/alloc-tlsf"]
alloc-slab = ["axfeat/alloc-slab"]
alloc-buddy = ["axfeat/alloc-buddy"]
alloc-new = ["axfeat/alloc-new"]
paging = ["axfeat/paging"]
tls = ["axfeat/tls"]

Expand Down

0 comments on commit db7a9ae

Please sign in to comment.