From 1bf005a19d98ccf2d36ac64500d8bfebaefe6ade Mon Sep 17 00:00:00 2001 From: bigsaltyfishes Date: Sat, 21 Sep 2024 22:19:54 +0800 Subject: [PATCH 1/2] fixup! test: fully add the empty space to heap. * This fixes commit 95ed6622046f238c3a09ee6c3a605113d1377779 Signed-off-by: bigsaltyfishes --- src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test.rs b/src/test.rs index d25ac23..c99d53c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -69,7 +69,7 @@ fn test_heap_add_large() { // 512 bytes of space let space: [u8; 512] = [0; 512]; unsafe { - heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize); + heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(512) as usize); } let addr = heap.alloc(Layout::from_size_align(1, 1).unwrap()); assert!(addr.is_ok()); From 00fd9f772ea8a79e65d3286f2ed66dc98c0ac262 Mon Sep 17 00:00:00 2001 From: bigsaltyfishes Date: Sat, 21 Sep 2024 22:44:05 +0800 Subject: [PATCH 2/2] 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 --- README.md | 2 +- benches/memory_allocator_benchmark.rs | 2 +- src/frame.rs | 18 ++++++++++-------- src/lib.rs | 14 ++++++++++---- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 98fa6b4..48a12f7 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ To use buddy_system_allocator for global allocator: use buddy_system_allocator::LockedHeap; #[global_allocator] -static HEAP_ALLOCATOR: LockedHeap = LockedHeap::<32>::empty(); +static HEAP_ALLOCATOR: LockedHeap = LockedHeap::<33>::empty(); ``` To init the allocator: diff --git a/benches/memory_allocator_benchmark.rs b/benches/memory_allocator_benchmark.rs index 0055c10..9628469 100644 --- a/benches/memory_allocator_benchmark.rs +++ b/benches/memory_allocator_benchmark.rs @@ -138,7 +138,7 @@ pub fn thread_test() { } } -const ORDER: usize = 32; +const ORDER: usize = 33; const MACHINE_ALIGN: usize = core::mem::size_of::(); /// for now 128M is needed /// TODO: reduce memory use diff --git a/src/frame.rs b/src/frame.rs index 8663c77..60f82b3 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -11,16 +11,17 @@ use spin::Mutex; /// A frame allocator that uses buddy system, requiring a global allocator. /// -/// The max order of the allocator is specified via the const generic parameter `ORDER`. The frame -/// allocator will only be able to allocate ranges of size up to 2ORDER, out of a total -/// range of size at most 2ORDER + 1 - 1. +/// The max order of the allocator is determined by the const generic parameter `ORDER` (`MAX_ORDER = ORDER - 1`). +/// The frame allocator will only be able to allocate ranges of size up to 2MAX_ORDER, out of a total +/// range of size at most 2MAX_ORDER + 1 - 1. /// /// # Usage /// /// Create a frame allocator and add some frames to it: /// ``` /// use buddy_system_allocator::*; -/// let mut frame = FrameAllocator::<32>::new(); +/// // Notice that the max order is `ORDER - 1`. +/// let mut frame = FrameAllocator::<33>::new(); /// assert!(frame.alloc(1).is_none()); /// /// frame.add_frame(0, 3); @@ -29,8 +30,8 @@ use spin::Mutex; /// let num = frame.alloc(2); /// assert_eq!(num, Some(0)); /// ``` -pub struct FrameAllocator { - // buddy system with max order of ORDER +pub struct FrameAllocator { + // buddy system with max order of `ORDER - 1` free_list: [BTreeSet; ORDER], // statistics @@ -175,7 +176,8 @@ impl FrameAllocator { /// Create a locked frame allocator and add frames to it: /// ``` /// use buddy_system_allocator::*; -/// let mut frame = LockedFrameAllocator::<32>::new(); +/// // Notice that the max order is `ORDER - 1`. +/// let mut frame = LockedFrameAllocator::<33>::new(); /// assert!(frame.lock().alloc(1).is_none()); /// /// frame.lock().add_frame(0, 3); @@ -185,7 +187,7 @@ impl FrameAllocator { /// assert_eq!(num, Some(0)); /// ``` #[cfg(feature = "use_spin")] -pub struct LockedFrameAllocator(Mutex>); +pub struct LockedFrameAllocator(Mutex>); #[cfg(feature = "use_spin")] impl LockedFrameAllocator { diff --git a/src/lib.rs b/src/lib.rs index 1fd98e3..85eb30a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,10 @@ pub use frame::*; /// ``` /// use buddy_system_allocator::*; /// # use core::mem::size_of; -/// let mut heap = Heap::<32>::empty(); +/// // The max order of the buddy system is `ORDER - 1`. +/// // For example, to create a heap with a maximum block size of 2^32 bytes, +/// // you should define the heap with `ORDER = 33`. +/// let mut heap = Heap::<33>::empty(); /// # let space: [usize; 100] = [0; 100]; /// # let begin: usize = space.as_ptr() as usize; /// # let end: usize = begin + 100 * size_of::(); @@ -51,7 +54,7 @@ pub use frame::*; /// } /// ``` pub struct Heap { - // buddy system with max order of `ORDER` + // buddy system with max order of `ORDER - 1` free_list: [linked_list::LinkedList; ORDER], // statistics @@ -229,7 +232,10 @@ impl fmt::Debug for Heap { /// ``` /// use buddy_system_allocator::*; /// # use core::mem::size_of; -/// let mut heap = LockedHeap::<32>::new(); +/// // The max order of the buddy system is `ORDER - 1`. +/// // For example, to create a heap with a maximum block size of 2^32 bytes, +/// // you should define the heap with `ORDER = 33`. +/// let mut heap = LockedHeap::<33>::new(); /// # let space: [usize; 100] = [0; 100]; /// # let begin: usize = space.as_ptr() as usize; /// # let end: usize = begin + 100 * size_of::(); @@ -287,7 +293,7 @@ unsafe impl GlobalAlloc for LockedHeap { /// Create a locked heap: /// ``` /// use buddy_system_allocator::*; -/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>, layout: &core::alloc::Layout| {}); +/// let heap = LockedHeapWithRescue::new(|heap: &mut Heap<33>, layout: &core::alloc::Layout| {}); /// ``` /// /// Before oom, the allocator will try to call rescue function and try for one more time.