rlalloc/
lib.rs

1mod chunk;
2pub mod ralloc;
3
4/// Custom global allocator implemented using a free list.
5/// # Usage
6/// ```rust
7/// use rlalloc::RAllocator;
8///
9/// #[global_allocator]
10/// static GLOBAL: RAllocator = RAllocator::new();
11/// // This will make you program use the RAllocatorInternal allocator for all heap allocations
12/// ```
13pub type RAllocator = ralloc::RAllocatorInternal;
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[global_allocator]
20    static GLOBAL: RAllocator = RAllocator::new();
21
22    use std::thread;
23
24    #[test]
25    fn large_number_of_allocations() {
26        let mut v = Vec::new();
27        for _ in 0..1_000_000 {
28            v.push(Box::new(10));
29        }
30        assert_eq!(*v[0], 10);
31    }
32
33    #[test]
34    fn large_allocation() {
35        let v = vec![0u8; 1_000_000];
36        assert_eq!(v.len(), 1_000_000);
37    }
38
39    #[test]
40    fn concurrent_allocations() {
41        let mut handles = Vec::new();
42        for _ in 0..10 {
43            let handle = thread::spawn(|| {
44                let mut v = Vec::new();
45                for _ in 0..100_000 {
46                    v.push(Box::new(10));
47                }
48                assert_eq!(*v[0], 10);
49            });
50            handles.push(handle);
51        }
52
53        for handle in handles {
54            handle.join().unwrap();
55        }
56    }
57
58    #[test]
59    fn concurrent_large_allocations() {
60        let mut handles = Vec::new();
61        for _ in 0..10 {
62            let handle = thread::spawn(|| {
63                let v = vec![0u8; 1_000_000];
64                assert_eq!(v.len(), 1_000_000);
65            });
66            handles.push(handle);
67        }
68
69        for handle in handles {
70            handle.join().unwrap();
71        }
72    }
73}