Skip to content

Commit

Permalink
Replaces KernelExt::fget_write with OwnedFile::from_fd
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Jul 22, 2024
1 parent 4a0dd43 commit 43390c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
23 changes: 0 additions & 23 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::file::OwnedFile;
use crate::socket::{OwnedSocket, SockAddr};
use crate::thread::Thread;
use crate::Kernel;
Expand All @@ -10,14 +9,6 @@ use core::ptr::null_mut;
///
/// This trait is automatically implemented for any type that implement [`Kernel`].
pub trait KernelExt: Kernel {
/// # Safety
/// `td` should not be null although the PS4 does not use it currently.
unsafe fn fget_write(
self,
td: *mut Self::Thread,
fd: c_int,
) -> Result<OwnedFile<Self>, NonZeroI32>;

/// # Safety
/// - `td` cannot be null.
unsafe fn socket(
Expand Down Expand Up @@ -50,20 +41,6 @@ pub trait KernelExt: Kernel {
}

impl<T: Kernel> KernelExt for T {
unsafe fn fget_write(
self,
td: *mut Self::Thread,
fd: c_int,
) -> Result<OwnedFile<Self>, NonZeroI32> {
let mut fp = null_mut();
let errno = self.fget_write(td, fd, 0, &mut fp);

match NonZeroI32::new(errno) {
Some(v) => Err(v),
None => Ok(OwnedFile::new(self, fp)),
}
}

unsafe fn socket(
self,
dom: c_int,
Expand Down
22 changes: 18 additions & 4 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::pcpu::Pcpu;
use crate::Kernel;
use core::ffi::c_int;
use core::num::NonZero;
use core::ptr::null_mut;
use core::sync::atomic::{fence, AtomicU32, Ordering};

/// Represents `file` structure.
Expand All @@ -10,16 +13,27 @@ pub trait File: Sized {

/// RAII struct to decrease `file::f_count` when dropped.
pub struct OwnedFile<K: Kernel> {
kernel: K,
kern: K,
file: *mut K::File,
}

impl<K: Kernel> OwnedFile<K> {
/// # Safety
/// `file` cannot be null and the caller must own a strong reference to it. This method do
/// **not** increase the reference count of this file.
pub unsafe fn new(kernel: K, file: *mut K::File) -> Self {
Self { kernel, file }
pub unsafe fn new(kern: K, file: *mut K::File) -> Self {
Self { kern, file }
}

pub fn from_fd(kern: K, fd: c_int) -> Result<Self, NonZero<c_int>> {
let td = K::Pcpu::curthread();
let mut fp = null_mut();
let errno = unsafe { kern.fget(td, fd, &mut fp, 0, null_mut()) };

match NonZero::new(errno) {
Some(v) => Err(v),
None => Ok(Self { kern, file: fp }),
}
}
}

Expand All @@ -33,6 +47,6 @@ impl<K: Kernel> Drop for OwnedFile<K> {
fence(Ordering::Acquire);

// The kernel itself does not check if fdrop is success so we don't need to.
unsafe { self.kernel.fdrop(self.file, K::Pcpu::curthread()) };
unsafe { self.kern.fdrop(self.file, K::Pcpu::curthread()) };
}
}

0 comments on commit 43390c8

Please sign in to comment.