polymorph-allocator 1.1.0

A simple no_std memory allocator
Documentation
//! Iterator over the regions in an Allocator.

use crate::{Allocator, Region};

/// Iterator over the regions in an Allocator.
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) => {
                // get next region
                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,
        }
    }
}