offset-allocator-2 0.2.0

A fork of Patrick Walton's port of Sebastian Aaltonen's `OffsetAllocator` to Rust
Documentation
//! Extension functions not present in the original C++ `OffsetAllocator`.

use crate::{small_float, Allocator};

/// Returns the minimum allocator size needed to hold an object of the given
/// size.
pub fn min_allocator_size(needed_object_size: u32) -> u32 {
    small_float::float_to_uint(small_float::uint_to_float_round_up(needed_object_size))
}

impl Allocator {
    /// Returns true if the allocator is empty (i.e. all storage is free).
    pub fn is_empty(&self) -> bool {
        self.free_storage == self.size
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn ext_is_empty() {
        let mut allocator: Allocator<u32> = Allocator::new(1024);
        assert!(allocator.is_empty());

        let allocation = allocator.allocate(1).unwrap();
        assert!(!allocator.is_empty());

        allocator.free(allocation);
        assert!(allocator.is_empty());
    }
}