Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgoergens committed Dec 10, 2024
1 parent 7f67dae commit 9280a9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 19 additions & 13 deletions ceno_host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ use rkyv::{
util::AlignedVec,
};

// We want to get access to the default value of `AlignedVec::ALIGNMENT`, and using it directly like this
// pub const RKVY_ALIGNMENT: usize = rkyv::util::AlignedVec::ALIGNMENT;
// doesn't work:
pub const RKYV_ALIGNMENT: usize = {
type AlignedVec = rkyv::util::AlignedVec;
AlignedVec::ALIGNMENT
};

#[derive(Default)]
pub struct CenoStdin {
pub items: Vec<AlignedVec>,
Expand All @@ -26,32 +34,30 @@ impl CenoStdin {
}

pub fn finalise(&self) -> Vec<u32> {
// TODO: perhaps don't hardcode 16 here.
// It's from rkyv's format, so we can probably take it from there somehow?
// TODO: clean this up.
let initial_offset = (size_of::<u32>() * self.items.len()).next_multiple_of(16);
// println!("offset: {}", initial_offset);
let initial_offset = (size_of::<u32>() * self.items.len()).next_multiple_of(RKYV_ALIGNMENT);
let offsets: Vec<u32> = self
.items
.iter()
.scan(initial_offset, |acc, bytes| {
let output = (*acc + bytes.len()) as u32;
// print!("len: {}\t", bytes.len());
*acc += bytes.len().next_multiple_of(16);
// println!("acc: {}", *acc);
*acc += bytes.len().next_multiple_of(RKYV_ALIGNMENT);
Some(output)
})
.collect();
let offsets_u8: Vec<u8> = offsets.iter().copied().flat_map(u32::to_le_bytes).collect();
let mut buf: AlignedVec = AlignedVec::new();
buf.extend_from_slice(&offsets_u8);
// println!("buf.len() after offsets: {}", buf.len());
buf.extend_from_slice(&vec![0; buf.len().next_multiple_of(16) - buf.len()]);
// println!("buf.len() after offset padding: {}", buf.len());
buf.extend_from_slice(&vec![
0;
buf.len().next_multiple_of(RKYV_ALIGNMENT) - buf.len()
]);
for (offset, item) in izip!(offsets, &self.items) {
buf.extend_from_slice(item);
buf.extend_from_slice(&vec![0; buf.len().next_multiple_of(16) - buf.len()]);
assert_eq!(buf.len(), offset.next_multiple_of(16) as usize);
buf.extend_from_slice(&vec![
0;
buf.len().next_multiple_of(RKYV_ALIGNMENT) - buf.len()
]);
assert_eq!(buf.len(), offset.next_multiple_of(RKYV_ALIGNMENT) as usize);
}
let (prefix, hints, postfix): (_, &[u32], _) = unsafe { buf.align_to() };
assert_eq!(prefix, &[]);
Expand Down

0 comments on commit 9280a9f

Please sign in to comment.