use super::Vmalloc;
use crate::page;
use core::marker::PhantomData;
use core::ptr::NonNull;
pub struct VmallocPageIter<'a> {
buf: NonNull<u8>,
size: usize,
index: usize,
_p: PhantomData<page::BorrowedPage<'a>>,
}
impl<'a> Iterator for VmallocPageIter<'a> {
type Item = page::BorrowedPage<'a>;
fn next(&mut self) -> Option<Self::Item> {
let offset = self.index.checked_mul(page::PAGE_SIZE)?;
if offset < self.size() {
self.index += 1;
} else {
return None;
}
let ptr = unsafe { self.buf.add(offset) };
Some(unsafe { Vmalloc::to_page(ptr) })
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.page_count().saturating_sub(self.index);
(remaining, Some(remaining))
}
}
impl<'a> VmallocPageIter<'a> {
pub unsafe fn new(buf: NonNull<u8>, size: usize) -> Self {
Self {
buf,
size,
index: 0,
_p: PhantomData,
}
}
#[inline]
pub fn size(&self) -> usize {
self.size
}
#[inline]
pub fn page_count(&self) -> usize {
self.size().div_ceil(page::PAGE_SIZE)
}
}