1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use super::block_list_alloc::{BlockListAlloc, BLOCK_SIZE};
use buddy_alloc::NonThreadsafeAlloc as BuddyAlloc;
use core::alloc::{GlobalAlloc, Layout};
const BLOCK_ALLOC_LIMIT: usize = BLOCK_SIZE;
pub struct MixedAlloc {
block_list_alloc: BlockListAlloc,
buddy_alloc: BuddyAlloc,
}
impl MixedAlloc {
pub const fn new(block_list_alloc: BlockListAlloc, buddy_alloc: BuddyAlloc) -> Self {
MixedAlloc {
block_list_alloc,
buddy_alloc,
}
}
}
unsafe impl GlobalAlloc for MixedAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let bytes = layout.size();
if bytes > BLOCK_ALLOC_LIMIT {
self.buddy_alloc.alloc(layout)
} else {
let mut p = self.block_list_alloc.alloc(layout);
if p.is_null() {
p = self.buddy_alloc.alloc(layout);
}
p
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if self.block_list_alloc.contains_ptr(ptr) {
self.block_list_alloc.dealloc(ptr, layout);
} else {
self.buddy_alloc.dealloc(ptr, layout);
}
}
}
unsafe impl Sync for MixedAlloc {}