#mmap #memmap2 #utility

mmapcell

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

3 unstable releases

0.2.2 Nov 13, 2024
0.1.2 Oct 10, 2024
0.1.1 Oct 10, 2024

#466 in Memory management

Download history 194/week @ 2024-10-05 100/week @ 2024-10-12 6/week @ 2024-10-19 18/week @ 2024-10-26 12/week @ 2024-11-02 102/week @ 2024-11-09 21/week @ 2024-11-16 3/week @ 2024-11-23

142 downloads per month
Used in 2 crates (via disk-mpmc)

MIT license

7KB
76 lines

MmapCell

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 mmapcell::MmapCell;

   #[repr(C)]
   struct MyStruct {
      thing1: i32,
      thing2: f64,
   }

   let mut cell = unsafe {
       MmapCell::<MyStruct>::new_named("/tmp/mystruct-mmap-test.bin")
   }.unwrap();

   let mmap_backed_mystruct = cell.get_mut();

   mmap_backed_mystruct.thing1 = 3;

Dependencies

~0.4–0.9MB
~19K SLoC