Skip to content

Commit

Permalink
zero copy static RO files
Browse files Browse the repository at this point in the history
  • Loading branch information
xor-bits committed Dec 23, 2023
1 parent 3ef4bfe commit 69e2ade
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
14 changes: 5 additions & 9 deletions crates/kshell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use hyperion_futures::keyboard::KeyboardEvents;
use hyperion_kernel_impl::VFS_ROOT;
use hyperion_log::*;
use hyperion_scheduler::lock::Mutex;
use hyperion_vfs::{error::IoError, path::PathBuf, ramdisk};
use hyperion_vfs::{error::IoError, path::PathBuf, ramdisk::StaticRoFile};
use snafu::Snafu;

use self::{shell::Shell, term::Term};
Expand Down Expand Up @@ -53,18 +53,14 @@ pub async fn kshell() {

for asset in ASSETS {
let (path, bytes): (&str, &[u8]) = *asset;
// TODO: no copy
VFS_ROOT.install_dev(path, ramdisk::File::new(bytes.into()));
VFS_ROOT.install_dev(path, StaticRoFile::new(bytes));
}

let bin = load_elf!("SAMPLE_ELF").into();
VFS_ROOT.install_dev("/bin/run", ramdisk::File::new(bin));
let bin = load_elf!("FBTEST").into();
VFS_ROOT.install_dev("/bin/fbtest", ramdisk::File::new(bin));
VFS_ROOT.install_dev("/bin/run", StaticRoFile::new(load_elf!("SAMPLE_ELF")));
VFS_ROOT.install_dev("/bin/fbtest", StaticRoFile::new(load_elf!("FBTEST")));

// everything is the same file
let bin = load_elf!("COREUTILS").into();
let bin = Arc::new(Mutex::new(ramdisk::File::new(bin)));
let bin = Arc::new(Mutex::new(StaticRoFile::new(load_elf!("COREUTILS"))));
VFS_ROOT.install_dev_ref("/bin/coreutils", bin.clone());
VFS_ROOT.install_dev_ref("/bin/cat", bin.clone());
VFS_ROOT.install_dev_ref("/bin/ls", bin.clone());
Expand Down
39 changes: 33 additions & 6 deletions crates/vfs/src/ramdisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloc::{
sync::Arc,
vec::Vec,
};
use core::any::Any;

use lock_api::Mutex;

Expand All @@ -23,6 +24,20 @@ impl File {
pub fn new(bytes: Vec<u8>) -> Self {
Self { bytes }
}

pub fn new_empty<Mut: AnyMutex>() -> FileRef<Mut> {
Arc::new(Mutex::new(Self { bytes: Vec::new() })) as _
}
}

pub struct StaticRoFile {
bytes: &'static [u8],
}

impl StaticRoFile {
pub const fn new(bytes: &'static [u8]) -> Self {
Self { bytes }
}
}

pub struct Directory<Mut: AnyMutex> {
Expand All @@ -36,7 +51,7 @@ pub struct Directory<Mut: AnyMutex> {
//

impl FileDevice for File {
fn as_any(&self) -> &dyn core::any::Any {
fn as_any(&self) -> &dyn Any {
self
}

Expand All @@ -45,19 +60,31 @@ impl FileDevice for File {
}

fn read(&self, offset: usize, buf: &mut [u8]) -> IoResult<usize> {
FileDevice::read(&self.bytes[..], offset, buf)
self.bytes.read(offset, buf)
}

fn write(&mut self, offset: usize, buf: &[u8]) -> IoResult<usize> {
self.bytes
.resize(self.bytes.len().max(buf.len() + offset), b'?');
FileDevice::write(&mut self.bytes[..], offset, buf)
self.bytes.write(offset, buf)
}
}

impl File {
pub fn new_empty<Mut: AnyMutex>() -> FileRef<Mut> {
Arc::new(Mutex::new(Self { bytes: Vec::new() })) as _
impl FileDevice for StaticRoFile {
fn as_any(&self) -> &dyn Any {
self
}

fn len(&self) -> usize {
self.bytes.len()
}

fn read(&self, offset: usize, buf: &mut [u8]) -> IoResult<usize> {
self.bytes.read(offset, buf)
}

fn write(&mut self, _: usize, _: &[u8]) -> IoResult<usize> {
Err(IoError::PermissionDenied)
}
}

Expand Down

0 comments on commit 69e2ade

Please sign in to comment.