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