Skip to content

Commit cc9166a

Browse files
committed
Breaking API change: Use heap_size instead of heap_top
1 parent bb5a8da commit cc9166a

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ mod test;
1515
/// A fixed size heap backed by a linked list of free memory blocks.
1616
pub struct Heap {
1717
bottom: usize,
18-
top: usize,
18+
size: usize,
1919
holes: HoleList,
2020
}
2121

2222
impl Heap {
2323
/// Creates an empty heap. All allocate calls will return `None`.
2424
pub const fn empty() -> Heap {
2525
Heap {
26-
top: 0,
2726
bottom: 0,
27+
size: 0,
2828
holes: HoleList::empty(),
2929
}
3030
}
3131

32-
/// Creates a new heap with the given `bottom` and `top`. Both addresses must be valid and the
33-
/// memory in the `[heap_bottom, heap_top)` range must not be used for anything else. This
34-
/// function is unsafe because it can cause undefined behavior if the given addresses are
35-
/// invalid.
36-
pub unsafe fn new(heap_bottom: usize, heap_top: usize) -> Heap {
32+
/// Creates a new heap with the given `bottom` and `size`. The bottom address must be valid
33+
/// and the memory in the `[heap_bottom, heap_bottom + heap_size)` range must not be used for
34+
/// anything else. This function is unsafe because it can cause undefined behavior if the
35+
/// given address is invalid.
36+
pub unsafe fn new(heap_bottom: usize, heap_size: usize) -> Heap {
3737
Heap {
3838
bottom: heap_bottom,
39-
top: heap_top,
40-
holes: HoleList::new(heap_bottom, heap_top - heap_bottom),
39+
size: heap_size,
40+
holes: HoleList::new(heap_bottom, heap_size),
4141
}
4242
}
4343

@@ -73,9 +73,9 @@ impl Heap {
7373
self.bottom
7474
}
7575

76-
/// Returns the top address of the heap.
77-
pub fn top(&self) -> usize {
78-
self.top
76+
/// Returns the size of the heap.
77+
pub fn size(&self) -> usize {
78+
self.size
7979
}
8080
}
8181

src/test.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ fn new_heap() -> Heap {
77
const HEAP_SIZE: usize = 1000;
88
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE]));
99

10-
let heap_bottom = heap_space as usize;
11-
let heap_top = heap_bottom + HEAP_SIZE;
12-
let heap = unsafe { Heap::new(heap_bottom, heap_top) };
13-
assert!(heap.bottom == heap_bottom);
14-
assert!(heap.top == heap_top);
10+
let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
11+
assert!(heap.bottom == heap_space as usize);
12+
assert!(heap.size == HEAP_SIZE);
1513
heap
1614
}
1715

@@ -24,7 +22,7 @@ fn empty() {
2422
#[test]
2523
fn oom() {
2624
let mut heap = new_heap();
27-
let size = heap.top() - heap.bottom() + 1;
25+
let size = heap.size() + 1;
2826
let addr = heap.allocate_first_fit(size, align_of::<usize>());
2927
assert!(addr.is_none());
3028
}
@@ -39,11 +37,10 @@ fn allocate_double_usize() {
3937
assert!(addr == heap.bottom);
4038
let (hole_addr, hole_size) = heap.holes.first_hole().expect("ERROR: no hole left");
4139
assert!(hole_addr == heap.bottom + size);
42-
assert!(hole_size == heap.top - heap.bottom - size);
40+
assert!(hole_size == heap.size - size);
4341

4442
unsafe {
45-
assert_eq!((*((addr + size) as *const Hole)).size,
46-
heap.top - heap.bottom - size);
43+
assert_eq!((*((addr + size) as *const Hole)).size, heap.size - size);
4744
}
4845
}
4946

@@ -56,7 +53,7 @@ fn allocate_and_free_double_usize() {
5653
*(x as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe);
5754

5855
heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>());
59-
assert_eq!((*(heap.bottom as *const Hole)).size, heap.top - heap.bottom);
56+
assert_eq!((*(heap.bottom as *const Hole)).size, heap.size);
6057
assert!((*(heap.bottom as *const Hole)).next.is_none());
6158
}
6259
}
@@ -76,7 +73,7 @@ fn deallocate_right_before() {
7673
heap.deallocate(x, size, 1);
7774
assert_eq!((*(x as *const Hole)).size, size * 2);
7875
heap.deallocate(z, size, 1);
79-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
76+
assert_eq!((*(x as *const Hole)).size, heap.size);
8077
}
8178
}
8279

@@ -95,7 +92,7 @@ fn deallocate_right_behind() {
9592
heap.deallocate(y, size, 1);
9693
assert_eq!((*(x as *const Hole)).size, size * 2);
9794
heap.deallocate(z, size, 1);
98-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
95+
assert_eq!((*(x as *const Hole)).size, heap.size);
9996
}
10097
}
10198

@@ -118,7 +115,7 @@ fn deallocate_middle() {
118115
heap.deallocate(y, size, 1);
119116
assert_eq!((*(x as *const Hole)).size, size * 3);
120117
heap.deallocate(a, size, 1);
121-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
118+
assert_eq!((*(x as *const Hole)).size, heap.size);
122119
}
123120
}
124121

0 commit comments

Comments
 (0)