Allow writing to ShmPool and initialize shm to all 1's

This commit is contained in:
2025-04-25 00:43:34 -07:00
parent 0fa9864a1c
commit d0bb89e393

View File

@@ -1,4 +1,4 @@
use libc::{c_void, ftruncate, mmap, munmap, shm_open, shm_unlink, MAP_FAILED, MAP_SHARED, O_CREAT, O_EXCL, O_RDWR, PROT_READ};
use libc::{c_void, ftruncate, mmap, munmap, shm_open, shm_unlink, MAP_FAILED, MAP_SHARED, O_CREAT, O_EXCL, O_RDWR, PROT_READ, PROT_WRITE};
#[derive(Clone)]
pub struct ShmPool {
@@ -25,7 +25,7 @@ impl ShmPool {
return Err(std::io::Error::last_os_error())
}
let addr = unsafe {
mmap(std::ptr::null_mut(), size, PROT_READ | PROT_READ, MAP_SHARED, fd, 0)
mmap(std::ptr::null_mut(), size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)
};
if addr == MAP_FAILED {
@@ -33,6 +33,8 @@ impl ShmPool {
return Err(std::io::Error::last_os_error())
}
unsafe { std::ptr::copy_nonoverlapping(vec![u8::MAX; size].as_ptr(), addr as *mut u8, size); }
Ok(ShmPool {
fd,
id,
@@ -49,6 +51,19 @@ impl ShmPool {
Ok(())
}
pub fn write(&mut self, data: &Vec<u8>, offset: usize) -> std::io::Result<()> {
// TODO: Bounds check
unsafe {
std::ptr::copy_nonoverlapping(
data.as_ptr(), // src: data as *const u8
self.addr as *mut u8, // dst: ShmPool address as *mut u8
data.len()
);
}
Ok(())
}
}
impl Drop for ShmPool {