Skip to content

Commit

Permalink
Mem: Convert table to enum and improve performance + start on PMM
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 11, 2025
1 parent 957065d commit 47506de
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 159 deletions.
1 change: 1 addition & 0 deletions crates/boolvec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use alloc::vec::Vec;

type BackingType = u64;

#[derive(Clone)]
pub struct BoolVec(Vec<BackingType>);

impl BoolVec {
Expand Down
16 changes: 16 additions & 0 deletions crates/mem/src/phys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ impl<const N: usize> PhysMemoryMap<N> {
bytes as usize
}

pub fn sdram_bytes(&self) -> usize {
let mut bytes = 0;
self.iter()
.filter(|r| match r.kind {
PhysMemoryKind::Free
| PhysMemoryKind::AcpiReclaimable
| PhysMemoryKind::Bootloader
| PhysMemoryKind::Kernel
| PhysMemoryKind::PageTables => true,
_ => false,
})
.for_each(|region| bytes += region.end - region.start);

bytes as usize
}

pub fn find_continuous_of(
&mut self,
from_kind: PhysMemoryKind,
Expand Down
39 changes: 38 additions & 1 deletion crates/mem/src/pmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use crate::{
MemoryError,
phys::{PhysMemoryKind, PhysMemoryMap},
};

extern crate alloc;

mod backing;
Expand All @@ -31,4 +36,36 @@ mod backing;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PhysPage(pub u64);

pub struct Pmm {}
pub struct Pmm {
table: backing::MemoryTable<backing::TableBits>,
}

impl Pmm {
pub fn new<const SIZE: usize>(memory_map: &PhysMemoryMap<SIZE>) -> Result<Self, MemoryError> {
let total_real_memory = memory_map.sdram_bytes() as u64;

let mut opt_table = *backing::OPT_TABLES.last().unwrap();
for table_size in backing::OPT_TABLES {
if total_real_memory < (table_size * backing::TABLE_SIZE as u64) {
opt_table = table_size;
}
}

let mut table = backing::MemoryTable::new(opt_table);

memory_map
.iter()
.filter(|entry| entry.kind == PhysMemoryKind::Free)
.try_for_each(|entry| table.populate_with(entry.start, entry.end).map(|_| ()))?;

Ok(Self { table })
}

pub fn allocate_page(&mut self) -> Result<PhysPage, MemoryError> {
self.table.request_page()
}

pub fn free_page(&mut self, page: PhysPage) -> Result<(), MemoryError> {
self.table.free_page(page)
}
}
Loading

0 comments on commit 47506de

Please sign in to comment.