#mmap #memory-layout #memmap2 #memory-region #utility #struct

no-std mmap-wrapper

a simple wrapper for the memmap2 crate to cast mmap backed pointers to structs

5 releases (3 stable)

2.0.1 Sep 8, 2024
1.1.0 Sep 7, 2024
1.0.1 Sep 5, 2024
0.9.10 Aug 26, 2024
0.9.9 Aug 2, 2024

#177 in Memory management


Used in disk-ringbuffer

MIT license

16KB
253 lines

mmap wrapper

A common use case for mmap in C is to cast the mmap backed region to a struct:

MyStruct* mmap_backed_mystruct;
int fd;

fd = open(path, O_RDWR | O_CREAT, 0644);
ftruncate(fd, sizeof(MyStruct));

mmap_backed_mystruct = (MyStruct*)mmap(0, sizeof(MyStruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Example

This is a helpful wrapper for the same usecase:

use mmap_wrapper::MmapWrapper;

// Your struct MUST have a consistent memory layout.
// Use either #[repr(transparent)] or #[repr(C)].
#[repr(C)]
struct MyStruct {
   thing1: i32,
   thing2: f64,
}

let f = std::fs::File::options()
    .read(true)
    .write(true)
    .create(true)
    .truncate(false)
    .open("/tmp/mystruct-mmap-test.bin")
    .unwrap();

let _ = f.set_len(std::mem::size_of::<MyStruct>() as u64);

let m = unsafe {
    memmap2::Mmap::map(&f).unwrap()
};

let m_wrapper = unsafe { MmapWrapper::<MyStruct>::new(m) };
let mmap_backed_mystruct = m_wrapper.get_inner();

no_std Example

use mmap_wrapper::MmapWrapper;

// Your struct MUST have a consistent memory layout.
// Use either #[repr(transparent)] or #[repr(C)].
#[repr(C)]
struct MyStruct {
   thing1: i32,
   thing2: f64,
}

let m_wrapper = unsafe { MmapWrapper::<MyStruct>::new(c"/tmp/mystruct-mmap-test.bin").unwrap() };
let mmap_backed_mystruct = m_wrapper.get_inner();

Dependencies

~170KB