use rushm::posixaccessor::POSIXShm;
use rushm::posixslicedaccessor::POSIXShmSliced;
use std::mem;
fn main() {
let shm_name = "test_shared_memory_file";
println!("Testing shared memory at {}", shm_name);
unsafe {
let mut posix_shm = POSIXShm::<u64>::new(shm_name.to_string(), std::mem::size_of::<u64>());
let ret = posix_shm.open();
if ret.is_err() {
println!("Error opening shared memory");
return;
}
let val: u64 = 0xAA;
println!("Writing value {}", val);
let ret = posix_shm.write(val);
if ret.is_err() {
println!("Error writing to shared memory");
return;
}
let ret = posix_shm.read();
if ret.is_err() {
println!("Error reading from shared memory");
return;
}
let ret = ret.unwrap();
if *ret != val {
println!("Error reading from shared memory");
return;
}
println!("Read value {}", *ret);
let ret = posix_shm.close(true);
if ret.is_err() {
println!("Error closing shared memory");
return;
}
}
let shm_name = "test_sliced_shared_memory_file";
println!("Testing sliced shared memory at {}", shm_name);
unsafe {
const NUM_ELEMENTS: usize = 2;
const MEM_SIZE: usize = mem::size_of::<u64>() * NUM_ELEMENTS;
let mut sliced_shm =
POSIXShmSliced::<u64, NUM_ELEMENTS>::new(shm_name.to_string(), MEM_SIZE);
let ret = sliced_shm.open();
if ret.is_err() {
println!("Error opening shared memory");
return;
}
let data = [1u64, 2u64];
let ptr_slice: *mut u64 = sliced_shm.get_as_mut();
for i in 0..NUM_ELEMENTS {
println!("Writing value {} at index {}", data[i], i);
*ptr_slice.add(i) = data[i];
}
let ptr_sliced = sliced_shm.read();
if ptr_sliced.is_err() {
println!("Error reading from shared memory");
return;
}
let ptr_sliced = ptr_sliced.unwrap();
for i in 0..NUM_ELEMENTS {
println!("Read value {} at index {}", ptr_sliced[i], i);
}
let ret = sliced_shm.close(true);
if ret.is_err() {
println!("Error closing shared memory");
return;
}
}
}