我需要在 C 和 Rust 应用程序之间共享内存。Rust - 生产者,C - 消费者。
在 CI 中会创建一个共享内存区域并将其传递给 Rust 进行写入。
在C端我使用这样的东西:
fd = shm_open(STORAGE_ID, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
res = ftruncate(fd, STORAGE_SIZE);
addr = mmap(NULL, STORAGE_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
// read from that memory after it is written by Rust app
char data[STORAGE_SIZE];
memcpy(data, addr, STORAGE_SIZE);
Run Code Online (Sandbox Code Playgroud)
我在 Rust 方面该怎么做才能使用 STORAGE_ID 打开内存并写入它?我想它是沿着使用这个的路线,但找不到可靠的例子:
https://docs.rs/libc/0.2.77/libc/fn.shm_open.html
https://docs.rs/memmap/0.6.1/memmap/struct.Mmap.html
https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.clone_from_slice
基本上,我需要做这样的事情,但是用 Rust 编写的 set.c: POSIX 共享内存 IPC 示例(shm_open、mmap),在 Linux 和 macOS 上工作