Skip to main content

vpp_plugin/vppinfra/
mod.rs

1//! VPP core library
2//!
3//! This module contains abstractions around VPP's core library, `vppinfra`, including
4//! vectors and utilities.
5
6pub mod cache;
7pub mod error;
8pub mod unformat;
9pub mod vec;
10
11pub use vec::{Vec, VecRef};
12
13/// Unstable API for doctest usage only
14#[doc(hidden)]
15pub fn clib_mem_init() {
16    use std::sync::OnceLock;
17
18    static INIT: OnceLock<()> = OnceLock::new();
19
20    let _ = INIT.get_or_init(|| {
21        // SAFETY: it's safe to call this with a null base pointer, since that tells VPP to
22        // choose it for us, and there are no constraints on the size paramter. There are no
23        // concurrency issues since this just sets the per-thread heap.
24        unsafe {
25            // 3 << 30 is just the value that the C vppinfra UTs use in vpp
26            crate::bindings::clib_mem_init(std::ptr::null_mut(), 3 << 30);
27        }
28    });
29}
30
31/// Hints to the compiler that the given code path is cold, i.e., unlikely to be executed.
32// until std::hint::cold_path is stabilised
33#[cold]
34pub const fn cold_path() {}
35
36/// Hints to the compiler that the given boolean expression is likely to be true
37///
38/// See also [`cold_path`] and [`unlikely`].
39// until std::hint::likely is stabilised
40#[inline(always)]
41pub const fn likely(b: bool) -> bool {
42    if b {
43        true
44    } else {
45        cold_path();
46        false
47    }
48}
49
50/// Hints to the compiler that the given boolean expression is unlikely to be true
51///
52/// See also [`likely`].
53// until std::hint::unlikely is stabilised
54#[inline(always)]
55pub const fn unlikely(b: bool) -> bool {
56    if b {
57        cold_path();
58        true
59    } else {
60        false
61    }
62}
63
64/// Compile-time assertion
65///
66/// Evaluates the given expression at compile time, causing a compilation error if it
67/// evaluates to false.
68#[macro_export]
69macro_rules! const_assert {
70    ($x:expr $(,)?) => {
71        #[allow(unknown_lints, eq_op)]
72        const _: [(); 0 - !{
73            const ASSERT: bool = $x;
74            ASSERT
75        } as usize] = [];
76    };
77}