Skip to content

Commit

Permalink
clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Aug 5, 2024
1 parent 3286f59 commit 206cf5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/storage/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,24 @@ impl<M: Memory> Storage for StableStorage<M> {

// Insert of update a selected file chunk with the data provided in a buffer.
fn write_filechunk(&mut self, node: Node, index: FileChunkIndex, offset: FileSize, buf: &[u8]) {

if let Some(memory) = self.active_mounts.get(&node) {

// work with memory diriectly
// grow memory if needed
let max_address = index as FileSize * FILE_CHUNK_SIZE as FileSize + offset as FileSize + buf.len() as FileSize;
let pages_required = (max_address + WASM_PAGE_SIZE_IN_BYTES - 1) / WASM_PAGE_SIZE_IN_BYTES;
let max_address = index as FileSize * FILE_CHUNK_SIZE as FileSize
+ offset as FileSize
+ buf.len() as FileSize;
let pages_required =
(max_address + WASM_PAGE_SIZE_IN_BYTES - 1) / WASM_PAGE_SIZE_IN_BYTES;

let cur_pages = memory.size();

if cur_pages < pages_required {
memory.grow(pages_required - cur_pages);
memory.grow(pages_required - cur_pages);
}

// store data
let address = index as FileSize * FILE_CHUNK_SIZE as FileSize + offset as FileSize;

memory.write(address, buf);
} else {
// work with stable structure
Expand Down
8 changes: 5 additions & 3 deletions src/storage/transient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ impl Storage for TransientStorage {
// Insert of update a selected file chunk with the data provided in buffer.
fn write_filechunk(&mut self, node: Node, index: FileChunkIndex, offset: FileSize, buf: &[u8]) {
if let Some(memory) = self.get_mounted_memory(node) {

// work with memory diriectly
// grow memory if needed
let max_address = index as FileSize * FILE_CHUNK_SIZE as FileSize + offset as FileSize + buf.len() as FileSize;
let pages_required = (max_address + WASM_PAGE_SIZE_IN_BYTES - 1) / WASM_PAGE_SIZE_IN_BYTES;
let max_address = index as FileSize * FILE_CHUNK_SIZE as FileSize
+ offset as FileSize
+ buf.len() as FileSize;
let pages_required =
(max_address + WASM_PAGE_SIZE_IN_BYTES - 1) / WASM_PAGE_SIZE_IN_BYTES;

let cur_pages = memory.size();

Expand Down

0 comments on commit 206cf5e

Please sign in to comment.