-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrushm-example.rs
84 lines (72 loc) · 2.4 KB
/
rushm-example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
}
}
// Test sliced shared memory
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;
}
}
}