From c26824b017861d6cbe04b82d8df884406af6712f Mon Sep 17 00:00:00 2001 From: hky1999 <976929993@qq.com> Date: Mon, 7 Apr 2025 10:29:35 +0800 Subject: [PATCH] [feat] extend MemoryAddr trait, add pageiter for huge pages --- memory_addr/src/addr.rs | 8 +++++++- memory_addr/src/lib.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/memory_addr/src/addr.rs b/memory_addr/src/addr.rs index b846572..80207d3 100644 --- a/memory_addr/src/addr.rs +++ b/memory_addr/src/addr.rs @@ -1,4 +1,6 @@ use core::cmp::Ord; +use core::fmt::{Debug, LowerHex}; +use core::marker::{Send, Sync}; /// A trait for memory address types. /// @@ -17,6 +19,7 @@ pub trait MemoryAddr: + Into // The address type should be comparable. + Ord + + Send + Sync + Debug + LowerHex { // No required methods for now. Following are some utility methods. @@ -260,7 +263,10 @@ pub trait MemoryAddr: /// Implement the `MemoryAddr` trait for any type that is `Copy`, `From`, /// `Into`, and `Ord`. -impl MemoryAddr for T where T: Copy + From + Into + Ord {} +impl MemoryAddr for T where + T: Copy + From + Into + Ord + Send + Sync + Debug + LowerHex +{ +} /// Creates a new address type by wrapping an `usize`. /// diff --git a/memory_addr/src/lib.rs b/memory_addr/src/lib.rs index 1ede0ed..c6faa07 100644 --- a/memory_addr/src/lib.rs +++ b/memory_addr/src/lib.rs @@ -12,9 +12,21 @@ pub use self::range::{AddrRange, PhysAddrRange, VirtAddrRange}; /// The size of a 4K page (4096 bytes). pub const PAGE_SIZE_4K: usize = 0x1000; +/// The size of a 2M page (2097152 bytes). +pub const PAGE_SIZE_2M: usize = 0x200000; + +/// The size of a 1G page (1073741824 bytes). +pub const PAGE_SIZE_1G: usize = 0x40000000; + /// A [`PageIter`] for 4K pages. pub type PageIter4K = PageIter; +/// A [`PageIter`] for 2M pages. +pub type PageIter2M = PageIter; + +/// A [`PageIter`] for 1G pages. +pub type PageIter1G = PageIter; + /// Align address downwards. /// /// Returns the greatest `x` with alignment `align` so that `x <= addr`.