Skip to content

Commit

Permalink
Add CryptoLog of layer 1-crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassong-mh committed Oct 26, 2023
1 parent c06ecb9 commit 3d8acb1
Show file tree
Hide file tree
Showing 10 changed files with 889 additions and 75 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ inherit-methods-macro = {git = "https://github.com/jinzhao-dev/inherit-methods-m
pod = {git = "https://github.com/jinzhao-dev/pod", rev = "7fa2ed2"}

anymap = {version = "0.12.1"}
array-init = "2.1.0"
bitvec = {version = "1.0.1", default-features = false, features = ["atomic", "alloc"]}
lending-iterator = "0.1.7"
libc = "0.2.147"
log = "0.4"
lru = "0.11.0"
openssl = "0.10.55"
serde = {version = "1.0", features = ["derive"]}
spin = "0.9.8"
static_assertions = "1.1.0"
typetag = "0.2"
openssl = "0.10.55"
libc = "0.2.147"
log = "0.4"
lending-iterator = "0.1.7"

[lib]
doctest = false
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl Error {
}
}

impl From<Errno> for Error {
fn from(errno: Errno) -> Self {
Error::new(errno)
}
}

#[macro_export]
macro_rules! return_errno {
($errno: expr) => {
Expand Down
88 changes: 87 additions & 1 deletion src/layers/0-bio/block_log.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::{BufMut, BufRef};
use super::{Buf, BufMut, BufRef};
use crate::os::Mutex;
use crate::prelude::*;

use core::sync::atomic::{AtomicUsize, Ordering};
use inherit_methods_macro::inherit_methods;

/// A log of data blocks that can support random reads and append-only
Expand Down Expand Up @@ -43,3 +45,87 @@ impl_blocklog_for!(&T, "(**self)");
impl_blocklog_for!(&mut T, "(**self)");
impl_blocklog_for!(Box<T>, "(**self)");
impl_blocklog_for!(Arc<T>, "(**self)");

/// An in-memory log that impls `BlockLog`.
pub struct MemLog {
log: Mutex<Buf>,
append_pos: AtomicUsize,
}

impl BlockLog for MemLog {
fn read(&self, pos: BlockId, mut buf: BufMut) -> Result<()> {
let nblocks = buf.nblocks();
if pos + nblocks > self.nblocks() {
return_errno_with_msg!(InvalidArgs, "read range out of bound");
}
let log = self.log.lock();
let read_buf = &log.as_slice()[Self::offset(pos)..Self::offset(pos) + nblocks * BLOCK_SIZE];
buf.as_mut_slice().copy_from_slice(&read_buf);
Ok(())
}

fn append(&self, buf: BufRef) -> Result<BlockId> {
let nblocks = buf.nblocks();
let mut log = self.log.lock();
let pos = self.append_pos.load(Ordering::Relaxed);
if pos + nblocks > log.nblocks() {
return_errno_with_msg!(InvalidArgs, "append range out of bound");
}
let write_buf =
&mut log.as_mut_slice()[Self::offset(pos)..Self::offset(pos) + nblocks * BLOCK_SIZE];
write_buf.copy_from_slice(buf.as_slice());
self.append_pos.fetch_add(nblocks, Ordering::Release);
Ok(pos)
}

fn flush(&self) -> Result<()> {
Ok(())
}

fn nblocks(&self) -> usize {
self.append_pos.load(Ordering::Relaxed)
}
}

impl MemLog {
pub fn create(num_blocks: usize) -> Result<Self> {
Ok(Self {
log: Mutex::new(Buf::alloc(num_blocks)?),
append_pos: AtomicUsize::new(0),
})
}

fn offset(pos: BlockId) -> usize {
pos * BLOCK_SIZE
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn mem_log() -> Result<()> {
let total_blocks = 64;
let append_nblocks = 8;
let mem_log = MemLog::create(total_blocks)?;
assert_eq!(mem_log.nblocks(), 0);

let mut append_buf = Buf::alloc(append_nblocks)?;
let content = 5_u8;
append_buf.as_mut_slice().fill(content);
let append_pos = mem_log.append(append_buf.as_ref())?;
assert_eq!(append_pos, 0);
assert_eq!(mem_log.nblocks(), append_nblocks);

mem_log.flush()?;
let mut read_buf = Buf::alloc(1)?;
let read_pos = 7 as BlockId;
mem_log.read(read_pos, read_buf.as_mut())?;
assert_eq!(
read_buf.as_slice(),
&append_buf.as_slice()[read_pos * BLOCK_SIZE..(read_pos + 1) * BLOCK_SIZE]
);
Ok(())
}
}
3 changes: 2 additions & 1 deletion src/layers/0-bio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ mod block_ring;
mod block_set;

pub use self::block_buf::{Buf, BufMut, BufRef};
pub use self::block_log::BlockLog;
pub use self::block_log::{BlockLog, MemLog};
pub use self::block_ring::BlockRing;
pub use self::block_set::{BlockSet, MemDisk};

pub type BlockId = usize;
pub const BLOCK_SIZE: usize = 0x1000;
pub const BID_SIZE: usize = core::mem::size_of::<BlockId>();

// This definition of BlockId assumes the target architecture is 64-bit
assert_eq_size!(usize, u64);
Loading

0 comments on commit 3d8acb1

Please sign in to comment.