Skip to content

Commit 81600c3

Browse files
author
Jorge Aparicio
committed
add an initialize method to Heap
this lets you create an allocator that doesn't depend on lazy_init. Example below: ``` rust static HEAP: Mutex<Heap> = Mutex::new(Heap::new()); unsafe fn before_main() { HEAP.lock().init(BOTTOM, SIZE); } fn main() { // Use the allocator } ```
1 parent a71d6cb commit 81600c3

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ impl Heap {
2929
}
3030
}
3131

32+
/// Initializes an empty heap
33+
///
34+
/// # Unsafety
35+
///
36+
/// This function must be called at most once and must only be used on an
37+
/// empty heap.
38+
pub unsafe fn init(&mut self, heap_bottom: usize, heap_size: usize) {
39+
self.bottom = heap_bottom;
40+
self.size = heap_size;
41+
self.holes = HoleList::new(heap_bottom, heap_size);
42+
}
43+
3244
/// Creates a new heap with the given `bottom` and `size`. The bottom address must be valid
3345
/// and the memory in the `[heap_bottom, heap_bottom + heap_size)` range must not be used for
3446
/// anything else. This function is unsafe because it can cause undefined behavior if the

0 commit comments

Comments
 (0)