1
1
use super :: prev_power_of_two;
2
2
use alloc:: collections:: BTreeSet ;
3
+ use core:: array;
3
4
use core:: cmp:: min;
4
5
use core:: ops:: Range ;
5
6
@@ -8,15 +9,18 @@ use core::ops::Deref;
8
9
#[ cfg( feature = "use_spin" ) ]
9
10
use spin:: Mutex ;
10
11
11
- /// A frame allocator that uses buddy system,
12
- /// requiring a global allocator
12
+ /// A frame allocator that uses buddy system, requiring a global allocator.
13
+ ///
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.
13
17
///
14
18
/// # Usage
15
19
///
16
20
/// Create a frame allocator and add some frames to it:
17
21
/// ```
18
22
/// use buddy_system_allocator::*;
19
- /// let mut frame = FrameAllocator::new();
23
+ /// let mut frame = FrameAllocator::<32>:: new();
20
24
/// assert!(frame.alloc(1).is_none());
21
25
///
22
26
/// frame.add_frame(0, 3);
@@ -25,20 +29,20 @@ use spin::Mutex;
25
29
/// let num = frame.alloc(2);
26
30
/// assert_eq!(num, Some(0));
27
31
/// ```
28
- pub struct FrameAllocator {
29
- // buddy system with max order of 32
30
- free_list : [ BTreeSet < usize > ; 32 ] ,
32
+ pub struct FrameAllocator < const ORDER : usize = 32 > {
33
+ // buddy system with max order of ORDER
34
+ free_list : [ BTreeSet < usize > ; ORDER ] ,
31
35
32
36
// statistics
33
37
allocated : usize ,
34
38
total : usize ,
35
39
}
36
40
37
- impl FrameAllocator {
41
+ impl < const ORDER : usize > FrameAllocator < ORDER > {
38
42
/// Create an empty frame allocator
39
43
pub fn new ( ) -> Self {
40
- FrameAllocator {
41
- free_list : Default :: default ( ) ,
44
+ Self {
45
+ free_list : array :: from_fn ( |_| BTreeSet :: default ( ) ) ,
42
46
allocated : 0 ,
43
47
total : 0 ,
44
48
}
@@ -137,7 +141,7 @@ impl FrameAllocator {
137
141
/// Create a locked frame allocator and add frames to it:
138
142
/// ```
139
143
/// use buddy_system_allocator::*;
140
- /// let mut frame = LockedFrameAllocator::new();
144
+ /// let mut frame = LockedFrameAllocator::<32>:: new();
141
145
/// assert!(frame.lock().alloc(1).is_none());
142
146
///
143
147
/// frame.lock().add_frame(0, 3);
@@ -147,21 +151,21 @@ impl FrameAllocator {
147
151
/// assert_eq!(num, Some(0));
148
152
/// ```
149
153
#[ cfg( feature = "use_spin" ) ]
150
- pub struct LockedFrameAllocator ( Mutex < FrameAllocator > ) ;
154
+ pub struct LockedFrameAllocator < const ORDER : usize = 32 > ( Mutex < FrameAllocator < ORDER > > ) ;
151
155
152
156
#[ cfg( feature = "use_spin" ) ]
153
- impl LockedFrameAllocator {
157
+ impl < const ORDER : usize > LockedFrameAllocator < ORDER > {
154
158
/// Creates an empty heap
155
- pub fn new ( ) -> LockedFrameAllocator {
156
- LockedFrameAllocator ( Mutex :: new ( FrameAllocator :: new ( ) ) )
159
+ pub fn new ( ) -> Self {
160
+ Self ( Mutex :: new ( FrameAllocator :: new ( ) ) )
157
161
}
158
162
}
159
163
160
164
#[ cfg( feature = "use_spin" ) ]
161
- impl Deref for LockedFrameAllocator {
162
- type Target = Mutex < FrameAllocator > ;
165
+ impl < const ORDER : usize > Deref for LockedFrameAllocator < ORDER > {
166
+ type Target = Mutex < FrameAllocator < ORDER > > ;
163
167
164
- fn deref ( & self ) -> & Mutex < FrameAllocator > {
168
+ fn deref ( & self ) -> & Mutex < FrameAllocator < ORDER > > {
165
169
& self . 0
166
170
}
167
171
}
0 commit comments