Skip to content

Commit

Permalink
Box the memory block size
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Aug 31, 2024
1 parent ffa7550 commit 821ede9
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ struct MemBlock
page_size: usize,

// Currently visible/accessible size
cur_size: usize,
// This is a box because we need a pointer
// To access this value from threads using MemView
cur_size: Box<usize>,
}

impl MemBlock
Expand Down Expand Up @@ -469,7 +471,7 @@ impl MemBlock
mem_block: unsafe { transmute(mem_block) },
mapping_size: alloc_size,
page_size,
cur_size: 0,
cur_size: Box::new(0),
}
}

Expand All @@ -487,15 +489,16 @@ impl MemBlock
}
assert!(new_size % self.page_size == 0);

let cur_size = *self.cur_size;

// Growing the memory block, need to map as read | write
if new_size <= self.cur_size {
return self.cur_size;
if new_size <= cur_size {
return cur_size;
}

let start_ofs = self.cur_size;
let map_addr = unsafe { transmute(self.mem_block.add(start_ofs)) };
let map_addr = unsafe { transmute(self.mem_block.add(cur_size)) };

let map_size = new_size - self.cur_size;
let map_size = new_size - cur_size;
assert!(map_size % self.page_size == 0);

let mem_block = unsafe {libc::mmap(
Expand All @@ -512,42 +515,52 @@ impl MemBlock
}

// Update the currently accessible size
self.cur_size = new_size;
*self.cur_size = new_size;

new_size
}


// TODO: method to create MemView





// Create a new thread-local view on this memory block
fn new_view(&self) -> MemView
{
MemView {
mem_block: self.mem_block,
cur_size: &*self.cur_size,
}
}
}


struct MemView
{
// Underlying memory block
mem_block: *mut u8,

// TODO: pointer to atomic var


// Pointer to size variable from parent MemBlock
cur_size: *const usize,
}

impl MemView
{



// TODO:

/*
/// Write a value at the given address
pub fn write<T>(&mut self, pos: usize, val: T) where T: Copy
{
unsafe {
let buf_ptr = self.data.as_mut_ptr();
let val_ptr = transmute::<*mut u8 , *mut T>(buf_ptr.add(pos));
std::ptr::write_unaligned(val_ptr, val);
}
}
*/


/*
/// Read a value at the current PC and then increment the PC
pub fn read_pc<T>(&self, pc: &mut usize) -> T where T: Copy
pub fn read_at_pc<T>(&self, pc: &mut usize) -> T where T: Copy
{
unsafe {
let buf_ptr = self.data.as_ptr();
Expand Down

0 comments on commit 821ede9

Please sign in to comment.