diff --git a/network-hypervisor/src/util/buffer.rs b/network-hypervisor/src/util/buffer.rs index 50fdd33c0..b51b4a8ed 100644 --- a/network-hypervisor/src/util/buffer.rs +++ b/network-hypervisor/src/util/buffer.rs @@ -3,6 +3,7 @@ use std::io::{Read, Write}; use std::mem::{size_of, MaybeUninit}; +use zerotier_utils::memory; use zerotier_utils::pool::PoolFactory; use zerotier_utils::varint; @@ -37,30 +38,6 @@ fn overflow_err() -> std::io::Error { impl Buffer { pub const CAPACITY: usize = L; - #[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64"))] - #[inline(always)] - unsafe fn read_obj_internal(&self, i: usize) -> T { - *self.1.as_ptr().add(i).cast() - } - - #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))] - #[inline(always)] - unsafe fn read_obj_internal(&self, i: usize) -> T { - std::mem::transmute_copy(&*self.1.as_ptr().add(i).cast::()) - } - - #[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64"))] - #[inline(always)] - unsafe fn write_obj_internal(&mut self, i: usize, o: T) { - *self.1.as_mut_ptr().add(i).cast::() = o; - } - - #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))] - #[inline(always)] - unsafe fn write_obj_internal(&mut self, i: usize, o: T) { - std::ptr::copy_nonoverlapping((&o as *const T).cast::(), self.1.as_mut_ptr().add(i), size_of::()) - } - /// Create an empty zeroed buffer. #[inline(always)] pub fn new() -> Self { @@ -302,7 +279,7 @@ impl Buffer { let end = ptr + 2; if end <= L { self.0 = end; - unsafe { self.write_obj_internal(ptr, i.to_be()) }; + memory::store_raw(i.to_be(), &mut self.1[ptr..]); Ok(()) } else { Err(overflow_err()) @@ -315,7 +292,7 @@ impl Buffer { let end = ptr + 4; if end <= L { self.0 = end; - unsafe { self.write_obj_internal(ptr, i.to_be()) }; + memory::store_raw(i.to_be(), &mut self.1[ptr..]); Ok(()) } else { Err(overflow_err()) @@ -328,7 +305,7 @@ impl Buffer { let end = ptr + 8; if end <= L { self.0 = end; - unsafe { self.write_obj_internal(ptr, i.to_be()) }; + memory::store_raw(i.to_be(), &mut self.1[ptr..]); Ok(()) } else { Err(overflow_err()) @@ -341,7 +318,7 @@ impl Buffer { let end = ptr + 8; if end <= L { self.0 = end; - unsafe { self.write_obj_internal(ptr, i.to_le()) }; + memory::store_raw(i.to_be(), &mut self.1[ptr..]); Ok(()) } else { Err(overflow_err()) @@ -398,7 +375,7 @@ impl Buffer { let end = ptr + 2; debug_assert!(end <= L); if end <= self.0 { - Ok(u16::from_be(unsafe { self.read_obj_internal(ptr) })) + Ok(u16::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } @@ -409,7 +386,7 @@ impl Buffer { let end = ptr + 4; debug_assert!(end <= L); if end <= self.0 { - Ok(u32::from_be(unsafe { self.read_obj_internal(ptr) })) + Ok(u32::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } @@ -420,7 +397,7 @@ impl Buffer { let end = ptr + 8; debug_assert!(end <= L); if end <= self.0 { - Ok(u64::from_be(unsafe { self.read_obj_internal(ptr) })) + Ok(u64::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } @@ -499,7 +476,7 @@ impl Buffer { debug_assert!(end <= L); if end <= self.0 { *cursor = end; - Ok(u16::from_be(unsafe { self.read_obj_internal(ptr) })) + Ok(u16::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } @@ -512,7 +489,7 @@ impl Buffer { debug_assert!(end <= L); if end <= self.0 { *cursor = end; - Ok(u32::from_be(unsafe { self.read_obj_internal(ptr) })) + Ok(u32::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } @@ -525,20 +502,7 @@ impl Buffer { debug_assert!(end <= L); if end <= self.0 { *cursor = end; - Ok(u64::from_be(unsafe { self.read_obj_internal(ptr) })) - } else { - Err(overflow_err()) - } - } - - #[inline(always)] - pub fn read_u64_le(&self, cursor: &mut usize) -> std::io::Result { - let ptr = *cursor; - let end = ptr + 8; - debug_assert!(end <= L); - if end <= self.0 { - *cursor = end; - Ok(u64::from_le(unsafe { self.read_obj_internal(ptr) })) + Ok(u64::from_be(memory::load_raw(&self.1[ptr..]))) } else { Err(overflow_err()) } diff --git a/network-hypervisor/src/vl1/dictionary.rs b/network-hypervisor/src/util/dictionary.rs similarity index 99% rename from network-hypervisor/src/vl1/dictionary.rs rename to network-hypervisor/src/util/dictionary.rs index 45df8fd9b..c3a3cac0a 100644 --- a/network-hypervisor/src/vl1/dictionary.rs +++ b/network-hypervisor/src/util/dictionary.rs @@ -226,8 +226,8 @@ mod tests { type TypeMap = HashMap; + use super::{Dictionary, BOOL_TRUTH}; use crate::util::testutil::randstring; - use crate::vl1::dictionary::{Dictionary, BOOL_TRUTH}; use std::collections::HashMap; fn make_dictionary() -> (Dictionary, TypeMap) { diff --git a/network-hypervisor/src/util/mod.rs b/network-hypervisor/src/util/mod.rs index d40b97ea3..385ae3cb0 100644 --- a/network-hypervisor/src/util/mod.rs +++ b/network-hypervisor/src/util/mod.rs @@ -1,6 +1,7 @@ // (c) 2020-2022 ZeroTier, Inc. -- currently propritery pending actual release and licensing. See LICENSE.md. pub mod buffer; +pub mod dictionary; pub(crate) mod gate; pub mod marshalable; diff --git a/network-hypervisor/src/vl1/mod.rs b/network-hypervisor/src/vl1/mod.rs index f306c2f17..cd3b90d76 100644 --- a/network-hypervisor/src/vl1/mod.rs +++ b/network-hypervisor/src/vl1/mod.rs @@ -1,7 +1,6 @@ // (c) 2020-2022 ZeroTier, Inc. -- currently propritery pending actual release and licensing. See LICENSE.md. mod address; -mod dictionary; mod endpoint; mod fragmentedpacket; mod identity; @@ -19,7 +18,6 @@ pub(crate) mod protocol; pub mod inetaddress; pub use address::Address; -pub use dictionary::Dictionary; pub use endpoint::Endpoint; pub use identity::*; pub use inetaddress::InetAddress;