pub use rarena_allocator::Freelist;
use rarena_allocator::Options as ArenaOptions;
pub(super) const CURRENT_VERSION: u16 = 0;
pub(super) const MAGIC_TEXT: [u8; 6] = *b"valog!";
pub(super) const MAGIC_TEXT_SIZE: usize = MAGIC_TEXT.len();
pub(super) const MAGIC_VERSION_SIZE: usize = core::mem::size_of::<u16>();
pub(super) const HEADER_SIZE: usize = MAGIC_TEXT_SIZE + MAGIC_VERSION_SIZE;
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
mod open_options;
mod builder;
pub use builder::*;
#[viewit::viewit(vis_all = "pub(super)", getters(skip), setters(skip))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Options {
max_value_size: u32,
magic_version: u16,
capacity: Option<u32>,
unify: bool,
freelist: Freelist,
reserved: u32,
lock_meta: bool,
sync: bool,
validate_checksum: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
create_new: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
create: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
read: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
write: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
append: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
truncate: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
offset: u64,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
stack: bool,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
huge: Option<u8>,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
populate: bool,
}
impl Default for Options {
#[inline]
fn default() -> Options {
Options::new()
}
}
impl Options {
#[inline]
pub const fn new() -> Self {
Self {
max_value_size: u32::MAX,
capacity: None,
unify: false,
magic_version: 0,
freelist: Freelist::None,
reserved: 0,
lock_meta: false,
sync: true,
validate_checksum: true,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
create_new: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
create: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
read: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
write: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
append: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
truncate: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
offset: 0,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
stack: false,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
huge: None,
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
populate: false,
}
}
#[inline]
pub const fn with_reserved(mut self, reserved: u32) -> Self {
self.reserved = reserved;
self
}
#[inline]
pub const fn with_sync(mut self, sync: bool) -> Self {
self.sync = sync;
self
}
#[inline]
pub const fn with_validate_checksum(mut self, validate_checksum: bool) -> Self {
self.validate_checksum = validate_checksum;
self
}
#[inline]
pub const fn with_lock_meta(mut self, lock_meta: bool) -> Self {
self.lock_meta = lock_meta;
self
}
#[inline]
pub const fn with_magic_version(mut self, magic_version: u16) -> Self {
self.magic_version = magic_version;
self
}
#[inline]
pub const fn with_freelist(mut self, freelist: Freelist) -> Self {
self.freelist = freelist;
self
}
#[inline]
pub const fn with_unify(mut self, unify: bool) -> Self {
self.unify = unify;
self
}
#[inline]
pub const fn with_maximum_value_size(mut self, size: u32) -> Self {
self.max_value_size = size;
self
}
#[inline]
pub const fn with_capacity(mut self, capacity: u32) -> Self {
self.capacity = Some(capacity);
self
}
#[inline]
pub const fn reserved(&self) -> u32 {
self.reserved
}
#[inline]
pub const fn sync(&self) -> bool {
self.sync
}
#[inline]
pub const fn validate_checksum(&self) -> bool {
self.validate_checksum
}
#[inline]
pub const fn lock_meta(&self) -> bool {
self.lock_meta
}
#[inline]
pub const fn maximum_value_size(&self) -> u32 {
self.max_value_size
}
#[inline]
pub const fn capacity(&self) -> u32 {
match self.capacity {
Some(capacity) => capacity,
None => 0,
}
}
#[inline]
pub const fn unify(&self) -> bool {
self.unify
}
#[inline]
pub const fn magic_version(&self) -> u16 {
self.magic_version
}
#[inline]
pub const fn freelist(&self) -> Freelist {
self.freelist
}
}
impl Options {
#[allow(clippy::wrong_self_convention)]
#[inline]
pub(super) const fn to_arena_options(&self) -> ArenaOptions {
let opts = ArenaOptions::new()
.with_magic_version(CURRENT_VERSION)
.with_reserved(HEADER_SIZE as u32 + self.reserved())
.with_unify(self.unify())
.maybe_capacity(self.capacity)
.with_freelist(self.freelist());
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
{
opts
.with_lock_meta(self.lock_meta())
.with_create(self.create())
.with_create_new(self.create_new())
.with_read(self.read())
.with_write(self.write())
.with_append(self.append())
.with_truncate(self.truncate())
.with_offset(self.offset())
.with_stack(self.stack())
.with_huge(self.huge())
.with_populate(self.populate())
}
#[cfg(not(all(feature = "memmap", not(target_family = "wasm"))))]
opts
}
}
#[inline]
fn write_header(buf: &mut [u8], magic_version: u16) {
buf[..MAGIC_TEXT_SIZE].copy_from_slice(&MAGIC_TEXT);
buf[MAGIC_TEXT_SIZE..MAGIC_TEXT_SIZE + MAGIC_VERSION_SIZE]
.copy_from_slice(&magic_version.to_le_bytes());
}