Rewrite Stdin/Stdout
This commit is contained in:
parent
f5c029d3db
commit
2e6734027d
8 changed files with 338 additions and 42 deletions
|
@ -138,9 +138,7 @@ impl PageTable {
|
|||
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
|
||||
self.find_pte(va.clone().floor())
|
||||
.map(|pte| {
|
||||
//println!("translate_va:va = {:?}", va);
|
||||
let aligned_pa: PhysAddr = pte.ppn().into();
|
||||
//println!("translate_va:pa_align = {:?}", aligned_pa);
|
||||
let offset = va.page_offset();
|
||||
let aligned_pa_usize: usize = aligned_pa.into();
|
||||
(aligned_pa_usize + offset).into()
|
||||
|
@ -189,9 +187,60 @@ pub fn translated_str(token: usize, ptr: *const u8) -> String {
|
|||
}
|
||||
|
||||
pub fn translated_refmut<T>(token: usize, ptr: *mut T) -> &'static mut T {
|
||||
//println!("into translated_refmut!");
|
||||
let page_table = PageTable::from_token(token);
|
||||
let va = ptr as usize;
|
||||
//println!("translated_refmut: before translate_va");
|
||||
page_table.translate_va(VirtAddr::from(va)).unwrap().get_mut()
|
||||
}
|
||||
|
||||
pub struct UserBuffer {
|
||||
pub buffers: Vec<&'static mut [u8]>,
|
||||
}
|
||||
|
||||
impl UserBuffer {
|
||||
pub fn new(buffers: Vec<&'static mut [u8]>) -> Self {
|
||||
Self { buffers }
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
let mut total: usize = 0;
|
||||
for b in self.buffers.iter() {
|
||||
total += b.len();
|
||||
}
|
||||
total
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for UserBuffer {
|
||||
type Item = *mut u8;
|
||||
type IntoIter = UserBufferIterator;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
UserBufferIterator {
|
||||
buffers: self.buffers,
|
||||
current_buffer: 0,
|
||||
current_idx: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UserBufferIterator {
|
||||
buffers: Vec<&'static mut [u8]>,
|
||||
current_buffer: usize,
|
||||
current_idx: usize,
|
||||
}
|
||||
|
||||
impl Iterator for UserBufferIterator {
|
||||
type Item = *mut u8;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_buffer >= self.buffers.len() {
|
||||
None
|
||||
} else {
|
||||
let r = &mut self.buffers[self.current_buffer][self.current_idx] as *mut _;
|
||||
if self.current_idx + 1 == self.buffers[self.current_buffer].len() {
|
||||
self.current_idx = 0;
|
||||
self.current_buffer += 1;
|
||||
} else {
|
||||
self.current_idx += 1;
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue