poulpy/backend/src/lib.rs
backend/src/lib.rsRaw
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 hal;
7pub mod implementation;
8
9pub mod doc {
10 #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/docs/backend_safety_contract.md"))]
11 pub mod backend_safety {
12 pub const _PLACEHOLDER: () = ();
13 }
14}
15
16pub const GALOISGENERATOR: u64 = 5;
17pub const DEFAULTALIGN: usize = 64;
18
19fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
20 (ptr as usize).is_multiple_of(align)
21}
22
23pub fn is_aligned<T>(ptr: *const T) -> bool {
24 is_aligned_custom(ptr, DEFAULTALIGN)
25}
26
27pub fn assert_alignement<T>(ptr: *const T) {
28 assert!(
29 is_aligned(ptr),
30 "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]"
31 )
32}
33
34pub fn cast<T, V>(data: &[T]) -> &[V] {
35 let ptr: *const V = data.as_ptr() as *const V;
36 let len: usize = data.len() / size_of::<V>();
37 unsafe { std::slice::from_raw_parts(ptr, len) }
38}
39
40#[allow(clippy::mut_from_ref)]
41pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
42 let ptr: *mut V = data.as_ptr() as *mut V;
43 let len: usize = data.len() / size_of::<V>();
44 unsafe { std::slice::from_raw_parts_mut(ptr, len) }
45}
46
47/// Allocates a block of bytes with a custom alignement.
48/// Alignement must be a power of two and size a multiple of the alignement.
49/// Allocated memory is initialized to zero.
50fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
51 assert!(
52 align.is_power_of_two(),
53 "Alignment must be a power of two but is {}",
54 align
55 );
56 assert_eq!(
57 (size * size_of::<u8>()) % align,
58 0,
59 "size={} must be a multiple of align={}",
60 size,
61 align
62 );
63 unsafe {
64 let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
65 let ptr: *mut u8 = std::alloc::alloc(layout);
66 if ptr.is_null() {
67 panic!("Memory allocation failed");
68 }
69 assert!(
70 is_aligned_custom(ptr, align),
71 "Memory allocation at {:p} is not aligned to {} bytes",
72 ptr,
73 align
74 );
75 // Init allocated memory to zero
76 std::ptr::write_bytes(ptr, 0, size);
77 Vec::from_raw_parts(ptr, size, size)
78 }
79}
80
81/// Allocates a block of T aligned with [DEFAULTALIGN].
82/// Size of T * size msut be a multiple of [DEFAULTALIGN].
83pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
84 assert_eq!(
85 (size * size_of::<T>()) % (align / size_of::<T>()),
86 0,
87 "size={} must be a multiple of align={}",
88 size,
89 align
90 );
91 let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(size_of::<T>() * size, align);
92 let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
93 let len: usize = vec_u8.len() / size_of::<T>();
94 let cap: usize = vec_u8.capacity() / size_of::<T>();
95 std::mem::forget(vec_u8);
96 unsafe { Vec::from_raw_parts(ptr, len, cap) }
97}
98
99/// Allocates an aligned vector of size equal to the smallest multiple
100/// of [DEFAULTALIGN]/`size_of::<T>`() that is equal or greater to `size`.
101pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
102 alloc_aligned_custom::<T>(
103 size + (DEFAULTALIGN - (size % (DEFAULTALIGN / size_of::<T>()))) % DEFAULTALIGN,
104 DEFAULTALIGN,
105 )
106}