Skip to main content

offset_allocator_2/
ext.rs

1//! Extension functions not present in the original C++ `OffsetAllocator`.
2
3use crate::{small_float, Allocator};
4
5/// Returns the minimum allocator size needed to hold an object of the given
6/// size.
7pub fn min_allocator_size(needed_object_size: u32) -> u32 {
8    small_float::float_to_uint(small_float::uint_to_float_round_up(needed_object_size))
9}
10
11impl Allocator {
12    /// Returns true if the allocator is empty (i.e. all storage is free).
13    pub fn is_empty(&self) -> bool {
14        self.free_storage == self.size
15    }
16}
17
18#[cfg(test)]
19mod tests {
20    use crate::*;
21
22    #[test]
23    fn ext_is_empty() {
24        let mut allocator: Allocator<u32> = Allocator::new(1024);
25        assert!(allocator.is_empty());
26
27        let allocation = allocator.allocate(1).unwrap();
28        assert!(!allocator.is_empty());
29
30        allocator.free(allocation);
31        assert!(allocator.is_empty());
32    }
33}