Skip to content

Commit de2147c

Browse files
fix(lab/3): code base not synced
Co-authored-by: GZTime <[email protected]>
1 parent c5bedf5 commit de2147c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/0x03/pkg/kernel/src/proc/process.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ impl Process {
4444
pub fn new(
4545
name: String,
4646
parent: Option<Weak<Process>>,
47-
page_table: PageTableContext,
47+
proc_vm: Option<ProcessVm>,
4848
proc_data: Option<ProcessData>,
4949
) -> Arc<Self> {
5050
let name = name.to_ascii_lowercase();
5151

5252
// create context
5353
let pid = ProcessId::new();
54+
let proc_vm = proc_vm.unwrap_or_else(|| ProcessVm::new(PageTableContext::new()));
5455

5556
let inner = ProcessInner {
5657
name,
@@ -60,7 +61,7 @@ impl Process {
6061
ticks_passed: 0,
6162
exit_code: None,
6263
children: Vec::new(),
63-
page_table: Some(page_table),
64+
proc_vm: Some(proc_vm),
6465
proc_data: Some(proc_data.unwrap_or_default()),
6566
};
6667

@@ -117,7 +118,7 @@ impl ProcessInner {
117118
}
118119

119120
pub fn clone_page_table(&self) -> PageTableContext {
120-
self.page_table.as_ref().unwrap().clone_level_4()
121+
self.proc_vm.as_ref().unwrap()
121122
}
122123

123124
pub fn is_ready(&self) -> bool {

src/0x03/pkg/kernel/src/utils/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,31 @@ fn wait(pid: ProcessId) {
6161
}
6262
}
6363
}
64+
65+
const SHORT_UNITS: [&str; 4] = ["B", "K", "M", "G"];
66+
const UNITS: [&str; 4] = ["B", "KiB", "MiB", "GiB"];
67+
68+
pub fn humanized_size(size: u64) -> (f32, &'static str) {
69+
humanized_size_impl(size, false)
70+
}
71+
72+
pub fn humanized_size_short(size: u64) -> (f32, &'static str) {
73+
humanized_size_impl(size, true)
74+
}
75+
76+
#[inline]
77+
pub fn humanized_size_impl(size: u64, short: bool) -> (f32, &'static str) {
78+
let bytes = size as f32;
79+
80+
let units = if short { &SHORT_UNITS } else { &UNITS };
81+
82+
let mut unit = 0;
83+
let mut bytes = bytes;
84+
85+
while bytes >= 1024f32 && unit < units.len() {
86+
bytes /= 1024f32;
87+
unit += 1;
88+
}
89+
90+
(bytes, units[unit])
91+
}

0 commit comments

Comments
 (0)