haz_alloc_internal/
lib.rs

1#![no_std]
2
3#[cfg(test)]
4mod tests;
5
6use core::mem;
7
8pub const SMALL_CLASSES: &[usize] = &[
9    mem::size_of::<usize>(),
10    8,
11    // Incremented by 16:
12    16,
13    16 * 2,
14    16 * 3,
15    16 * 4,
16    16 * 5,
17    16 * 6,
18    // Incremented by 64:
19    64 * 2,
20    64 * 3,
21    64 * 4,
22    64 * 5,
23    64 * 6,
24    64 * 7,
25    64 * 8,
26    // Incremented by 256:
27    256 * 3,
28    256 * 4,
29    256 * 5,
30    256 * 6,
31    256 * 7,
32];
33
34pub const SMALL_MAX: usize = 256 * 7;
35
36pub fn small_class_of(size: usize) -> usize {
37    if size <= mem::size_of::<usize>() {
38        0
39    } else if size <= 8 {
40        1
41    } else if size <= 16 * 6 {
42        2 + size.align_up(16) / 16 - 1
43    } else if size <= 64 * 8 {
44        8 + size.align_up(64) / 64 - 2
45    } else {
46        15 + size.align_up(256) / 256 - 3
47    }
48}
49
50pub trait UsizeExt {
51    fn align_up(self, multiple: usize) -> Self;
52    fn align_down(self, multiple: usize) -> Self;
53}
54
55impl UsizeExt for usize {
56    #[inline]
57    fn align_up(self, multiple: usize) -> Self {
58        (self + multiple - 1) / multiple * multiple
59    }
60
61    #[inline]
62    fn align_down(self, multiple: usize) -> Self {
63        self / multiple * multiple
64    }
65}