Skip to content

Commit

Permalink
Mem: Format output into table for fmt::Display
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 5, 2025
1 parent a4ad013 commit 9017c0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bootloader/stage-64bit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn main(stage_to_stage: &Stage32toStage64) {
})
.expect("Unable to add stage64 to memory map");

mm.iter().for_each(|region| logln!("{:x?}", region));
logln!("{}", mm);
}

let elf = Elf::new(unsafe {
Expand Down
23 changes: 23 additions & 0 deletions crates/mem/src/phys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 util::bytes::HumanBytes;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PhysMemoryKind {
None,
Expand Down Expand Up @@ -358,6 +360,27 @@ impl<const N: usize> PhysMemoryMap<N> {
}
}

impl<const N: usize> core::fmt::Display for PhysMemoryMap<N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(
"| Kind | Start | End | Size |\n",
)?;
f.write_str(
"+--------------+------------------+------------------+-------------------+\n",
)?;
for region in self.iter() {
f.write_fmt(format_args!(
"| {:?}\t | {:#016x} | {:#016x} | {:>16} |\n",
region.kind,
region.start,
region.end,
HumanBytes::from(region.end - region.start)
))?;
}
f.write_str("+--------------+------------------+------------------+-------------------+\n")
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 9017c0f

Please sign in to comment.