Skip to content

Commit

Permalink
简化了代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiichen committed Aug 20, 2023
1 parent 9e01cef commit d7f9a54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
6 changes: 3 additions & 3 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,18 +653,18 @@ impl Syscall {
SYS_CLOCK => Self::clock(),
SYS_PIPE => {
let pipefd = args[0] as *mut c_int;

match UserBufferWriter::new(
pipefd,
core::mem::size_of::<[c_int; 2]>() as usize,
from_user,
) {
Err(e) => Err(e),
Ok(mut user_buffer) => match user_buffer.get_buffer::<i32>(0) {
Ok(mut user_buffer) => match user_buffer.buffer::<i32>(0) {
Err(e) => Err(e),
Ok(pipefd) => Self::pipe(pipefd),
},
}

}

SYS_UNLINK_AT => {
Expand Down Expand Up @@ -862,7 +862,7 @@ impl Syscall {
) {
Err(e) => Err(e),
Ok(mut user_buffer_writer) => {
match user_buffer_writer.get_buffer::<crate::net::syscall::MsgHdr>(0) {
match user_buffer_writer.buffer::<crate::net::syscall::MsgHdr>(0) {
Err(e) => Err(e),
Ok(buffer) => {
let msg = &mut buffer[0];
Expand Down
16 changes: 5 additions & 11 deletions kernel/src/syscall/user_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ pub fn check_and_clone_cstr_array(user: *const *const u8) -> Result<Vec<String>,
#[derive(Debug)]
pub struct UserBufferWriter<'a> {
buffer: &'a mut [u8],
len: usize,
}

#[derive(Debug)]
pub struct UserBufferReader<'a> {
buffer: &'a [u8],
len: usize,
}

impl<'a> UserBufferReader<'a> {
Expand All @@ -169,8 +167,7 @@ impl<'a> UserBufferReader<'a> {
return Err(SystemError::EFAULT);
}
return Ok(Self {
buffer: unsafe { core::slice::from_raw_parts(addr as *const u8, len) },
len,
buffer: unsafe { core::slice::from_raw_parts(addr as *const u8, len) }
});
}

Expand Down Expand Up @@ -294,8 +291,7 @@ impl<'a> UserBufferWriter<'a> {
return Ok(Self {
buffer: unsafe {
core::slice::from_raw_parts_mut(addr as *mut u8, len * core::mem::size_of::<U>())
},
len,
}
});
}

Expand Down Expand Up @@ -378,11 +374,9 @@ impl<'a> UserBufferWriter<'a> {
}
}

pub fn get_buffer<T>(&'a mut self, offset: usize) -> Result<&mut [T], SystemError> {
match Self::convert_with_offset(self.buffer, offset) {
Err(_) => return Err(SystemError::EINVAL),
Ok(buffer) => return Ok(buffer),
}
pub fn buffer<T>(&'a mut self, offset: usize) -> Result<&mut [T], SystemError> {
Ok(Self::convert_with_offset::<T>(self.buffer, offset).map_err(|e|SystemError::EINVAL)?)

}

fn convert_with_offset<T>(src: &mut [u8], offset: usize) -> Result<&mut [T], SystemError> {
Expand Down

0 comments on commit d7f9a54

Please sign in to comment.