From 7a748cadbb2e2ce8c0e045cb8fbd77ccbd47459f Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Tue, 17 Oct 2023 23:07:21 -0700 Subject: initial commit --- src/block_device/memory_disk.rs | 34 ++++++++++++++++++++++++++++++++++ src/block_device/mod.rs | 7 +++++++ 2 files changed, 41 insertions(+) create mode 100644 src/block_device/memory_disk.rs create mode 100644 src/block_device/mod.rs (limited to 'src/block_device') diff --git a/src/block_device/memory_disk.rs b/src/block_device/memory_disk.rs new file mode 100644 index 0000000..4cffaf2 --- /dev/null +++ b/src/block_device/memory_disk.rs @@ -0,0 +1,34 @@ +use std::cell::RefCell; +use crate::block_device::{BLOCK_SIZE, BlockDevice}; + +#[repr(C)] +pub struct MemoryDisk { + /// Emulating a block device with a segment of RAM, + /// which is 64MiB == 4KiB per block * 16384 blocks + pub arena: RefCell>, +} + +impl MemoryDisk { + pub fn new() -> Self { + Self { + arena: RefCell::new(vec![0u8; BLOCK_SIZE * 16384]), + } + } + +} + +impl BlockDevice for MemoryDisk { + fn read(&self, block_id: usize, buffer: &mut [u8]) { + let block_front = block_id * BLOCK_SIZE; + let block_back = block_front + BLOCK_SIZE; + let arena = self.arena.borrow(); + buffer.copy_from_slice(&arena[block_front..block_back]); + } + + fn write(&self, block_id: usize, buffer: &[u8]) { + let block_front = block_id * BLOCK_SIZE; + let block_back = block_front + BLOCK_SIZE; + let mut arena = self.arena.borrow_mut(); + arena[block_front..block_back].copy_from_slice(buffer); + } +} \ No newline at end of file diff --git a/src/block_device/mod.rs b/src/block_device/mod.rs new file mode 100644 index 0000000..9d52e53 --- /dev/null +++ b/src/block_device/mod.rs @@ -0,0 +1,7 @@ +pub mod memory_disk; + +pub const BLOCK_SIZE: usize = 4096; +pub trait BlockDevice { + fn read(&self, block_id: usize, buffer: &mut [u8]); + fn write(&self, block_id: usize, buffer: &[u8]); +} -- cgit v1.2.3-70-g09d2