Skip to main content

Module fmmap

Module fmmap 

Source
Expand description

Custom implementation of mmap(2)

§Constraints

FrozenMMap treats the mapped file as raw storage for values of T. Because of that, T must be a POD type which is safe to persist and later reinterpret from bytes.

Required properties for T,

  • Must use #[repr(C)]
  • Should be 8-bytes aligned
  • Must not implement Drop
  • size_of::<T>() should be multiple of 8

NOTE: T must not contain heap owning or process-local pointers like Vec, String, Box, references and function pointers, or other fields whose bit-pattern is not stable across reopen.

These constrains are enforced as FrozenMMap does not serialize or deserialize values. It directly reads and writes T inside a memory mapped file. That means T must have a stable layout and must remain valid when the file is reopened in a later process.

§Example

use frozen_core::fmmap::{FrozenMMap, FMCfg};

const MODULE_ID: u8 = 0;

let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tmp_frozen_mmap");

let cfg = FMCfg {
    initial_count: 0x0A,
    flush_duration: std::time::Duration::from_micros(0x96),
};

let mmap = FrozenMMap::<u64, MODULE_ID>::new(&path, cfg.clone()).unwrap();
assert_eq!(mmap.total_slots(), 0x0A);

let epoch = unsafe { mmap.write(0, |v| *v = 0xDEADC0DE) }.unwrap();
mmap.wait_for_durability(epoch).unwrap();

let val = unsafe { mmap.read(0, |v| *v) }.unwrap();
assert_eq!(val, 0xDEADC0DE);

drop(mmap);

let reopened = FrozenMMap::<u64, MODULE_ID>::new_grown(&path, cfg, 0x05).unwrap();
assert_eq!(reopened.total_slots(), 0x0A + 0x05);

let val = unsafe { reopened.read(0, |v| *v) }.unwrap();
assert_eq!(val, 0xDEADC0DE);

Structs§

FMCfg
Config for FrozenMMap
FMTransaction
A context for grouping multi write ops into a single atomic operation
FrozenMMap
Custom implementation of mmap(2)

Type Aliases§

TEpoch
type for epoch used by write ops