Skip to content

Commit 999b7e0

Browse files
Improve docs for buddy system heap order explanation
- Clarify that the real order of the buddy system is `ORDER - 1`. - Treewide change of default `ORDER` to `33` to match the explanation. Signed-off-by: bigsaltyfishes <bigsaltyfishes@gmail.com>
1 parent 1bf005a commit 999b7e0

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

benches/memory_allocator_benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn thread_test() {
138138
}
139139
}
140140

141-
const ORDER: usize = 32;
141+
const ORDER: usize = 33;
142142
const MACHINE_ALIGN: usize = core::mem::size_of::<usize>();
143143
/// for now 128M is needed
144144
/// TODO: reduce memory use

src/frame.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use spin::Mutex;
1111

1212
/// A frame allocator that uses buddy system, requiring a global allocator.
1313
///
14-
/// The max order of the allocator is specified via the const generic parameter `ORDER`. The frame
15-
/// allocator will only be able to allocate ranges of size up to 2<sup>ORDER</sup>, out of a total
16-
/// range of size at most 2<sup>ORDER + 1</sup> - 1.
14+
/// The max order of the allocator is determined by the const generic parameter `ORDER` (`MAX_ORDER = ORDER - 1`).
15+
/// The frame allocator will only be able to allocate ranges of size up to 2<sup>MAX_ORDER</sup>, out of a total
16+
/// range of size at most 2<sup>MAX_ORDER + 1</sup> - 1.
1717
///
1818
/// # Usage
1919
///
@@ -29,7 +29,7 @@ use spin::Mutex;
2929
/// let num = frame.alloc(2);
3030
/// assert_eq!(num, Some(0));
3131
/// ```
32-
pub struct FrameAllocator<const ORDER: usize = 32> {
32+
pub struct FrameAllocator<const ORDER: usize = 33> {
3333
// buddy system with max order of ORDER
3434
free_list: [BTreeSet<usize>; ORDER],
3535

@@ -175,7 +175,7 @@ impl<const ORDER: usize> FrameAllocator<ORDER> {
175175
/// Create a locked frame allocator and add frames to it:
176176
/// ```
177177
/// use buddy_system_allocator::*;
178-
/// let mut frame = LockedFrameAllocator::<32>::new();
178+
/// let mut frame = LockedFrameAllocator::<33>::new();
179179
/// assert!(frame.lock().alloc(1).is_none());
180180
///
181181
/// frame.lock().add_frame(0, 3);
@@ -185,7 +185,7 @@ impl<const ORDER: usize> FrameAllocator<ORDER> {
185185
/// assert_eq!(num, Some(0));
186186
/// ```
187187
#[cfg(feature = "use_spin")]
188-
pub struct LockedFrameAllocator<const ORDER: usize = 32>(Mutex<FrameAllocator<ORDER>>);
188+
pub struct LockedFrameAllocator<const ORDER: usize = 33>(Mutex<FrameAllocator<ORDER>>);
189189

190190
#[cfg(feature = "use_spin")]
191191
impl<const ORDER: usize> LockedFrameAllocator<ORDER> {

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ pub use frame::*;
3939
/// ```
4040
/// use buddy_system_allocator::*;
4141
/// # use core::mem::size_of;
42-
/// let mut heap = Heap::<32>::empty();
42+
/// // The order of the buddy system is `ORDER - 1`.
43+
/// // For example, to create a heap with a maximum block size of 2^32 bytes,
44+
/// // you should define the heap with `ORDER = 33`.
45+
/// let mut heap = Heap::<33>::empty();
4346
/// # let space: [usize; 100] = [0; 100];
4447
/// # let begin: usize = space.as_ptr() as usize;
4548
/// # let end: usize = begin + 100 * size_of::<usize>();
@@ -229,7 +232,10 @@ impl<const ORDER: usize> fmt::Debug for Heap<ORDER> {
229232
/// ```
230233
/// use buddy_system_allocator::*;
231234
/// # use core::mem::size_of;
232-
/// let mut heap = LockedHeap::<32>::new();
235+
/// // The order of the buddy system is `ORDER - 1`.
236+
/// // For example, to create a heap with a maximum block size of 2^32 bytes,
237+
/// // you should define the heap with `ORDER = 33`.
238+
/// let mut heap = LockedHeap::<33>::new();
233239
/// # let space: [usize; 100] = [0; 100];
234240
/// # let begin: usize = space.as_ptr() as usize;
235241
/// # let end: usize = begin + 100 * size_of::<usize>();
@@ -287,7 +293,7 @@ unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeap<ORDER> {
287293
/// Create a locked heap:
288294
/// ```
289295
/// use buddy_system_allocator::*;
290-
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>, layout: &core::alloc::Layout| {});
296+
/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<33>, layout: &core::alloc::Layout| {});
291297
/// ```
292298
///
293299
/// Before oom, the allocator will try to call rescue function and try for one more time.

0 commit comments

Comments
 (0)