Skip to content

Add Order as const generic #10

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 2 commits into from
Mar 31, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
matrix:
rust:
- stable
- nightly-2020-09-24
- nightly-2021-03-09

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ documentation = "https://docs.rs/buddy_system_allocator"
homepage = "https://github.com/rcore-os/buddy_system_allocator"
repository = "https://github.com/rcore-os/buddy_system_allocator"
keywords = ["allocator", "no_std", "heap"]
version = "0.7.0"
authors = ["Jiajie Chen <noc@jiegec.ac.cn>"]
version = "0.8.0"
authors = ["Jiajie Chen <noc@jiegec.ac.cn>", "Vinay Chandra Dommeti <github@vinay.vc>"]
edition = "2018"
license = "MIT"

Expand Down
64 changes: 32 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ mod test;

pub use frame::*;

/// A heap that uses buddy system
/// A heap that uses buddy system with configurable order.
///
/// # Usage
///
/// Create a heap and add a memory region to it:
/// ```
/// use buddy_system_allocator::*;
/// # use core::mem::size_of;
/// let mut heap = Heap::empty();
/// let mut heap = Heap::<32>::empty();
/// # let space: [usize; 100] = [0; 100];
/// # let begin: usize = space.as_ptr() as usize;
/// # let end: usize = begin + 100 * size_of::<usize>();
Expand All @@ -46,21 +46,21 @@ pub use frame::*;
/// heap.add_to_heap(begin, end);
/// }
/// ```
pub struct Heap {
// buddy system with max order of 32
free_list: [linked_list::LinkedList; 32],
pub struct Heap<const ORDER: usize> {
// buddy system with max order of `ORDER`
free_list: [linked_list::LinkedList; ORDER],

// statistics
user: usize,
allocated: usize,
total: usize,
}

impl Heap {
impl<const ORDER: usize> Heap<ORDER> {
/// Create an empty heap
pub const fn new() -> Self {
Heap {
free_list: [linked_list::LinkedList::new(); 32],
free_list: [linked_list::LinkedList::new(); ORDER],
user: 0,
allocated: 0,
total: 0,
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Heap {
}
}

impl fmt::Debug for Heap {
impl<const ORDER: usize> fmt::Debug for Heap<ORDER> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Heap")
.field("user", &self.user)
Expand All @@ -216,7 +216,7 @@ impl fmt::Debug for Heap {
/// ```
/// use buddy_system_allocator::*;
/// # use core::mem::size_of;
/// let mut heap = LockedHeap::new();
/// let mut heap = LockedHeap::<32>::new();
/// # let space: [usize; 100] = [0; 100];
/// # let begin: usize = space.as_ptr() as usize;
/// # let end: usize = begin + 100 * size_of::<usize>();
Expand All @@ -228,32 +228,32 @@ impl fmt::Debug for Heap {
/// }
/// ```
#[cfg(feature = "use_spin")]
pub struct LockedHeap(Mutex<Heap>);
pub struct LockedHeap<const ORDER: usize>(Mutex<Heap<ORDER>>);

#[cfg(feature = "use_spin")]
impl LockedHeap {
impl<const ORDER: usize> LockedHeap<ORDER> {
/// Creates an empty heap
pub const fn new() -> LockedHeap {
LockedHeap(Mutex::new(Heap::new()))
pub const fn new() -> Self {
LockedHeap(Mutex::new(Heap::<ORDER>::new()))
}

/// Creates an empty heap
pub const fn empty() -> LockedHeap {
LockedHeap(Mutex::new(Heap::new()))
pub const fn empty() -> Self {
LockedHeap(Mutex::new(Heap::<ORDER>::new()))
}
}

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

fn deref(&self) -> &Mutex<Heap> {
fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(feature = "use_spin")]
unsafe impl GlobalAlloc for LockedHeap {
unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeap<ORDER> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.0
.lock()
Expand All @@ -274,48 +274,48 @@ unsafe impl GlobalAlloc for LockedHeap {
/// Create a locked heap:
/// ```
/// use buddy_system_allocator::*;
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap| {});
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>| {});
/// ```
///
/// Before oom, the allocator will try to call rescue function and try for one more time.
#[cfg(feature = "use_spin")]
pub struct LockedHeapWithRescue {
inner: Mutex<Heap>,
rescue: fn(&mut Heap),
pub struct LockedHeapWithRescue<const ORDER: usize> {
inner: Mutex<Heap<ORDER>>,
rescue: fn(&mut Heap<ORDER>),
}

#[cfg(feature = "use_spin")]
impl LockedHeapWithRescue {
impl<const ORDER: usize> LockedHeapWithRescue<ORDER> {
/// Creates an empty heap
#[cfg(feature = "const_fn")]
pub const fn new(rescue: fn(&mut Heap)) -> LockedHeapWithRescue {
pub const fn new(rescue: fn(&mut Heap)) -> Self {
LockedHeapWithRescue {
inner: Mutex::new(Heap::new()),
inner: Mutex::new(Heap::<ORDER>::new()),
rescue,
}
}

/// Creates an empty heap
#[cfg(not(feature = "const_fn"))]
pub fn new(rescue: fn(&mut Heap)) -> LockedHeapWithRescue {
pub fn new(rescue: fn(&mut Heap<ORDER>)) -> Self {
LockedHeapWithRescue {
inner: Mutex::new(Heap::new()),
inner: Mutex::new(Heap::<ORDER>::new()),
rescue,
}
}
}

#[cfg(feature = "use_spin")]
impl Deref for LockedHeapWithRescue {
type Target = Mutex<Heap>;
impl<const ORDER: usize> Deref for LockedHeapWithRescue<ORDER> {
type Target = Mutex<Heap<ORDER>>;

fn deref(&self) -> &Mutex<Heap> {
fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[cfg(feature = "use_spin")]
unsafe impl GlobalAlloc for LockedHeapWithRescue {
unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeapWithRescue<ORDER> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut inner = self.inner.lock();
match inner.alloc(layout) {
Expand Down
10 changes: 5 additions & 5 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ fn test_linked_list() {

#[test]
fn test_empty_heap() {
let mut heap = Heap::new();
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
}

#[test]
fn test_heap_add() {
let mut heap = Heap::new();
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());

let space: [usize; 100] = [0; 100];
Expand All @@ -62,7 +62,7 @@ fn test_heap_add() {

#[test]
fn test_heap_oom() {
let mut heap = Heap::new();
let mut heap = Heap::<32>::new();
let space: [usize; 100] = [0; 100];
unsafe {
heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
Expand All @@ -77,7 +77,7 @@ fn test_heap_oom() {
#[test]
fn test_heap_oom_rescue() {
static mut SPACE: [usize; 100] = [0; 100];
let heap = LockedHeapWithRescue::new(|heap: &mut Heap| unsafe {
let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>| unsafe {
heap.add_to_heap(SPACE.as_ptr() as usize, SPACE.as_ptr().add(100) as usize);
});

Expand All @@ -88,7 +88,7 @@ fn test_heap_oom_rescue() {

#[test]
fn test_heap_alloc_and_free() {
let mut heap = Heap::new();
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());

let space: [usize; 100] = [0; 100];
Expand Down