Skip to content

Access to non-available memory areas #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 2, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/memory_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ pub struct MemoryMapTag {
impl MemoryMapTag {
/// Return an iterator over all AVAILABLE marked memory areas.
pub fn memory_areas(&self) -> MemoryAreaIter {
Copy link
Member

@IsaacWoods IsaacWoods Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if, rather than having two iteratory types, this could return impl Iterator<Item = &MemoryArea> (maybe you'll need a lifetime idk) and then the body could be self.all_memory_areas().filter(|entry| entry.typ == 1)? I think that'd be cleaner, if it works.

Copy link
Contributor Author

@CalebLBaker CalebLBaker Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be cleaner and it does work. Initially I was worried that filter wouldn't work without the standard library (I'm using this crate for work on an OS kernel where I don't have the standard library and I'm new to working without the standard library, so I haven't yet figured out what is part of the standard library vs what is part of core), so I chose to avoid using filter initially. On further investigation, it seems to work fine, so I will update my pull request to make use of fileter.

MemoryAreaIter {
iter: self.all_memory_areas(),
}
}
/// Return an iterator over all marked memory areas.
pub fn all_memory_areas(&self) -> AllMemoryAreaIter {
let self_ptr = self as *const MemoryMapTag;
let start_area = (&self.first_area) as *const MemoryArea;
MemoryAreaIter {
AllMemoryAreaIter {
current_area: start_area as u64,
last_area: (self_ptr as u64 + (self.size - self.entry_size) as u64),
entry_size: self.entry_size,
Expand Down Expand Up @@ -91,26 +97,42 @@ pub enum MemoryAreaType {
Defective,
}

/// An iterator over Available memory areas.
/// An iterator over all memory areas
#[derive(Clone, Debug)]
pub struct MemoryAreaIter<'a> {
pub struct AllMemoryAreaIter<'a> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EFIMemoryMap also could use an iterator over all memory areas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like EFIMemoryMapTag already iterates over all memory areas.

current_area: u64,
last_area: u64,
entry_size: u32,
phantom: PhantomData<&'a MemoryArea>,
}

impl<'a> Iterator for MemoryAreaIter<'a> {
impl<'a> Iterator for AllMemoryAreaIter<'a> {
type Item = &'a MemoryArea;
fn next(&mut self) -> Option<&'a MemoryArea> {
if self.current_area > self.last_area {
None
} else {
let area = unsafe{&*(self.current_area as *const MemoryArea)};
self.current_area = self.current_area + (self.entry_size as u64);
if area.typ == 1 {
Some(area)
} else {self.next()}
Some(area)
}
}
}

/// An iterator over Available memory areas.
#[derive(Clone, Debug)]
pub struct MemoryAreaIter<'a> {
iter: AllMemoryAreaIter<'a>,
}

impl<'a> Iterator for MemoryAreaIter<'a> {
type Item = &'a MemoryArea;
fn next(&mut self) -> Option<&'a MemoryArea> {
let ret = self.iter.next()?;
if ret.typ == 1 {
Some(ret)
} else {
self.next()
}
}
}
Expand Down