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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{buddy::MultiBuddySystem, to_order};
use core::{
alloc::{GlobalAlloc, Layout},
ptr::null_mut,
};
use spin::Mutex;
use typenum::{PowerOfTwo, Unsigned};
struct Freelist {
next: *mut Freelist,
}
impl Freelist {
pub const fn new() -> Self {
Self { next: null_mut() }
}
}
pub struct Cached<T> {
inner: T,
freelist: [Freelist; 32],
}
impl<P: Unsigned + PowerOfTwo + 'static> Cached<MultiBuddySystem<P>> {
pub(crate) const fn new() -> Self {
const LS: Freelist = Freelist::new();
Self {
inner: MultiBuddySystem::new(),
freelist: [LS; 32],
}
}
pub(crate) unsafe fn add_zone(&mut self, begin: usize, end: usize) {
self.inner.add_zone(begin, end);
}
pub unsafe fn alloc(&mut self, layout: core::alloc::Layout) -> *mut u8 {
let order = to_order(P::to_usize(), &layout);
if let Some(head) = self.freelist.get_mut(order) {
let p = head.next;
if !p.is_null() {
head.next = (*p).next;
return p as *mut u8;
}
}
let p = self.inner.alloc1(order);
if !p.is_null() {
return p;
}
for (d, head) in self.freelist.iter_mut().enumerate().rev() {
let mut p = head.next;
while !p.is_null() {
head.next = (*p).next;
if self.inner.free(p as *mut u8, d) >= order {
return self.inner.alloc1(order);
}
p = head.next;
}
}
p
}
pub unsafe fn dealloc(&mut self, ptr: *mut u8, layout: core::alloc::Layout) {
let order = to_order(P::to_usize(), &layout);
if let Some(head) = self.freelist.get_mut(order) {
let ptr = ptr as *mut Freelist;
(*ptr).next = head.next;
head.next = ptr;
} else {
self.inner.free(ptr, order);
}
}
}
pub struct Allocator<P> {
inner: Mutex<Cached<MultiBuddySystem<P>>>,
}
impl<P: Unsigned + PowerOfTwo + 'static> Allocator<P> {
pub const fn new() -> Self {
Self {
inner: Mutex::new(Cached::<MultiBuddySystem<P>>::new()),
}
}
pub unsafe fn add_zone(&mut self, begin: usize, end: usize) {
self.inner.lock().add_zone(begin, end);
}
}
unsafe impl<P: Unsigned + PowerOfTwo + 'static> GlobalAlloc for Allocator<P> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.inner.lock().alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.inner.lock().dealloc(ptr, layout);
}
}
#[cfg(test)]
mod tests {
use super::super::buddy::tests::{test1, PGSIZE};
use super::*;
use rand::Rng;
use std::vec::Vec;
use typenum::Unsigned;
#[test]
fn test_allocator() {
let n: usize = PGSIZE::to_usize() * 1000;
let mut v: Vec<u8> = Vec::new();
v.reserve(n);
let mut rng = rand::thread_rng();
for _ in 0..n {
v.push(rng.gen_range(0..0xFF));
}
let ptr = v.as_mut_ptr() as usize;
unsafe {
let mut a: Allocator<PGSIZE> = Allocator::new();
a.add_zone(ptr, ptr + n);
test1(&a);
}
}
}