brotli 9.1.4

A brotli compressor and decompressor that with an interface avoiding the rust stdlib. This makes it suitable for embedded devices and kernels. It is designed with a pluggable allocator so that the standard lib's allocator may be employed. The default build also includes a stdlib allocator and stream interface. Disable this with --features=no-stdlib. All included code is safe.
Documentation
use crate::memory as alloc;
use crate::memory::AllocError;
use crate::memory::{alloc_default, try_alloc_cell};
use crate::memory::{Allocator, SliceWrapper, SliceWrapperMut};

pub struct BlockSplit<Alloc: alloc::Allocator<u8> + alloc::Allocator<u32>> {
    pub num_types: usize,
    pub num_blocks: usize,
    pub types: <Alloc as Allocator<u8>>::AllocatedMemory,
    pub lengths: <Alloc as Allocator<u32>>::AllocatedMemory,
}

impl<Alloc: alloc::Allocator<u8> + alloc::Allocator<u32>> Default for BlockSplit<Alloc> {
    fn default() -> Self {
        Self {
            num_types: 0,
            num_blocks: 0,
            types: alloc_default::<u8, Alloc>(),
            lengths: alloc_default::<u32, Alloc>(),
        }
    }
}

impl<Alloc: alloc::Allocator<u8> + alloc::Allocator<u32>> BlockSplit<Alloc> {
    pub fn new() -> BlockSplit<Alloc> {
        Self::default()
    }
    pub fn destroy(&mut self, m: &mut Alloc) {
        <Alloc as Allocator<u8>>::free_cell(m, core::mem::take(&mut self.types));
        <Alloc as Allocator<u32>>::free_cell(m, core::mem::take(&mut self.lengths));
        self.num_blocks = 0;
        self.num_types = 0;
    }
    pub fn types_alloc_size(&self) -> usize {
        self.types.slice().len()
    }
    pub fn lengths_alloc_size(&self) -> usize {
        self.lengths.slice().len()
    }

    pub(crate) fn try_reserve(
        &mut self,
        alloc: &mut Alloc,
        num_blocks: usize,
    ) -> Result<(), AllocError> {
        try_grow::<u8, _>(alloc, &mut self.types, num_blocks)?;
        try_grow::<u32, _>(alloc, &mut self.lengths, num_blocks)
    }
}

fn try_grow<T: Copy, A: Allocator<T>>(
    alloc: &mut A,
    buffer: &mut A::AllocatedMemory,
    min_len: usize,
) -> Result<(), AllocError> {
    let old_len = buffer.slice().len();
    if old_len >= min_len {
        return Ok(());
    }
    let mut new_len = if old_len == 0 { min_len } else { old_len };
    while new_len < min_len {
        new_len = new_len.checked_mul(2).ok_or(AllocError)?;
    }
    let mut new_buffer = try_alloc_cell::<T, _>(alloc, new_len)?;
    new_buffer.slice_mut()[..old_len].copy_from_slice(buffer.slice());
    alloc.free_cell(core::mem::replace(buffer, new_buffer));
    Ok(())
}