1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#[cfg(test)]
use crate::block::Anchor;
#[cfg(test)]
use crate::block::FreeBlock;
use crate::block::Offset;
use crate::consts;

#[repr(align(4))]
pub struct Header<const FLL: usize> {
    fl_bitmap: u16,
    sl_bitmaps: [u16; FLL],
    free_lists: [FirstLevel; FLL],
}

impl<const FLL: usize> Header<FLL> {
    pub const fn new() -> Self {
        const FREE_LIST: FreeList = None;
        const FIRST_LEVEL: FirstLevel = [FREE_LIST; consts::SLL as usize];

        Self {
            fl_bitmap: 0,
            sl_bitmaps: [0; FLL],
            free_lists: [FIRST_LEVEL; FLL],
        }
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn get_free_list(&self, fl: u8, sl: u8) -> FreeList {
        #[cfg(any(fuzzing, test))]
        debug_assert!(usize::from(fl) < FLL);
        #[cfg(any(fuzzing, test))]
        debug_assert!(sl < consts::SLL);

        *self
            .free_lists
            .get_unchecked(usize::from(fl))
            .get_unchecked(usize::from(sl))
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn set_free_list(&mut self, fl: u8, sl: u8, free_list: FreeList) {
        #[cfg(any(fuzzing, test))]
        debug_assert!(usize::from(fl) < FLL);
        #[cfg(any(fuzzing, test))]
        debug_assert!(sl < consts::SLL);

        *self
            .free_lists
            .get_unchecked_mut(usize::from(fl))
            .get_unchecked_mut(usize::from(sl)) = free_list;
    }

    pub fn clear_fl_bit(&mut self, fl: u8) {
        self.fl_bitmap &= !1u16.wrapping_shl(fl.into());
    }

    pub fn set_fl_bit(&mut self, fl: u8) {
        self.fl_bitmap |= 1u16.wrapping_shl(fl.into());
    }

    #[cfg(test)]
    pub fn is_fl_bit_set(&self, fl: u8) -> bool {
        let mask = 1u16.wrapping_shl(fl.into());
        self.fl_bitmap & mask == mask
    }

    pub fn suitable_fls(&self, fl: u8) -> u16 {
        let mask = (!0u16).wrapping_shl(fl.into());
        self.fl_bitmap & mask
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn get_sl_bitmap(&self, fl: u8) -> u16 {
        *self.sl_bitmap(fl)
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn clear_sl_bit(&mut self, fl: u8, sl: u8) {
        let sl_bitmap = self.sl_bitmap_mut(fl);
        *sl_bitmap &= !1u16.wrapping_shl(sl.into());
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn set_sl_bit(&mut self, fl: u8, sl: u8) {
        let sl_bitmap = self.sl_bitmap_mut(fl);
        *sl_bitmap |= 1u16.wrapping_shl(sl.into());
    }

    /// # Safety
    /// - caller must perform bounds checks
    #[cfg(test)]
    pub unsafe fn is_sl_bit_set(&self, fl: u8, sl: u8) -> bool {
        let mask = 1u16.wrapping_shl(sl.into());
        *self.sl_bitmap(fl) & mask == mask
    }

    pub unsafe fn suitable_sls(&self, fl: u8, sl: u8) -> u16 {
        let sl_bitmap = self.get_sl_bitmap(fl);
        let mask = (!0u16).wrapping_shl(sl.into());
        sl_bitmap & mask
    }

    /// # Safety
    /// - caller must perform bounds checks
    pub unsafe fn is_sl_empty(&self, fl: u8) -> bool {
        self.get_sl_bitmap(fl) == 0
    }

    /// # Safety
    /// - caller must perform bounds checks
    unsafe fn sl_bitmap(&self, fl: u8) -> &u16 {
        #[cfg(any(fuzzing, test))]
        debug_assert!(usize::from(fl) < FLL);

        self.sl_bitmaps.get_unchecked(usize::from(fl))
    }

    /// # Safety
    /// - caller must perform bounds checks
    unsafe fn sl_bitmap_mut(&mut self, fl: u8) -> &mut u16 {
        #[cfg(any(fuzzing, test))]
        debug_assert!(usize::from(fl) < FLL);

        self.sl_bitmaps.get_unchecked_mut(usize::from(fl))
    }

    #[cfg(test)]
    pub unsafe fn linked_free_blocks<'a>(&self, anchor: Anchor<'a>) -> Vec<FreeBlock<'a>> {
        let mut blocks = vec![];

        for free_lists in self.free_lists {
            for mut free_list in free_lists {
                while let Some(offset) = free_list {
                    let block = unsafe { anchor.get_free_block(offset) };

                    free_list = block.get_next_free();
                    blocks.push(block);
                }
            }
        }

        blocks
    }
}

type FirstLevel = [FreeList; consts::SLL as usize];
type FreeList = Option<Offset>;

#[cfg(test)]
mod tests {
    use super::Header;

    #[test]
    fn fl_bitmap_roundtrip() {
        let mut header = Header::<1>::new();
        assert!(!header.is_fl_bit_set(0));
        header.set_fl_bit(0);
        assert!(header.is_fl_bit_set(0));
    }

    #[test]
    fn sl_bitmap_roundtrip() {
        let mut header = Header::<1>::new();
        unsafe {
            assert!(!header.is_sl_bit_set(0, 0));
            header.set_sl_bit(0, 0);
            assert!(header.is_sl_bit_set(0, 0));
        }
    }
}