Skip to content

Commit 28764f4

Browse files
committed
Remove bottom and size fields
Let user of the heap decide about where to extend the heap. Thanks to that the heap does not need to be contiguous.
1 parent 2de33b9 commit 28764f4

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

src/lib.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ mod test;
1515

1616
/// A fixed size heap backed by a linked list of free memory blocks.
1717
pub struct Heap {
18-
bottom: usize,
19-
size: usize,
2018
holes: HoleList,
2119
}
2220

2321
impl Heap {
2422
/// Creates an empty heap. All allocate calls will return `None`.
2523
pub const fn empty() -> Heap {
2624
Heap {
27-
bottom: 0,
28-
size: 0,
2925
holes: HoleList::empty(),
3026
}
3127
}
@@ -37,8 +33,6 @@ impl Heap {
3733
/// This function must be called at most once and must only be used on an
3834
/// empty heap.
3935
pub unsafe fn init(&mut self, heap_bottom: usize, heap_size: usize) {
40-
self.bottom = heap_bottom;
41-
self.size = heap_size;
4236
self.holes = HoleList::new(heap_bottom, heap_size);
4337
}
4438

@@ -48,8 +42,6 @@ impl Heap {
4842
/// given address is invalid.
4943
pub unsafe fn new(heap_bottom: usize, heap_size: usize) -> Heap {
5044
Heap {
51-
bottom: heap_bottom,
52-
size: heap_size,
5345
holes: HoleList::new(heap_bottom, heap_size),
5446
}
5547
}
@@ -84,31 +76,14 @@ impl Heap {
8476
self.holes.deallocate(ptr, size);
8577
}
8678

87-
/// Returns the bottom address of the heap.
88-
pub fn bottom(&self) -> usize {
89-
self.bottom
90-
}
91-
92-
/// Returns the size of the heap.
93-
pub fn size(&self) -> usize {
94-
self.size
95-
}
96-
97-
/// Return the top address of the heap
98-
pub fn top(&self) -> usize {
99-
self.bottom + self.size
100-
}
101-
10279
/// Extends the size of the heap if it won't exceed max_size
10380
/// Returns true if the call succeeded
10481
///
10582
/// # Unsafety
10683
///
10784
/// The new extended area must be valid
108-
pub unsafe fn extend(&mut self, by: usize) {
109-
let top = self.top();
110-
self.holes.deallocate(top as *mut u8, by);
111-
self.size += by;
85+
pub unsafe fn extend(&mut self, addr: *mut u8, by: usize) {
86+
self.holes.deallocate(addr, by);
11287
}
11388
}
11489

src/test.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,57 @@ use std::prelude::v1::*;
22
use std::mem::{size_of, align_of};
33
use super::*;
44

5-
fn new_heap() -> Heap {
5+
struct TestHeap {
6+
heap: Heap,
7+
bottom: usize,
8+
size: usize
9+
}
10+
11+
impl TestHeap {
12+
fn top(&self) -> usize {
13+
self.bottom + self.size
14+
}
15+
16+
pub fn size(&self) -> usize {
17+
self.size
18+
}
19+
20+
pub fn allocate_first_fit(&mut self, mut size: usize, align: usize) -> Option<*mut u8> {
21+
self.heap.allocate_first_fit(size, align)
22+
}
23+
24+
pub unsafe fn deallocate(&mut self, ptr: *mut u8, mut size: usize, _align: usize) {
25+
self.heap.deallocate(ptr, size, _align);
26+
}
27+
28+
pub unsafe fn extend(&mut self, by: usize) {
29+
let top = self.top();
30+
self.heap.deallocate(top as *mut u8, by, 1);
31+
self.size += by;
32+
}
33+
}
34+
35+
fn new_heap() -> TestHeap {
636
const HEAP_SIZE: usize = 1000;
737
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE]));
838

9-
let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
10-
assert!(heap.bottom == heap_space as usize);
11-
assert!(heap.size == HEAP_SIZE);
12-
heap
39+
TestHeap {
40+
heap: unsafe { Heap::new(heap_space as usize, HEAP_SIZE) },
41+
bottom: heap_space as usize,
42+
size: HEAP_SIZE,
43+
}
1344
}
1445

15-
fn new_max_heap() -> Heap {
46+
fn new_max_heap() -> TestHeap {
1647
const HEAP_SIZE: usize = 1024;
1748
const HEAP_SIZE_MAX: usize = 2048;
1849
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE_MAX]));
1950

20-
let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
21-
assert!(heap.bottom == heap_space as usize);
22-
assert!(heap.size == HEAP_SIZE);
23-
heap
51+
TestHeap {
52+
heap: unsafe { Heap::new(heap_space as usize, HEAP_SIZE) },
53+
bottom: heap_space as usize,
54+
size: HEAP_SIZE,
55+
}
2456
}
2557

2658
#[test]
@@ -45,7 +77,7 @@ fn allocate_double_usize() {
4577
assert!(addr.is_some());
4678
let addr = addr.unwrap() as usize;
4779
assert!(addr == heap.bottom);
48-
let (hole_addr, hole_size) = heap.holes.first_hole().expect("ERROR: no hole left");
80+
let (hole_addr, hole_size) = heap.heap.holes.first_hole().expect("ERROR: no hole left");
4981
assert!(hole_addr == heap.bottom + size);
5082
assert!(hole_size == heap.size - size);
5183

0 commit comments

Comments
 (0)