Skip to content

Use new spinning_top crate instead of spin #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2020
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: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ homepage = "http://os.phil-opp.com/kernel-heap.html#a-better-allocator"

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

[dependencies.spin]
version = "0.5.0"
[dependencies.spinning_top]
version = "0.1.0"
features = ["nightly"]
optional = true
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
extern crate std;

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

extern crate alloc;

use alloc::alloc::{AllocRef, AllocErr, Layout};
use core::alloc::{GlobalAlloc};
use alloc::alloc::{AllocErr, AllocRef, Layout};
use core::alloc::GlobalAlloc;
use core::mem;
#[cfg(feature = "use_spin")]
use core::ops::Deref;
use core::ptr::NonNull;
use hole::{Hole, HoleList};
#[cfg(feature = "use_spin")]
use spin::Mutex;
use spinning_top::Spinlock;

mod hole;
#[cfg(test)]
Expand Down Expand Up @@ -140,21 +140,21 @@ unsafe impl AllocRef for Heap {
}

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

#[cfg(feature = "use_spin")]
impl LockedHeap {
/// Creates an empty heap. All allocate calls will return `None`.
pub const fn empty() -> LockedHeap {
LockedHeap(Mutex::new(Heap::empty()))
LockedHeap(Spinlock::new(Heap::empty()))
}

/// Creates a new heap with the given `bottom` and `size`. The bottom address must be valid
/// and the memory in the `[heap_bottom, heap_bottom + heap_size)` range must not be used for
/// anything else. This function is unsafe because it can cause undefined behavior if the
/// given address is invalid.
pub unsafe fn new(heap_bottom: usize, heap_size: usize) -> LockedHeap {
LockedHeap(Mutex::new(Heap {
LockedHeap(Spinlock::new(Heap {
bottom: heap_bottom,
size: heap_size,
holes: HoleList::new(heap_bottom, heap_size),
Expand All @@ -164,9 +164,9 @@ impl LockedHeap {

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

fn deref(&self) -> &Mutex<Heap> {
fn deref(&self) -> &Spinlock<Heap> {
&self.0
}
}
Expand Down