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) % align == 0
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
40pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
41 let ptr: *mut V = data.as_ptr() as *mut V;
42 let len: usize = data.len() / size_of::<V>();
43 unsafe { std::slice::from_raw_parts_mut(ptr, len) }
44}
45
46/// Allocates a block of bytes with a custom alignement.
47/// Alignement must be a power of two and size a multiple of the alignement.
48/// Allocated memory is initialized to zero.
49fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
50 assert!(
51 align.is_power_of_two(),
52 "Alignment must be a power of two but is {}",
53 align
54 );
55 assert_eq!(
56 (size * size_of::<u8>()) % align,
57 0,
58 "size={} must be a multiple of align={}",
59 size,
60 align
61 );
62 unsafe {
63 let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
64 let ptr: *mut u8 = std::alloc::alloc(layout);
65 if ptr.is_null() {
66 panic!("Memory allocation failed");
67 }
68 assert!(
69 is_aligned_custom(ptr, align),
70 "Memory allocation at {:p} is not aligned to {} bytes",
71 ptr,
72 align
73 );
74 // Init allocated memory to zero
75 std::ptr::write_bytes(ptr, 0, size);
76 Vec::from_raw_parts(ptr, size, size)
77 }
78}
79
80/// Allocates a block of T aligned with [DEFAULTALIGN].
81/// Size of T * size msut be a multiple of [DEFAULTALIGN].
82pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
83 assert_eq!(
84 (size * size_of::<T>()) % (align / size_of::<T>()),
85 0,
86 "size={} must be a multiple of align={}",
87 size,
88 align
89 );
90 let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(size_of::<T>() * size, align);
91 let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
92 let len: usize = vec_u8.len() / size_of::<T>();
93 let cap: usize = vec_u8.capacity() / size_of::<T>();
94 std::mem::forget(vec_u8);
95 unsafe { Vec::from_raw_parts(ptr, len, cap) }
96}
97
98/// Allocates an aligned vector of size equal to the smallest multiple
99/// of [DEFAULTALIGN]/`size_of::<T>`() that is equal or greater to `size`.
100pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
101 alloc_aligned_custom::<T>(
102 size + (DEFAULTALIGN - (size % (DEFAULTALIGN / size_of::<T>()))) % DEFAULTALIGN,
103 DEFAULTALIGN,
104 )
105}