Skip to content

Commit

Permalink
add ALO1 marker
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Aug 30, 2024
1 parent 526a841 commit 8cc7834
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pub enum Error {
ExpectedToRemoveDirectory,
CannotRemoveOpenedNode,
CannotRemoveMountedMemoryFile,
IncompatibleChunkSize,
IncompatibleChunkSize,
InvalidMagicMarker,
}
4 changes: 2 additions & 2 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
pub use crate::runtime::fd::Fd;

pub use crate::runtime::types::{
DstBuf, DstIoVec, FdFlags, FdStat, OpenFlags, SrcBuf, SrcIoVec, Whence, ChunkSize
ChunkSize, DstBuf, DstIoVec, FdFlags, FdStat, OpenFlags, SrcBuf, SrcIoVec, Whence,
};

// The main class implementing the API to work with the file system.
Expand Down Expand Up @@ -290,7 +290,7 @@ impl FileSystem {

Ok(())
}

// Get the metadata for a given file descriptor
pub fn metadata_from_node(&self, node: Node) -> Result<Metadata, Error> {
self.storage.get_metadata(node)
Expand Down
65 changes: 62 additions & 3 deletions src/storage/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ impl<M: Memory> ChunkPtrAllocator<M> {
if v2_available_chunks.size() == 0 {
v2_available_chunks.grow(1);

// TODO: here write the allocator marker
v2_available_chunks.write(0, &0u64.to_le_bytes());
// write the magic marker
let b = [b'A', b'L', b'O', b'1', 0, 0, 0, 0];
v2_available_chunks.write(0, &b);

v2_available_chunks.write(8, &0u64.to_le_bytes());
v2_available_chunks.write(16, &0u64.to_le_bytes());
v2_available_chunks.write(24, &0u64.to_le_bytes());
} else {
// check the marker
let mut b = [0u8; 4];
v2_available_chunks.read(0, &mut b);

if b != *b"ALO1" {
return Err(Error::InvalidMagicMarker);
}
}

let mut allocator = ChunkPtrAllocator {
Expand Down Expand Up @@ -137,7 +146,8 @@ impl<M: Memory> ChunkPtrAllocator<M> {
// new size must be one of the available values

if !ChunkSize::VALUES
.iter().any(|size| *size as usize == new_size)
.iter()
.any(|size| *size as usize == new_size)
{
return Err(Error::IncompatibleChunkSize);
}
Expand Down Expand Up @@ -420,6 +430,55 @@ mod tests {
assert_eq!(allocator.chunk_size(), chunk_size);
}

#[test]
fn alo1_marker_is_written() {
let mem = new_vector_memory();
let memory_manager = MemoryManager::init(mem);
let mut allocator = ChunkPtrAllocator::new(memory_manager.get(MemoryId::new(1))).unwrap();

assert_eq!(allocator.allocate(), 0);
allocator.free(0);

let memory = memory_manager.get(MemoryId::new(1));
let mut b = [0u8; 4];

memory.read(0, &mut b);
assert_eq!(&b[0..4], b"ALO1");
}

#[test]
fn correct_alo1_marker_is_accepted() {
let mem = new_vector_memory();
let memory_manager = MemoryManager::init(mem);
let mut allocator = ChunkPtrAllocator::new(memory_manager.get(MemoryId::new(1))).unwrap();

assert_eq!(allocator.allocate(), 0);
allocator.free(0);

let res = ChunkPtrAllocator::new(memory_manager.get(MemoryId::new(1)));

assert!(res.is_ok());
}

#[test]
fn wrong_alo1_marker_is_rejected() {
let mem = new_vector_memory();
let memory_manager = MemoryManager::init(mem);
let mut allocator = ChunkPtrAllocator::new(memory_manager.get(MemoryId::new(1))).unwrap();

assert_eq!(allocator.allocate(), 0);
allocator.free(0);

let memory = memory_manager.get(MemoryId::new(1));
let b = [0u8; 1];

memory.write(0, &b);

let res = ChunkPtrAllocator::new(memory_manager.get(MemoryId::new(1)));

assert!(res.is_err());
}

#[test]
fn same_custom_chunk_size_succeeds() {
let mem = new_vector_memory();
Expand Down

0 comments on commit 8cc7834

Please sign in to comment.