forked from rcore-os/buddy_system_allocator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
241 lines (202 loc) · 6.84 KB
/
test.rs
File metadata and controls
241 lines (202 loc) · 6.84 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::linked_list;
use crate::FrameAllocator;
use crate::Heap;
use crate::LockedHeapWithRescue;
use core::alloc::GlobalAlloc;
use core::alloc::Layout;
use core::mem::size_of;
#[test]
fn test_linked_list() {
let mut value1: usize = 0;
let mut value2: usize = 0;
let mut value3: usize = 0;
let mut list = linked_list::LinkedList::new();
unsafe {
list.push(&mut value1 as *mut usize);
list.push(&mut value2 as *mut usize);
list.push(&mut value3 as *mut usize);
}
// Test links
assert_eq!(value3, &value2 as *const usize as usize);
assert_eq!(value2, &value1 as *const usize as usize);
assert_eq!(value1, 0);
// Test iter
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&mut value3 as *mut usize));
assert_eq!(iter.next(), Some(&mut value2 as *mut usize));
assert_eq!(iter.next(), Some(&mut value1 as *mut usize));
assert_eq!(iter.next(), None);
// Test iter_mut
let mut iter_mut = list.iter_mut();
assert_eq!(iter_mut.next().unwrap().pop(), &mut value3 as *mut usize);
// Test pop
assert_eq!(list.pop(), Some(&mut value2 as *mut usize));
assert_eq!(list.pop(), Some(&mut value1 as *mut usize));
assert_eq!(list.pop(), None);
}
#[test]
fn test_empty_heap() {
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
}
#[test]
fn test_heap_add() {
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
let space: [usize; 100] = [0; 100];
unsafe {
heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
}
let addr = heap.alloc(Layout::from_size_align(1, 1).unwrap());
assert!(addr.is_ok());
}
#[test]
fn test_heap_add_large() {
// Max size of block is 2^7 == 128 bytes
let mut heap = Heap::<8>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
// 512 bytes of space
let space: [u8; 512] = [0; 512];
unsafe {
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());
}
#[test]
fn test_heap_oom() {
let mut heap = Heap::<32>::new();
let space: [usize; 100] = [0; 100];
unsafe {
heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
}
assert!(heap
.alloc(Layout::from_size_align(100 * size_of::<usize>(), 1).unwrap())
.is_err());
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_ok());
}
#[test]
fn test_heap_oom_rescue() {
const SPACE_SIZE: usize = 100;
static mut SPACE: [usize; 100] = [0; SPACE_SIZE];
let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>, _layout: &Layout| unsafe {
heap.init(&raw mut SPACE as usize, SPACE_SIZE);
});
unsafe {
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()) as usize != 0);
}
}
#[test]
fn test_heap_alloc_and_free() {
let mut heap = Heap::<32>::new();
assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
let space: [usize; 100] = [0; 100];
unsafe {
heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
}
for _ in 0..100 {
let addr = heap.alloc(Layout::from_size_align(1, 1).unwrap()).unwrap();
heap.dealloc(addr, Layout::from_size_align(1, 1).unwrap());
}
}
#[test]
fn test_empty_frame_allocator() {
let mut frame = FrameAllocator::<32>::new();
assert!(frame.alloc(1).is_none());
}
#[test]
fn test_frame_allocator_add() {
let mut frame = FrameAllocator::<32>::new();
assert!(frame.alloc(1).is_none());
frame.insert(0..3);
let num = frame.alloc(1);
assert_eq!(num, Some(2));
let num = frame.alloc(2);
assert_eq!(num, Some(0));
assert!(frame.alloc(1).is_none());
assert!(frame.alloc(2).is_none());
}
#[test]
fn test_frame_allocator_allocate_large() {
let mut frame = FrameAllocator::<32>::new();
assert_eq!(frame.alloc(10_000_000_000), None);
}
#[test]
fn test_frame_allocator_add_large_size_split() {
let mut frame = FrameAllocator::<32>::new();
frame.insert(0..10_000_000_000);
assert_eq!(frame.alloc(0x8000_0001), None);
assert_eq!(frame.alloc(0x8000_0000), Some(0x8000_0000));
assert_eq!(frame.alloc(0x8000_0000), Some(0x1_0000_0000));
}
#[test]
fn test_frame_allocator_add_large_size() {
let mut frame = FrameAllocator::<33>::new();
frame.insert(0..10_000_000_000);
assert_eq!(frame.alloc(0x8000_0001), Some(0x1_0000_0000));
}
#[test]
fn test_frame_allocator_alloc_and_free() {
let mut frame = FrameAllocator::<32>::new();
assert!(frame.alloc(1).is_none());
frame.add_frame(0, 1024);
for _ in 0..100 {
let addr = frame.alloc(512).unwrap();
frame.dealloc(addr, 512);
}
}
#[test]
fn test_frame_allocator_alloc_and_free_complex() {
let mut frame = FrameAllocator::<32>::new();
frame.add_frame(100, 1024);
for _ in 0..10 {
let addr = frame.alloc(1).unwrap();
frame.dealloc(addr, 1);
}
let addr1 = frame.alloc(1).unwrap();
let addr2 = frame.alloc(1).unwrap();
assert_ne!(addr1, addr2);
}
#[test]
fn test_frame_allocator_aligned() {
let mut frame = FrameAllocator::<32>::new();
frame.add_frame(1, 64);
assert_eq!(
frame.alloc_aligned(Layout::from_size_align(2, 4).unwrap()),
Some(4)
);
assert_eq!(
frame.alloc_aligned(Layout::from_size_align(2, 2).unwrap()),
Some(2)
);
assert_eq!(
frame.alloc_aligned(Layout::from_size_align(2, 1).unwrap()),
Some(8)
);
assert_eq!(
frame.alloc_aligned(Layout::from_size_align(1, 16).unwrap()),
Some(16)
);
}
#[test]
fn test_heap_merge_final_order() {
const NUM_ORDERS: usize = 5;
let backing_size = 1 << NUM_ORDERS;
let backing_layout = Layout::from_size_align(backing_size, backing_size).unwrap();
// create a new heap with 5 orders
let mut heap = Heap::<NUM_ORDERS>::new();
// allocate host memory for use by heap
let backing_allocation = unsafe { std::alloc::alloc(backing_layout) };
let start = backing_allocation as usize;
let middle = unsafe { backing_allocation.add(backing_size / 2) } as usize;
let end = unsafe { backing_allocation.add(backing_size) } as usize;
// add two contiguous ranges of memory
unsafe { heap.add_to_heap(start, middle) };
unsafe { heap.add_to_heap(middle, end) };
// NUM_ORDERS - 1 is the maximum order of the heap
let layout = Layout::from_size_align(1 << (NUM_ORDERS - 1), 1).unwrap();
// allocation should succeed, using one of the added ranges
let alloc = heap.alloc(layout).unwrap();
// deallocation should not attempt to merge the two contiguous ranges as the next order does not exist
heap.dealloc(alloc, layout);
}