use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use memmap2::MmapMut;
use crate::error::{Error, Result};
use crate::layout::{
self, RawHeader, BUFFER_OFFSET, FRAME_ALIGN, MAGIC, PUBLISHED_OFFSET, RESERVED_OFFSET, VERSION,
};
pub(crate) struct Mapping {
_map: MmapMut,
base: *mut u8,
capacity: u64,
instance: u64,
file: Option<File>,
}
fn new_instance() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or_default()
}
unsafe impl Send for Mapping {}
unsafe impl Sync for Mapping {}
impl Mapping {
fn total_len(capacity: u64) -> usize {
BUFFER_OFFSET + capacity as usize
}
pub(crate) fn create(path: &Path, capacity: u64) -> Result<Self> {
if !layout::capacity_ok(capacity) {
return Err(Error::BadCapacity(capacity));
}
let tmp = Self::temp_path(path)?;
let built = Self::build(&tmp, capacity).and_then(|this| {
std::fs::rename(&tmp, path)?;
Ok(this)
});
if built.is_err() {
let _ = std::fs::remove_file(&tmp);
}
built
}
fn temp_path(path: &Path) -> Result<PathBuf> {
let dir = match path.parent() {
Some(p) if !p.as_os_str().is_empty() => p,
_ => Path::new("."),
};
let name = path.file_name().ok_or_else(|| {
Error::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"queue path does not name a file",
))
})?;
Ok(dir.join(format!(
".{}.{}.{}.tmp",
name.to_string_lossy(),
std::process::id(),
new_instance()
)))
}
fn build(path: &Path, capacity: u64) -> Result<Self> {
let file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(path)?;
file.set_len(Self::total_len(capacity) as u64)?;
let mut map = unsafe { MmapMut::map_mut(&file)? };
map.fill(0);
let base = map.as_mut_ptr();
let this = Self {
_map: map,
base,
capacity,
instance: new_instance(),
file: Some(file),
};
this.write_header();
Ok(this)
}
pub(crate) fn anon(capacity: u64) -> Result<Self> {
if !layout::capacity_ok(capacity) {
return Err(Error::BadCapacity(capacity));
}
let mut map = MmapMut::map_anon(Self::total_len(capacity))?;
let base = map.as_mut_ptr();
let this = Self {
_map: map,
base,
capacity,
instance: new_instance(),
file: None,
};
this.write_header();
Ok(this)
}
pub(crate) fn open(path: &Path) -> Result<Self> {
let file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)?;
let len = file.metadata()?.len() as usize;
if len < BUFFER_OFFSET {
return Err(Error::BadHeader("file too small for a header"));
}
let mut map = unsafe { MmapMut::map_mut(&file)? };
let base = map.as_mut_ptr();
let magic = unsafe { (*(base as *const AtomicU64)).load(Ordering::Acquire) };
if magic != MAGIC {
return Err(Error::BadHeader("magic mismatch"));
}
let header: RawHeader = unsafe { base.cast::<RawHeader>().read_unaligned() };
if header.version != VERSION {
return Err(Error::BadHeader("layout version mismatch"));
}
if header.frame_align != FRAME_ALIGN as u32 {
return Err(Error::BadHeader("frame alignment mismatch"));
}
if !layout::capacity_ok(header.capacity) {
return Err(Error::BadHeader("capacity is not a power of two"));
}
if len < Self::total_len(header.capacity) {
return Err(Error::BadHeader("file shorter than its declared capacity"));
}
let this = Self {
_map: map,
base,
capacity: header.capacity,
instance: header.instance,
file: Some(file),
};
let published = this.published().load(Ordering::Acquire);
let reserved = this.reserved().load(Ordering::Acquire);
if !published.is_multiple_of(FRAME_ALIGN as u64)
|| !reserved.is_multiple_of(FRAME_ALIGN as u64)
{
return Err(Error::BadHeader("position is not frame-aligned"));
}
if reserved < published || reserved - published > header.capacity {
return Err(Error::BadHeader(
"reservation does not cover the published end",
));
}
Ok(this)
}
fn write_header(&self) {
let header = RawHeader {
magic: 0, version: VERSION,
frame_align: FRAME_ALIGN as u32,
capacity: self.capacity,
instance: self.instance,
_reserved: [0; 4],
};
let bytes = bytemuck::bytes_of(&header);
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), self.base, bytes.len()) };
self.magic().store(MAGIC, Ordering::Release);
}
fn atomic_at(&self, offset: usize) -> &AtomicU64 {
debug_assert!(offset + 8 <= BUFFER_OFFSET);
unsafe { &*(self.base.add(offset) as *const AtomicU64) }
}
pub(crate) fn magic(&self) -> &AtomicU64 {
self.atomic_at(0)
}
pub(crate) fn published(&self) -> &AtomicU64 {
self.atomic_at(PUBLISHED_OFFSET)
}
pub(crate) fn reserved(&self) -> &AtomicU64 {
self.atomic_at(RESERVED_OFFSET)
}
pub(crate) fn capacity(&self) -> u64 {
self.capacity
}
pub(crate) fn instance(&self) -> u64 {
self.instance
}
pub(crate) fn claim_producer(&self) -> bool {
match &self.file {
Some(file) => file.try_lock().is_ok(),
None => true,
}
}
pub(crate) fn release_producer(&self) {
if let Some(file) = &self.file {
let _ = file.unlock();
}
}
pub(crate) fn mask(&self) -> u64 {
self.capacity - 1
}
#[cfg(test)]
pub(crate) fn poke_ring(&self, idx: usize, value: u64) {
assert!(
idx.is_multiple_of(FRAME_ALIGN),
"ring writes are word-aligned"
);
assert!(
idx + FRAME_ALIGN <= self.capacity as usize,
"inside the ring"
);
unsafe { crate::copy::write_word(self.buffer_ptr().add(idx), value) };
}
pub(crate) fn buffer_ptr(&self) -> *mut u8 {
unsafe { self.base.add(BUFFER_OFFSET) }
}
}