tlsf/ops/
util.rs

1use crate::block::{Anchor, FreeBlock, UsedBlock};
2use crate::consts;
3use crate::header::Header;
4
5pub fn round_up_block_size(num: u16) -> Option<u16> {
6    let multiple = consts::BLOCK_ALIGN as u16;
7    let rem = num % multiple;
8    if rem == 0 {
9        Some(num)
10    } else {
11        u16::try_from((num as u32) + (multiple as u32 - rem as u32)).ok()
12    }
13}
14
15impl<const FLL: usize> Header<FLL> {
16    pub(super) unsafe fn adjust_free_block_size<'a>(
17        &mut self,
18        anchor: Anchor<'a>,
19        block: FreeBlock<'a>,
20        size: u16,
21    ) -> FreeBlock<'a> {
22        if u32::from(block.usable_size()) >= u32::from(size) + u32::from(FreeBlock::HEADER_SIZE) {
23            #[cfg(all(test, not(miri)))]
24            cov_mark::hit!(alloc_adjust_size);
25
26            let at = usize::from(UsedBlock::HEADER_SIZE) + usize::from(size);
27            let new = unsafe { anchor.split(&block, at) };
28            unsafe { self.push(anchor, new) }
29        }
30
31        block
32    }
33}