Skip to content

Commit 026a647

Browse files
authored
Merge pull request #10 from vinaychandra/vinay/order
Add Order as const generic
2 parents 9c6e36a + 361d6b8 commit 026a647

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

.github/workflows/rust.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
matrix:
1515
rust:
1616
- stable
17-
- nightly-2020-09-24
17+
- nightly-2021-03-09
1818

1919
steps:
2020
- uses: actions/checkout@v2

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ documentation = "https://docs.rs/buddy_system_allocator"
55
homepage = "https://github.com/rcore-os/buddy_system_allocator"
66
repository = "https://github.com/rcore-os/buddy_system_allocator"
77
keywords = ["allocator", "no_std", "heap"]
8-
version = "0.7.0"
9-
authors = ["Jiajie Chen <[email protected]>"]
8+
version = "0.8.0"
9+
authors = ["Jiajie Chen <[email protected]>", "Vinay Chandra Dommeti <[email protected]>"]
1010
edition = "2018"
1111
license = "MIT"
1212

src/lib.rs

+32-32
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ mod test;
2727

2828
pub use frame::*;
2929

30-
/// A heap that uses buddy system
30+
/// A heap that uses buddy system with configurable order.
3131
///
3232
/// # Usage
3333
///
3434
/// Create a heap and add a memory region to it:
3535
/// ```
3636
/// use buddy_system_allocator::*;
3737
/// # use core::mem::size_of;
38-
/// let mut heap = Heap::empty();
38+
/// let mut heap = Heap::<32>::empty();
3939
/// # let space: [usize; 100] = [0; 100];
4040
/// # let begin: usize = space.as_ptr() as usize;
4141
/// # let end: usize = begin + 100 * size_of::<usize>();
@@ -46,21 +46,21 @@ pub use frame::*;
4646
/// heap.add_to_heap(begin, end);
4747
/// }
4848
/// ```
49-
pub struct Heap {
50-
// buddy system with max order of 32
51-
free_list: [linked_list::LinkedList; 32],
49+
pub struct Heap<const ORDER: usize> {
50+
// buddy system with max order of `ORDER`
51+
free_list: [linked_list::LinkedList; ORDER],
5252

5353
// statistics
5454
user: usize,
5555
allocated: usize,
5656
total: usize,
5757
}
5858

59-
impl Heap {
59+
impl<const ORDER: usize> Heap<ORDER> {
6060
/// Create an empty heap
6161
pub const fn new() -> Self {
6262
Heap {
63-
free_list: [linked_list::LinkedList::new(); 32],
63+
free_list: [linked_list::LinkedList::new(); ORDER],
6464
user: 0,
6565
allocated: 0,
6666
total: 0,
@@ -198,7 +198,7 @@ impl Heap {
198198
}
199199
}
200200

201-
impl fmt::Debug for Heap {
201+
impl<const ORDER: usize> fmt::Debug for Heap<ORDER> {
202202
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
203203
fmt.debug_struct("Heap")
204204
.field("user", &self.user)
@@ -216,7 +216,7 @@ impl fmt::Debug for Heap {
216216
/// ```
217217
/// use buddy_system_allocator::*;
218218
/// # use core::mem::size_of;
219-
/// let mut heap = LockedHeap::new();
219+
/// let mut heap = LockedHeap::<32>::new();
220220
/// # let space: [usize; 100] = [0; 100];
221221
/// # let begin: usize = space.as_ptr() as usize;
222222
/// # let end: usize = begin + 100 * size_of::<usize>();
@@ -228,32 +228,32 @@ impl fmt::Debug for Heap {
228228
/// }
229229
/// ```
230230
#[cfg(feature = "use_spin")]
231-
pub struct LockedHeap(Mutex<Heap>);
231+
pub struct LockedHeap<const ORDER: usize>(Mutex<Heap<ORDER>>);
232232

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

240240
/// Creates an empty heap
241-
pub const fn empty() -> LockedHeap {
242-
LockedHeap(Mutex::new(Heap::new()))
241+
pub const fn empty() -> Self {
242+
LockedHeap(Mutex::new(Heap::<ORDER>::new()))
243243
}
244244
}
245245

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

250-
fn deref(&self) -> &Mutex<Heap> {
250+
fn deref(&self) -> &Self::Target {
251251
&self.0
252252
}
253253
}
254254

255255
#[cfg(feature = "use_spin")]
256-
unsafe impl GlobalAlloc for LockedHeap {
256+
unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeap<ORDER> {
257257
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
258258
self.0
259259
.lock()
@@ -274,48 +274,48 @@ unsafe impl GlobalAlloc for LockedHeap {
274274
/// Create a locked heap:
275275
/// ```
276276
/// use buddy_system_allocator::*;
277-
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap| {});
277+
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>| {});
278278
/// ```
279279
///
280280
/// Before oom, the allocator will try to call rescue function and try for one more time.
281281
#[cfg(feature = "use_spin")]
282-
pub struct LockedHeapWithRescue {
283-
inner: Mutex<Heap>,
284-
rescue: fn(&mut Heap),
282+
pub struct LockedHeapWithRescue<const ORDER: usize> {
283+
inner: Mutex<Heap<ORDER>>,
284+
rescue: fn(&mut Heap<ORDER>),
285285
}
286286

287287
#[cfg(feature = "use_spin")]
288-
impl LockedHeapWithRescue {
288+
impl<const ORDER: usize> LockedHeapWithRescue<ORDER> {
289289
/// Creates an empty heap
290290
#[cfg(feature = "const_fn")]
291-
pub const fn new(rescue: fn(&mut Heap)) -> LockedHeapWithRescue {
291+
pub const fn new(rescue: fn(&mut Heap)) -> Self {
292292
LockedHeapWithRescue {
293-
inner: Mutex::new(Heap::new()),
293+
inner: Mutex::new(Heap::<ORDER>::new()),
294294
rescue,
295295
}
296296
}
297297

298298
/// Creates an empty heap
299299
#[cfg(not(feature = "const_fn"))]
300-
pub fn new(rescue: fn(&mut Heap)) -> LockedHeapWithRescue {
300+
pub fn new(rescue: fn(&mut Heap<ORDER>)) -> Self {
301301
LockedHeapWithRescue {
302-
inner: Mutex::new(Heap::new()),
302+
inner: Mutex::new(Heap::<ORDER>::new()),
303303
rescue,
304304
}
305305
}
306306
}
307307

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

312-
fn deref(&self) -> &Mutex<Heap> {
312+
fn deref(&self) -> &Self::Target {
313313
&self.inner
314314
}
315315
}
316316

317317
#[cfg(feature = "use_spin")]
318-
unsafe impl GlobalAlloc for LockedHeapWithRescue {
318+
unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeapWithRescue<ORDER> {
319319
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
320320
let mut inner = self.inner.lock();
321321
match inner.alloc(layout) {

src/test.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ fn test_linked_list() {
4343

4444
#[test]
4545
fn test_empty_heap() {
46-
let mut heap = Heap::new();
46+
let mut heap = Heap::<32>::new();
4747
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
4848
}
4949

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

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

6363
#[test]
6464
fn test_heap_oom() {
65-
let mut heap = Heap::new();
65+
let mut heap = Heap::<32>::new();
6666
let space: [usize; 100] = [0; 100];
6767
unsafe {
6868
heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
@@ -77,7 +77,7 @@ fn test_heap_oom() {
7777
#[test]
7878
fn test_heap_oom_rescue() {
7979
static mut SPACE: [usize; 100] = [0; 100];
80-
let heap = LockedHeapWithRescue::new(|heap: &mut Heap| unsafe {
80+
let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>| unsafe {
8181
heap.add_to_heap(SPACE.as_ptr() as usize, SPACE.as_ptr().add(100) as usize);
8282
});
8383

@@ -88,7 +88,7 @@ fn test_heap_oom_rescue() {
8888

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

9494
let space: [usize; 100] = [0; 100];

0 commit comments

Comments
 (0)