bomalloc 0.3.0

A fragmentation-aware experimental allocator built around address-span ownership
Documentation
use crate::BoAllocError;

#[cfg(windows)]
mod windows;

#[cfg(windows)]
pub(crate) use windows::PlatformBacking;
#[cfg(windows)]
pub(crate) use windows::{release_metadata_block, reserve_commit_metadata_block};

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "linux")]
pub(crate) use linux::PlatformBacking;
#[cfg(target_os = "linux")]
pub(crate) use linux::{release_metadata_block, reserve_commit_metadata_block};

#[cfg(not(any(windows, target_os = "linux")))]
pub(crate) struct PlatformBacking;

#[cfg(not(any(windows, target_os = "linux")))]
pub(crate) unsafe fn reserve_commit_metadata_block(_: usize) -> Result<usize, BoAllocError> {
    Err(BoAllocError::UnsupportedPlatform)
}

#[cfg(not(any(windows, target_os = "linux")))]
pub(crate) unsafe fn release_metadata_block(_: usize) -> Result<(), BoAllocError> {
    Err(BoAllocError::UnsupportedPlatform)
}

pub(crate) trait BackingStore: Send + Sync + 'static {
    fn page_size(&self) -> usize;
    fn allocation_granularity(&self) -> usize;
    unsafe fn reserve(&self, size: usize) -> Result<usize, BoAllocError>;
    unsafe fn commit(&self, address: usize, size: usize) -> Result<(), BoAllocError>;
    unsafe fn decommit(&self, address: usize, size: usize) -> Result<(), BoAllocError>;
    /// Discards obsolete page contents without making the address range
    /// inaccessible. A later allocation must overwrite before reading.
    unsafe fn discard(&self, address: usize, size: usize) -> Result<(), BoAllocError>;
    unsafe fn release(&self, address: usize, size: usize) -> Result<(), BoAllocError>;
}

#[cfg(not(any(windows, target_os = "linux")))]
impl PlatformBacking {
    pub(crate) fn new() -> Result<Self, BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
}

#[cfg(not(any(windows, target_os = "linux")))]
impl BackingStore for PlatformBacking {
    fn page_size(&self) -> usize {
        4096
    }
    fn allocation_granularity(&self) -> usize {
        65536
    }
    unsafe fn reserve(&self, _: usize) -> Result<usize, BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
    unsafe fn commit(&self, _: usize, _: usize) -> Result<(), BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
    unsafe fn decommit(&self, _: usize, _: usize) -> Result<(), BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
    unsafe fn discard(&self, _: usize, _: usize) -> Result<(), BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
    unsafe fn release(&self, _: usize, _: usize) -> Result<(), BoAllocError> {
        Err(BoAllocError::UnsupportedPlatform)
    }
}