added debug checks for alignement

This commit is contained in:
Jean-Philippe Bossuat
2025-02-25 15:04:56 +01:00
parent 871b85e471
commit 483a142ab0
8 changed files with 140 additions and 80 deletions

View File

@@ -35,14 +35,18 @@ pub use vmp::*;
pub const GALOISGENERATOR: u64 = 5;
pub const DEFAULTALIGN: usize = 64;
fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
pub fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
(ptr as usize) % align == 0
}
fn is_aligned<T>(ptr: *const T) -> bool {
pub fn is_aligned<T>(ptr: *const T) -> bool {
is_aligned_custom(ptr, DEFAULTALIGN)
}
pub fn assert_alignement<T>(ptr: *const T) {
assert!(is_aligned(ptr), "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]")
}
pub fn cast<T, V>(data: &[T]) -> &[V] {
let ptr: *const V = data.as_ptr() as *const V;
let len: usize = data.len() / std::mem::size_of::<V>();