-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathallocator.rs
More file actions
143 lines (127 loc) · 4.07 KB
/
Copy pathallocator.rs
File metadata and controls
143 lines (127 loc) · 4.07 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
#![feature(btreemap_alloc)]
#![feature(allocator_api)]
use std::alloc::{Allocator, Layout};
use std::collections::BTreeMap;
use std::io::Write;
use axallocator::{AllocatorRc, BuddyByteAllocator, SlabByteAllocator, TlsfByteAllocator};
use rand::{prelude::SliceRandom, Rng};
const POOL_SIZE: usize = 1024 * 1024 * 128;
fn test_vec(n: usize, alloc: &(impl Allocator + Clone)) {
let mut v = Vec::with_capacity_in(n, alloc.clone());
for _ in 0..n {
v.push(rand::random::<u32>());
}
v.sort();
for i in 0..n - 1 {
assert!(v[i] <= v[i + 1]);
}
}
fn test_vec2(n: usize, blk_size: usize, alloc: &(impl Allocator + Clone)) {
let mut v = Vec::new_in(alloc.clone());
for _ in 0..n {
let block = Vec::<u64, _>::with_capacity_in(blk_size, alloc.clone());
v.push(block);
}
let mut index = Vec::with_capacity_in(n, alloc.clone());
for i in 0..n {
index.push(i);
}
index.shuffle(&mut rand::thread_rng());
for i in index {
v[i] = Vec::new_in(alloc.clone())
}
}
fn test_btree_map(n: usize, alloc: &(impl Allocator + Clone)) {
let mut m = BTreeMap::new_in(alloc.clone());
for _ in 0..n {
if rand::random::<u32>() % 5 == 0 && !m.is_empty() {
m.pop_first();
} else {
let value = rand::random::<u32>();
let mut key = Vec::new_in(alloc.clone());
write!(&mut key, "key_{value}").unwrap();
m.insert(key, value);
}
}
for (k, v) in m.iter() {
let key = std::str::from_utf8(k)
.unwrap()
.strip_prefix("key_")
.unwrap();
assert_eq!(key.parse::<u32>().unwrap(), *v);
}
}
pub fn test_alignment(n: usize, alloc: &(impl Allocator + Clone)) {
let mut rng = rand::thread_rng();
let mut blocks = vec![];
for _ in 0..n {
if rng.gen_ratio(2, 3) || blocks.is_empty() {
// insert a block
let size =
((1 << rng.gen_range(0..16)) as f32 * rng.gen_range(1.0..2.0)).round() as usize;
let align = 1 << rng.gen_range(0..8);
let layout = Layout::from_size_align(size, align).unwrap();
let ptr = alloc.allocate(layout).unwrap();
blocks.push((ptr, layout));
} else {
// delete a block
let idx = rng.gen_range(0..blocks.len());
let blk = blocks.swap_remove(idx);
unsafe { alloc.deallocate(blk.0.cast(), blk.1) };
}
}
for blk in blocks {
unsafe { alloc.deallocate(blk.0.cast(), blk.1) };
}
}
fn run_test(f: impl FnOnce(&mut [u8])) {
let layout = Layout::from_size_align(POOL_SIZE, 4096).unwrap();
let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
let pool = unsafe { core::slice::from_raw_parts_mut(ptr, POOL_SIZE) };
f(pool);
unsafe { std::alloc::dealloc(ptr, layout) };
}
#[test]
fn system_alloc() {
run_test(|_pool| {
let alloc = std::alloc::System;
test_alignment(50, &alloc);
test_vec(3_000_000, &alloc);
test_vec2(30_000, 64, &alloc);
test_vec2(7_500, 520, &alloc);
test_btree_map(50_000, &alloc);
})
}
#[test]
fn buddy_alloc() {
run_test(|pool| {
let alloc = AllocatorRc::new(BuddyByteAllocator::new(), pool);
test_alignment(50, &alloc);
test_vec(3_000_000, &alloc);
test_vec2(30_000, 64, &alloc);
test_vec2(7_500, 520, &alloc);
test_btree_map(50_000, &alloc);
})
}
#[test]
fn slab_alloc() {
run_test(|pool| {
let alloc = AllocatorRc::new(SlabByteAllocator::new(), pool);
test_alignment(50, &alloc);
test_vec(3_000_000, &alloc);
test_vec2(30_000, 64, &alloc);
test_vec2(7_500, 520, &alloc);
test_btree_map(50_000, &alloc);
})
}
#[test]
fn tlsf_alloc() {
run_test(|pool| {
let alloc = AllocatorRc::new(TlsfByteAllocator::new(), pool);
test_alignment(50, &alloc);
test_vec(3_000_000, &alloc);
test_vec2(30_000, 64, &alloc);
test_vec2(7_500, 520, &alloc);
test_btree_map(50_000, &alloc);
})
}