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
use crate::{Allocator, Region};
pub struct AllocatorRegionIterator<'a> {
allocator: &'a Allocator,
header_ptr: usize,
}
impl<'a> AllocatorRegionIterator<'a> {
pub(crate) fn new(allocator: &Allocator) -> AllocatorRegionIterator {
AllocatorRegionIterator {
allocator,
header_ptr: allocator
.get_region_first()
.map_or(0, |r| unsafe { r.header_ptr().unwrap().as_ptr() as usize }),
}
}
}
impl<'a> Iterator for AllocatorRegionIterator<'a> {
type Item = &'a Region;
fn next(&mut self) -> Option<&'a Region> {
if self.header_ptr == 0 {
return None;
}
match self
.allocator
.get_region_for_ptr(self.header_ptr as *mut u8)
{
Some(r) => {
match self.allocator.get_region_for_ptr(r.next as *mut u8) {
Some(next) => {
self.header_ptr = unsafe { next.header_ptr().unwrap().as_ptr() as usize }
}
None => self.header_ptr = 0,
};
Some(r)
}
None => None,
}
}
}