Skip to content

Commit f5ba156

Browse files
committed
Add a flag to prevent duplicate initialization
1 parent eb87f1a commit f5ba156

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
- Added a `init` macro to make initialization easier.
13+
- Added a flag to prevent duplicate initialization
1314

1415
## [v0.6.0] - 2024-09-01
1516

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ pub use tlsf::Heap as TlsfHeap;
3030
/// It internally calls `Heap::init(...)` on the heap,
3131
/// so `Heap::init(...)` should not be called directly if this macro is used.
3232
///
33+
/// # Panics
34+
///
35+
/// This macro will panic if either of the following are true:
36+
///
37+
/// - this function is called more than ONCE.
38+
/// - `size == 0`.
39+
///
3340
/// # Example
3441
///
3542
/// ```rust

src/llff.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::alloc::{GlobalAlloc, Layout};
2-
use core::cell::RefCell;
2+
use core::cell::{Cell, RefCell};
33
use core::ptr::{self, NonNull};
44

55
use critical_section::Mutex;
@@ -8,6 +8,7 @@ use linked_list_allocator::Heap as LLHeap;
88
/// A linked list first fit heap.
99
pub struct Heap {
1010
heap: Mutex<RefCell<LLHeap>>,
11+
once_flag: Mutex<Cell<bool>>,
1112
}
1213

1314
impl Heap {
@@ -18,6 +19,7 @@ impl Heap {
1819
pub const fn empty() -> Heap {
1920
Heap {
2021
heap: Mutex::new(RefCell::new(LLHeap::empty())),
22+
once_flag: Mutex::new(Cell::new(false)),
2123
}
2224
}
2325

@@ -41,12 +43,16 @@ impl Heap {
4143
///
4244
/// # Safety
4345
///
44-
/// Obey these or Bad Stuff will happen.
46+
/// This function will panic if either of the following are true:
4547
///
46-
/// - This function must be called exactly ONCE.
47-
/// - `size > 0`
48+
/// - this function is called more than ONCE.
49+
/// - `size == 0`.
4850
pub unsafe fn init(&self, start_addr: usize, size: usize) {
51+
assert!(size > 0);
4952
critical_section::with(|cs| {
53+
assert!(!self.once_flag.borrow(cs).get());
54+
self.once_flag.borrow(cs).set(true);
55+
5056
self.heap
5157
.borrow(cs)
5258
.borrow_mut()

src/tlsf.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::alloc::{GlobalAlloc, Layout};
2-
use core::cell::RefCell;
2+
use core::cell::{Cell, RefCell};
33
use core::ptr::{self, NonNull};
44

55
use const_default::ConstDefault;
@@ -11,6 +11,7 @@ type TlsfHeap = Tlsf<'static, usize, usize, { usize::BITS as usize }, { usize::B
1111
/// A two-Level segregated fit heap.
1212
pub struct Heap {
1313
heap: Mutex<RefCell<TlsfHeap>>,
14+
once_flag: Mutex<Cell<bool>>,
1415
}
1516

1617
impl Heap {
@@ -21,6 +22,7 @@ impl Heap {
2122
pub const fn empty() -> Heap {
2223
Heap {
2324
heap: Mutex::new(RefCell::new(ConstDefault::DEFAULT)),
25+
once_flag: Mutex::new(Cell::new(false)),
2426
}
2527
}
2628

@@ -44,12 +46,16 @@ impl Heap {
4446
///
4547
/// # Safety
4648
///
47-
/// Obey these or Bad Stuff will happen.
49+
/// This function will panic if either of the following are true:
4850
///
49-
/// - This function must be called exactly ONCE.
50-
/// - `size > 0`
51+
/// - this function is called more than ONCE.
52+
/// - `size == 0`.
5153
pub unsafe fn init(&self, start_addr: usize, size: usize) {
54+
assert!(size > 0);
5255
critical_section::with(|cs| {
56+
assert!(!self.once_flag.borrow(cs).get());
57+
self.once_flag.borrow(cs).set(true);
58+
5359
let block: &[u8] = core::slice::from_raw_parts(start_addr as *const u8, size);
5460
self.heap
5561
.borrow(cs)

0 commit comments

Comments
 (0)