pub struct FrameAllocator<const ORDER: usize = 33> { /* private fields */ }Expand description
A frame allocator that uses buddy system, requiring a global allocator.
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::*;
// 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);
let num = frame.alloc(1);
assert_eq!(num, Some(2));
let num = frame.alloc(2);
assert_eq!(num, Some(0));Implementations§
Source§impl<const ORDER: usize> FrameAllocator<ORDER>
impl<const ORDER: usize> FrameAllocator<ORDER>
Sourcepub fn add_frame(&mut self, start: usize, end: usize)
pub fn add_frame(&mut self, start: usize, end: usize)
Add a range of frame number [start, end) to the allocator
Sourcepub fn alloc(&mut self, count: usize) -> Option<usize>
pub fn alloc(&mut self, count: usize) -> Option<usize>
Allocate a range of frames from the allocator, returning the first frame of the allocated range.
Sourcepub fn alloc_aligned(&mut self, layout: Layout) -> Option<usize>
pub fn alloc_aligned(&mut self, layout: Layout) -> Option<usize>
Allocate a range of frames with the given size and alignment from the allocator, returning the first frame of the allocated range. The allocated size is the maximum of the next power of two of the given size and the alignment.
Sourcepub fn dealloc(&mut self, start_frame: usize, count: usize)
pub fn dealloc(&mut self, start_frame: usize, count: usize)
Deallocate a range of frames [frame, frame+count) from the frame allocator.
The range should be exactly the same when it was allocated, as in heap allocator
Sourcepub fn dealloc_aligned(&mut self, start_frame: usize, layout: Layout)
pub fn dealloc_aligned(&mut self, start_frame: usize, layout: Layout)
Deallocate a range of frames which was previously allocated by [alloc_aligned].
The layout must be exactly the same as when it was allocated.