Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup unsafe code #45

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ jobs:
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: "valgrind --leak-check=full --error-exitcode=1"
run: cargo test --release --features xxh32,const_xxh32,xxh64,const_xxh64,xxh3,const_xxh3

- name: Valgrind Test(AVX2)
env:
RUSTFLAGS: "-Ctarget-feature=+avx2"
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: "valgrind --leak-check=full --error-exitcode=1"
run: cargo test --release --features xxh32,const_xxh32,xxh64,const_xxh64,xxh3,const_xxh3

#- name: Miri Test(No SSE2)
# env:
# RUSTFLAGS: "-Ctarget-feature=-sse2"
# MIRIFLAGS: "-Zmiri-tag-raw-pointers"
# run: |
# cargo +nightly miri test --features xxh32,const_xxh32,xxh64,const_xxh64,xxh3,const_xxh3
# cargo +nightly miri test --release --features xxh32,const_xxh32,xxh64,const_xxh64,xxh3,const_xxh3
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
#[cfg(feature = "std")]
extern crate std;

#[cfg(any(feature = "xxh32", feature = "xxh3", feature = "xxh64"))]
mod utils;

#[cfg(any(feature = "xxh32", feature = "const_xxh32", feature = "xxh3", feature = "const_xxh3"))]
mod xxh32_common;
#[cfg(feature = "xxh32")]
Expand Down
50 changes: 50 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Utilities of the crate
use core::{ptr, mem};

#[inline(always)]
pub const fn get_aligned_chunk_ref<T: Copy>(input: &[u8], offset: usize) -> &T {
debug_assert!(mem::size_of::<T>() > 0); //Size MUST be positive
debug_assert!(mem::size_of::<T>() <= input.len().saturating_sub(offset)); //Must fit

unsafe {
&*(input.as_ptr().add(offset) as *const T)
}
}

#[inline(always)]
pub const fn get_aligned_chunk<T: Copy>(input: &[u8], offset: usize) -> T {
*get_aligned_chunk_ref(input, offset)
}

#[inline(always)]
pub fn get_unaligned_chunk<T: Copy>(input: &[u8], offset: usize) -> T {
debug_assert!(mem::size_of::<T>() > 0); //Size MUST be positive
debug_assert!(mem::size_of::<T>() <= input.len().saturating_sub(offset)); //Must fit

unsafe {
ptr::read_unaligned(input.as_ptr().add(offset) as *const T)
}
}

#[derive(Debug)]
pub struct Buffer<T> {
pub ptr: T,
pub len: usize,
pub offset: usize,
}

impl Buffer<*mut u8> {
#[inline(always)]
pub fn copy_from_slice(&self, src: &[u8]) {
self.copy_from_slice_by_size(src, src.len())
}

#[inline(always)]
pub fn copy_from_slice_by_size(&self, src: &[u8], len: usize) {
debug_assert!(self.len.saturating_sub(self.offset) >= len);

unsafe {
ptr::copy_nonoverlapping(src.as_ptr(), self.ptr.add(self.offset), len);
}
}
}
Loading