mmapcell

Crates.iommapcell
lib.rsmmapcell
version0.1.2
sourcesrc
created_at2024-10-10 17:30:27.376704
updated_at2024-10-10 17:36:44.559534
descriptiona simple wrapper for the memmap2 crate to cast mmap backed pointers to structs
homepagehttps://github.com/mostlymaxi/mmapcell
repositoryhttps://github.com/mostlymaxi/mmapcell
max_upload_size
id1404067
size7,343
maxi (mostlymaxi)

documentation

https://docs.rs/mmapcell

README

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 cell = unsafe {
       MmapCell::<MyStruct>::new_named("/tmp/mystruct-mmap-test.bin")
   }.unwrap();

   let mmap_backed_mystruct = cell.get_mut();

   mmap_backed_mystruct.thing1 = 3;
Commit count: 5

cargo fmt