dyn_stack/
stack_req.rs

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use core::{
    alloc::{Layout, LayoutError},
    num::NonZeroUsize,
};

/// Stack allocation requirements.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct StackReq {
    align: Option<NonZeroUsize>,
    size: usize,
}

impl Default for StackReq {
    #[inline]
    fn default() -> Self {
        Self::empty()
    }
}

#[inline(always)]
const fn try_round_up_pow2(a: usize, b: usize) -> Option<usize> {
    match a.checked_add(!b.wrapping_neg()) {
        None => None,
        Some(x) => Some(x & b.wrapping_neg()),
    }
}

#[inline(always)]
const fn max(a: usize, b: usize) -> usize {
    if a > b {
        a
    } else {
        b
    }
}

impl StackReq {
    /// Allocation requirements for an empty unaligned buffer.
    pub const EMPTY: Self = Self {
        align: unsafe { Some(NonZeroUsize::new_unchecked(1)) },
        size: 0,
    };

    /// Unsatisfiable allocation requirements.
    pub const OVERFLOW: Self = Self {
        align: None,
        size: 0,
    };

    /// Allocation requirements for an empty unaligned buffer.
    #[inline]
    pub const fn empty() -> StackReq {
        Self::EMPTY
    }

    /// Allocation requirements sufficient for `n` elements of type `T`, overaligned with alignment
    /// `align`.
    ///
    /// # Errors
    ///
    /// * if `align` is smaller than the minimum required alignment for an object of type `T`.
    /// * if `align` is not a power of two.
    /// * if the size computation overflows
    #[inline]
    pub const fn new_aligned<T>(n: usize, align: usize) -> StackReq {
        if align >= core::mem::align_of::<T>() && align.is_power_of_two() {
            StackReq {
                align: unsafe { Some(NonZeroUsize::new_unchecked(align)) },
                size: core::mem::size_of::<T>(),
            }
            .array(n)
        } else {
            StackReq {
                align: None,
                size: 0,
            }
        }
    }

    /// Allocation requirements sufficient for `n` elements of type `T`.
    ///
    /// # Errors
    ///
    /// * if the size computation overflows
    #[inline]
    pub const fn new<T>(n: usize) -> StackReq {
        StackReq::new_aligned::<T>(n, core::mem::align_of::<T>())
    }

    /// The number of allocated bytes required, aligned to `self.align_bytes()`.
    #[inline]
    pub const fn size_bytes(&self) -> usize {
        self.size
    }

    /// The alignment of allocated bytes required, or `0` if the size overflowed.
    #[inline]
    pub const fn align_bytes(&self) -> usize {
        match self.align {
            Some(align) => align.get(),
            None => 0,
        }
    }

    /// The number of allocated bytes required, with no alignment constraints, or `usize::MAX` in the case of overflow.
    ///
    /// # Panics
    ///
    /// * if the size computation overflowed
    #[inline]
    pub const fn unaligned_bytes_required(&self) -> usize {
        match self.layout() {
            Ok(layout) => layout.size() + (layout.align() - 1),
            Err(_) => usize::MAX,
        }
    }

    /// Returns the corresponding layout for the allocation size and alignment.
    #[inline]
    pub const fn layout(self) -> Result<Layout, LayoutError> {
        Layout::from_size_align(self.size_bytes(), self.align_bytes())
    }

    /// The required allocation to allocate storage sufficient for both of `self` and `other`,
    /// simultaneously and in any order.
    ///
    /// # Panics
    ///
    /// * if the allocation requirement computation overflows.
    #[inline]
    pub const fn and(self, other: StackReq) -> StackReq {
        match (self.align, other.align) {
            (Some(left), Some(right)) => {
                let align = max(left.get(), right.get());
                let left = try_round_up_pow2(self.size, align);
                let right = try_round_up_pow2(other.size, align);

                match (left, right) {
                    (Some(left), Some(right)) => {
                        match left.checked_add(right) {
                            Some(size) => StackReq {
                                // SAFETY: align is either self.align or other.align, both of which are non zero
                                align: unsafe { Some(NonZeroUsize::new_unchecked(align)) },
                                size,
                            },
                            _ => StackReq::OVERFLOW,
                        }
                    }
                    _ => StackReq::OVERFLOW,
                }
            }
            _ => StackReq::OVERFLOW,
        }
    }

    /// The required allocation to allocate storage sufficient for all the requirements produced by
    /// the given iterator, simultaneously and in any order.
    ///
    /// # Panics
    ///
    /// * if the allocation requirement computation overflows.
    #[inline]
    pub const fn all_of(reqs: &[Self]) -> Self {
        let mut total = StackReq::EMPTY;
        let mut reqs = reqs;
        while let Some((req, next)) = reqs.split_first() {
            total = total.and(*req);
            reqs = next;
        }
        total
    }

    /// The required allocation to allocate storage sufficient for either of `self` and `other`,
    /// with only one being active at a time.
    ///
    /// # Panics
    ///
    /// * if the allocation requirement computation overflows.
    #[inline]
    pub const fn or(self, other: StackReq) -> StackReq {
        match (self.align, other.align) {
            (Some(left), Some(right)) => {
                let align = max(left.get(), right.get());
                let left = try_round_up_pow2(self.size, align);
                let right = try_round_up_pow2(other.size, align);

                match (left, right) {
                    (Some(left), Some(right)) => {
                        let size = max(left, right);
                        StackReq {
                            // SAFETY: align is either self.align or other.align, both of which are non zero
                            align: unsafe { Some(NonZeroUsize::new_unchecked(align)) },
                            size,
                        }
                    }
                    _ => StackReq::OVERFLOW,
                }
            }
            _ => StackReq::OVERFLOW,
        }
    }

    /// The required allocation to allocate storage sufficient for any of the requirements produced
    /// by the given iterator, with at most one being active at a time.
    ///
    /// # Panics
    ///
    /// * if the allocation requirement computation overflows.
    #[inline]
    pub fn any_of(reqs: &[StackReq]) -> StackReq {
        let mut total = StackReq::EMPTY;
        let mut reqs = reqs;
        while let Some((req, next)) = reqs.split_first() {
            total = total.or(*req);
            reqs = next;
        }
        total
    }

    /// Same as [`StackReq::and`] repeated `n` times.
    #[inline]
    pub const fn array(self, n: usize) -> StackReq {
        match self.align {
            Some(align) => {
                let size = self.size.checked_mul(n);
                match size {
                    Some(size) => StackReq {
                        size,
                        align: Some(align),
                    },
                    None => StackReq::OVERFLOW,
                }
            }
            None => StackReq::OVERFLOW,
        }
    }
}

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

    #[test]
    fn round_up() {
        assert_eq!(try_round_up_pow2(0, 4), Some(0));
        assert_eq!(try_round_up_pow2(1, 4), Some(4));
        assert_eq!(try_round_up_pow2(2, 4), Some(4));
        assert_eq!(try_round_up_pow2(3, 4), Some(4));
        assert_eq!(try_round_up_pow2(4, 4), Some(4));
    }

    #[test]
    fn overflow() {
        assert_eq!(StackReq::new::<u32>(usize::MAX).align_bytes(), 0);
    }

    #[test]
    fn and_overflow() {
        assert_eq!(
            StackReq::new::<u8>(usize::MAX)
                .and(StackReq::new::<u8>(1))
                .align_bytes(),
            0,
        );
    }
}