blob: 441ead1e0e2df395360226c6695e80769dc3c183 [file] [log] [blame]
Stjepan Glavina1479e862019-08-12 20:18:51 +02001use std::alloc::Layout;
2use std::mem;
3
4/// Calls a function and aborts if it panics.
5///
6/// This is useful in unsafe code where we can't recover from panics.
7#[inline]
8pub(crate) fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
9 struct Bomb;
10
11 impl Drop for Bomb {
12 fn drop(&mut self) {
13 std::process::abort();
14 }
15 }
16
17 let bomb = Bomb;
18 let t = f();
19 mem::forget(bomb);
20 t
21}
22
23/// Returns the layout for `a` followed by `b` and the offset of `b`.
24///
25/// This function was adapted from the currently unstable `Layout::extend()`:
26/// https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.extend
27#[inline]
28pub(crate) fn extend(a: Layout, b: Layout) -> (Layout, usize) {
29 let new_align = a.align().max(b.align());
30 let pad = padding_needed_for(a, b.align());
31
32 let offset = a.size().checked_add(pad).unwrap();
33 let new_size = offset.checked_add(b.size()).unwrap();
34
35 let layout = Layout::from_size_align(new_size, new_align).unwrap();
36 (layout, offset)
37}
38
39/// Returns the padding after `layout` that aligns the following address to `align`.
40///
41/// This function was adapted from the currently unstable `Layout::padding_needed_for()`:
42/// https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.padding_needed_for
43#[inline]
44pub(crate) fn padding_needed_for(layout: Layout, align: usize) -> usize {
45 let len = layout.size();
46 let len_rounded_up = len.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
47 len_rounded_up.wrapping_sub(len)
48}