use crate::base::RUMVec;
use crate::cpu::CPU_SIMD_64_SIZE;
use crate::strings::RUMString;
#[cfg(all(feature = "mimalloc", feature = "default"))]
use mimalloc::MiMalloc;
#[cfg(all(feature = "mimalloc", feature = "default"))]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
pub trait AsPtr {
#[inline(always)]
fn as_ptr(&self) -> *const u8 {
self as *const _ as *const u8
}
#[inline(always)]
fn as_mut_ptr(&mut self) -> *mut u8 {
self as *mut _ as *mut u8
}
}
impl AsPtr for [u8] { }
impl AsPtr for &[u8] { }
impl AsPtr for RUMVec<u8> { }
impl AsPtr for &RUMVec<u8> { }
impl AsPtr for RUMString { }
pub trait SizedType {
#[inline(always)]
fn size(&self) -> usize;
}
impl SizedType for [u8] { fn size(&self) -> usize { self.len() } }
impl SizedType for &[u8] { fn size(&self) -> usize { self.len() } }
impl SizedType for RUMVec<u8> { fn size(&self) -> usize { self.len() } }
impl SizedType for &RUMVec<u8> { fn size(&self) -> usize { self.len() } }
impl SizedType for RUMString { fn size(&self) -> usize { self.len() } }
pub trait AsSlice: AsPtr + SizedType {
#[inline(always)]
fn as_slice(&self) -> &[u8] { as_slice(self.as_ptr(), self.size()) }
#[inline(always)]
fn as_slice_mut(&mut self) -> &mut [u8] { as_slice_mut(self.as_mut_ptr(), self.size()) }
#[inline(always)]
fn contains(&self, x: &u8) -> bool {
self.as_slice().contains(x)
}
}
impl AsSlice for [u8] { }
impl AsSlice for &[u8] { }
impl AsSlice for RUMVec<u8> { }
impl AsSlice for &RUMVec<u8> { }
impl AsSlice for RUMString { }
#[inline]
pub fn as_slice<'a>(src: *const u8, size: usize) -> &'a [u8] {
unsafe { std::slice::from_raw_parts(src, size) }
}
#[inline]
pub fn as_slice_mut<'a>(src: *mut u8, size: usize) -> &'a mut [u8] {
unsafe { std::slice::from_raw_parts_mut(src, size) }
}
#[macro_export]
macro_rules! rumtk_mem_quick_array_init {
( $typ:ty, $size:expr ) => {{
const DATA_SLICE_LEN: usize = $size * size_of::<$typ>();
let arr: [$typ; $size] = unsafe { mem::transmute([0u8; DATA_SLICE_LEN]) };
arr
}}
}
#[inline]
pub fn copy_simd_slice<'a, const LANE_SIZE: usize>(src: &[u8], mut dst: &'a mut [u8]) -> &'a mut [u8] {
let (prefix, middle, postfix) = src.as_simd::<LANE_SIZE>();
let prefix_len = prefix.len();
let postfix_len = postfix.len();
dst[..prefix_len].copy_from_slice(prefix);
dst = &mut dst[prefix_len..];
for chunk in middle.into_iter() {
chunk.copy_to_slice(&mut dst[..LANE_SIZE]);
dst = &mut dst[LANE_SIZE..];
}
dst[..postfix_len].copy_from_slice(postfix);
dst
}
#[inline]
pub fn copy_from_slice<'a>(src: &[u8], dst: &'a mut [u8]) -> &'a mut [u8] {
debug_assert!(src.len() <= dst.len(), "Destination memory slice is smaller than source! This is a bug near the call site of copy_from_slice!");
copy_simd_slice::<CPU_SIMD_64_SIZE>(
src,
dst,
)
}