Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ repository = "https://github.com/phil-opp/linked-list-allocator"
documentation = "https://docs.rs/crate/linked_list_allocator"
homepage = "http://os.phil-opp.com/kernel-heap.html#a-better-allocator"

[dependencies]
[features]
default = ["use_spin"]
use_spin = ["spin"]

[dependencies.spin]
spin = "0.4.5"
optional = true
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ extern crate alloc;
#[macro_use]
extern crate std;

#[cfg(feature = "use_spin")]
extern crate spin;

use hole::{Hole, HoleList};
use core::mem;
#[cfg(feature = "use_spin")]
use core::ops::Deref;
use alloc::allocator::{Alloc, Layout, AllocErr};
#[cfg(feature = "use_spin")]
use spin::Mutex;

mod hole;
Expand Down Expand Up @@ -138,8 +141,10 @@ unsafe impl Alloc for Heap {
}
}

#[cfg(feature = "use_spin")]
pub struct LockedHeap(Mutex<Heap>);

#[cfg(feature = "use_spin")]
impl LockedHeap {
/// Creates an empty heap. All allocate calls will return `None`.
pub const fn empty() -> LockedHeap {
Expand All @@ -159,6 +164,7 @@ impl LockedHeap {
}
}

#[cfg(feature = "use_spin")]
impl Deref for LockedHeap {
type Target = Mutex<Heap>;

Expand All @@ -167,6 +173,7 @@ impl Deref for LockedHeap {
}
}

#[cfg(feature = "use_spin")]
unsafe impl<'a> Alloc for &'a LockedHeap {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
self.0.lock().allocate_first_fit(layout)
Expand Down