1#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code, improper_ctypes)]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![feature(trait_alias)]
5
6pub mod api;
7pub mod delegates;
8pub mod layouts;
9pub mod oep;
10pub mod source;
11pub mod tests;
12
13pub mod doc {
14 #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/docs/backend_safety_contract.md"))]
15 pub mod backend_safety {
16 pub const _PLACEHOLDER: () = ();
17 }
18}
19
20pub const GALOISGENERATOR: u64 = 5;
21pub const DEFAULTALIGN: usize = 64;
22
23fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
24 (ptr as usize).is_multiple_of(align)
25}
26
27pub fn is_aligned<T>(ptr: *const T) -> bool {
28 is_aligned_custom(ptr, DEFAULTALIGN)
29}
30
31pub fn assert_alignement<T>(ptr: *const T) {
32 assert!(
33 is_aligned(ptr),
34 "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]"
35 )
36}
37
38pub fn cast<T, V>(data: &[T]) -> &[V] {
39 let ptr: *const V = data.as_ptr() as *const V;
40 let len: usize = data.len() / size_of::<V>();
41 unsafe { std::slice::from_raw_parts(ptr, len) }
42}
43
44#[allow(clippy::mut_from_ref)]
45pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
46 let ptr: *mut V = data.as_ptr() as *mut V;
47 let len: usize = data.len() / size_of::<V>();
48 unsafe { std::slice::from_raw_parts_mut(ptr, len) }
49}
50
51fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
55 assert!(
56 align.is_power_of_two(),
57 "Alignment must be a power of two but is {}",
58 align
59 );
60 assert_eq!(
61 (size * size_of::<u8>()) % align,
62 0,
63 "size={} must be a multiple of align={}",
64 size,
65 align
66 );
67 unsafe {
68 let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
69 let ptr: *mut u8 = std::alloc::alloc(layout);
70 if ptr.is_null() {
71 panic!("Memory allocation failed");
72 }
73 assert!(
74 is_aligned_custom(ptr, align),
75 "Memory allocation at {:p} is not aligned to {} bytes",
76 ptr,
77 align
78 );
79 std::ptr::write_bytes(ptr, 0, size);
81 Vec::from_raw_parts(ptr, size, size)
82 }
83}
84
85pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
88 assert_eq!(
89 (size * size_of::<T>()) % (align / size_of::<T>()),
90 0,
91 "size={} must be a multiple of align={}",
92 size,
93 align
94 );
95 let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(size_of::<T>() * size, align);
96 let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
97 let len: usize = vec_u8.len() / size_of::<T>();
98 let cap: usize = vec_u8.capacity() / size_of::<T>();
99 std::mem::forget(vec_u8);
100 unsafe { Vec::from_raw_parts(ptr, len, cap) }
101}
102
103pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
106 alloc_aligned_custom::<T>(
107 size + (DEFAULTALIGN - (size % (DEFAULTALIGN / size_of::<T>()))) % DEFAULTALIGN,
108 DEFAULTALIGN,
109 )
110}