1use core::alloc::Layout as StdLayout;
2use core::mem;
34/// Aborts the process.
5///
6/// To abort, this function simply panics while panicking.
7pub(crate) fn abort() -> ! {
8struct Panic;
910impl Drop for Panic {
11fn drop(&mut self) {
12panic!("aborting the process");
13 }
14 }
1516let _panic = Panic;
17panic!("aborting the process");
18}
1920/// Calls a function and aborts if it panics.
21///
22/// This is useful in unsafe code where we can't recover from panics.
23#[inline]
24pub(crate) fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
25struct Bomb;
2627impl Drop for Bomb {
28fn drop(&mut self) {
29 abort();
30 }
31 }
3233let bomb = Bomb;
34let t = f();
35 mem::forget(bomb);
36 t
37}
3839/// A version of `alloc::alloc::Layout` that can be used in the const
40/// position.
41#[derive(Clone, Copy, Debug)]
42pub(crate) struct Layout {
43 size: usize,
44 align: usize,
45}
4647impl Layout {
48/// Creates a new `Layout` with the given size and alignment.
49#[inline]
50pub(crate) const fn from_size_align(size: usize, align: usize) -> Self {
51Self { size, align }
52 }
5354/// Creates a new `Layout` for the given sized type.
55#[inline]
56pub(crate) const fn new<T>() -> Self {
57Self::from_size_align(mem::size_of::<T>(), mem::align_of::<T>())
58 }
5960/// Convert this into the standard library's layout type.
61 ///
62 /// # Safety
63 ///
64 /// - `align` must be non-zero and a power of two.
65 /// - When rounded up to the nearest multiple of `align`, the size
66 /// must not overflow.
67#[inline]
68pub(crate) const unsafe fn into_std(self) -> StdLayout {
69 StdLayout::from_size_align_unchecked(self.size, self.align)
70 }
7172/// Get the alignment of this layout.
73#[inline]
74pub(crate) const fn align(&self) -> usize {
75self.align
76 }
7778/// Get the size of this layout.
79#[inline]
80pub(crate) const fn size(&self) -> usize {
81self.size
82 }
8384/// Returns the layout for `a` followed by `b` and the offset of `b`.
85 ///
86 /// This function was adapted from the `Layout::extend()`:
87 /// https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.extend
88#[inline]
89pub(crate) const fn extend(self, other: Layout) -> Option<(Layout, usize)> {
90let new_align = max(self.align(), other.align());
91let pad = self.padding_needed_for(other.align());
9293let offset = leap!(self.size().checked_add(pad));
94let new_size = leap!(offset.checked_add(other.size()));
9596// return None if any of the following are true:
97 // - align is 0 (implied false by is_power_of_two())
98 // - align is not a power of 2
99 // - size rounded up to align overflows
100if !new_align.is_power_of_two() || new_size > isize::MAX as usize - (new_align - 1) {
101return None;
102 }
103104let layout = Layout::from_size_align(new_size, new_align);
105Some((layout, offset))
106 }
107108/// Returns the padding after `layout` that aligns the following address to `align`.
109 ///
110 /// This function was adapted from the `Layout::padding_needed_for()`:
111 /// https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.padding_needed_for
112#[inline]
113pub(crate) const fn padding_needed_for(self, align: usize) -> usize {
114let len = self.size();
115let len_rounded_up = len.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
116 len_rounded_up.wrapping_sub(len)
117 }
118}
119120#[inline]
121pub(crate) const fn max(left: usize, right: usize) -> usize {
122if left > right {
123 left
124 } else {
125 right
126 }
127}