This commit is contained in:
Adam Ierymenko 2022-10-23 11:04:16 -07:00
parent 07eed995e7
commit 72f98dea14
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
4 changed files with 21 additions and 21 deletions

View file

@ -24,8 +24,8 @@ use zerotier_utils::error::InvalidParameterError;
use zerotier_utils::gate::IntervalGate; use zerotier_utils::gate::IntervalGate;
use zerotier_utils::hex; use zerotier_utils::hex;
use zerotier_utils::marshalable::Marshalable; use zerotier_utils::marshalable::Marshalable;
use zerotier_utils::pocket::Pocket;
use zerotier_utils::ringbuffer::RingBuffer; use zerotier_utils::ringbuffer::RingBuffer;
use zerotier_utils::thing::Thing;
/// Trait implemented by external code to handle events and provide an interface to the system or application. /// Trait implemented by external code to handle events and provide an interface to the system or application.
/// ///
@ -248,7 +248,7 @@ pub struct Node {
intervals: Mutex<BackgroundTaskIntervals>, intervals: Mutex<BackgroundTaskIntervals>,
/// Canonicalized network paths, held as Weak<> to be automatically cleaned when no longer in use. /// Canonicalized network paths, held as Weak<> to be automatically cleaned when no longer in use.
paths: RwLock<Pocket<PATH_MAP_SIZE>>, paths: RwLock<Thing<PATH_MAP_SIZE>>,
/// Peers with which we are currently communicating. /// Peers with which we are currently communicating.
peers: RwLock<HashMap<Address, Arc<Peer>>>, peers: RwLock<HashMap<Address, Arc<Peer>>>,
@ -300,7 +300,7 @@ impl Node {
instance_id: random::get_bytes_secure(), instance_id: random::get_bytes_secure(),
identity: id, identity: id,
intervals: Mutex::new(BackgroundTaskIntervals::default()), intervals: Mutex::new(BackgroundTaskIntervals::default()),
paths: RwLock::new(Pocket::new(PathMap::<HostSystemImpl>::new())), paths: RwLock::new(Thing::new(PathMap::<HostSystemImpl>::new())),
peers: RwLock::new(HashMap::new()), peers: RwLock::new(HashMap::new()),
roots: RwLock::new(RootInfo { roots: RwLock::new(RootInfo {
sets: HashMap::new(), sets: HashMap::new(),

View file

@ -10,7 +10,7 @@ use crate::vl1::endpoint::Endpoint;
use crate::vl1::node::*; use crate::vl1::node::*;
use zerotier_crypto::random; use zerotier_crypto::random;
use zerotier_utils::pocket::Pocket; use zerotier_utils::thing::Thing;
use zerotier_utils::NEVER_HAPPENED_TICKS; use zerotier_utils::NEVER_HAPPENED_TICKS;
pub(crate) const SERVICE_INTERVAL_MS: i64 = protocol::PATH_KEEPALIVE_INTERVAL; pub(crate) const SERVICE_INTERVAL_MS: i64 = protocol::PATH_KEEPALIVE_INTERVAL;
@ -27,8 +27,8 @@ pub(crate) enum PathServiceResult {
/// for them and uniform application of things like keepalives. /// for them and uniform application of things like keepalives.
pub struct Path { pub struct Path {
pub endpoint: Endpoint, pub endpoint: Endpoint,
local_socket: Pocket<64>, local_socket: Thing<64>,
local_interface: Pocket<64>, local_interface: Thing<64>,
last_send_time_ticks: AtomicI64, last_send_time_ticks: AtomicI64,
last_receive_time_ticks: AtomicI64, last_receive_time_ticks: AtomicI64,
create_time_ticks: i64, create_time_ticks: i64,
@ -44,8 +44,8 @@ impl Path {
) -> Self { ) -> Self {
Self { Self {
endpoint, endpoint,
local_socket: Pocket::new(local_socket), // enlarge Pocket<> if this panics local_socket: Thing::new(local_socket), // enlarge Thing<> if this panics
local_interface: Pocket::new(local_interface), // enlarge Pocket<> if this panics local_interface: Thing::new(local_interface), // enlarge Thing<> if this panics
last_send_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS), last_send_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS),
last_receive_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS), last_receive_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS),
create_time_ticks: time_ticks, create_time_ticks: time_ticks,

View file

@ -14,10 +14,10 @@ pub mod io;
pub mod json; pub mod json;
pub mod marshalable; pub mod marshalable;
pub mod memory; pub mod memory;
pub mod pocket;
pub mod pool; pub mod pool;
pub mod ringbuffer; pub mod ringbuffer;
pub mod ringbuffermap; pub mod ringbuffermap;
pub mod thing;
pub mod varint; pub mod varint;
#[cfg(feature = "tokio")] #[cfg(feature = "tokio")]

View file

@ -4,22 +4,22 @@ use std::any::TypeId;
use std::mem::{forget, size_of, MaybeUninit}; use std::mem::{forget, size_of, MaybeUninit};
use std::ptr::{drop_in_place, read, write}; use std::ptr::{drop_in_place, read, write};
/// A statically sized container that acts a bit like Box<dyn Any> but with less overhead. /// A statically sized container that acts like Box<dyn Any> but without heap overhead.
/// ///
/// This is used in a few places to avoid cascades of templates by allowing templated /// This is used in a couple places to avoid what would otherwise be an explosion of cascading
/// objects to be held generically and accessed only within templated functions. There's a /// template parameters to support externally defined small objects. It does so with virtually
/// bit of unsafe here but externally it's safe and panics if misused. /// no overhead, unlike Box<>, and could be no_std compatible.
/// ///
/// This will panic if the capacity is too small. If that occurs, it must be enlarged. It will /// This will panic if the capacity is too small. If that occurs, it must be enlarged. It will
/// also panic if any of the accessors (other than the try_ versions) are used to try to get /// also panic if any of the accessors (other than the try_ versions) are used to try to get
/// a type other than the one it was constructed with. /// a type other than the one it was constructed with.
pub struct Pocket<const CAPACITY: usize> { pub struct Thing<const CAPACITY: usize> {
storage: [u8; CAPACITY], storage: [u8; CAPACITY],
dropper: fn(*mut u8), dropper: fn(*mut u8),
data_type: TypeId, data_type: TypeId,
} }
impl<const CAPACITY: usize> Pocket<CAPACITY> { impl<const CAPACITY: usize> Thing<CAPACITY> {
#[inline(always)] #[inline(always)]
pub fn new<T: Sized + 'static>(x: T) -> Self { pub fn new<T: Sized + 'static>(x: T) -> Self {
assert!(size_of::<T>() <= CAPACITY); assert!(size_of::<T>() <= CAPACITY);
@ -73,7 +73,7 @@ impl<const CAPACITY: usize> Pocket<CAPACITY> {
} }
} }
impl<T: Sized + 'static, const CAPACITY: usize> AsRef<T> for Pocket<CAPACITY> { impl<T: Sized + 'static, const CAPACITY: usize> AsRef<T> for Thing<CAPACITY> {
#[inline(always)] #[inline(always)]
fn as_ref(&self) -> &T { fn as_ref(&self) -> &T {
assert_eq!(TypeId::of::<T>(), self.data_type); assert_eq!(TypeId::of::<T>(), self.data_type);
@ -81,7 +81,7 @@ impl<T: Sized + 'static, const CAPACITY: usize> AsRef<T> for Pocket<CAPACITY> {
} }
} }
impl<T: Sized + 'static, const CAPACITY: usize> AsMut<T> for Pocket<CAPACITY> { impl<T: Sized + 'static, const CAPACITY: usize> AsMut<T> for Thing<CAPACITY> {
#[inline(always)] #[inline(always)]
fn as_mut(&mut self) -> &mut T { fn as_mut(&mut self) -> &mut T {
assert_eq!(TypeId::of::<T>(), self.data_type); assert_eq!(TypeId::of::<T>(), self.data_type);
@ -89,7 +89,7 @@ impl<T: Sized + 'static, const CAPACITY: usize> AsMut<T> for Pocket<CAPACITY> {
} }
} }
impl<const CAPACITY: usize> Drop for Pocket<CAPACITY> { impl<const CAPACITY: usize> Drop for Thing<CAPACITY> {
#[inline(always)] #[inline(always)]
fn drop(&mut self) { fn drop(&mut self) {
(self.dropper)((self as *mut Self).cast()); (self.dropper)((self as *mut Self).cast());
@ -105,9 +105,9 @@ mod tests {
fn typing_and_life_cycle() { fn typing_and_life_cycle() {
let test_obj = Rc::new(1i32); let test_obj = Rc::new(1i32);
assert_eq!(Rc::strong_count(&test_obj), 1); assert_eq!(Rc::strong_count(&test_obj), 1);
let a = Pocket::<32>::new(test_obj.clone()); let a = Thing::<32>::new(test_obj.clone());
let b = Pocket::<32>::new(test_obj.clone()); let b = Thing::<32>::new(test_obj.clone());
let c = Pocket::<32>::new(test_obj.clone()); let c = Thing::<32>::new(test_obj.clone());
assert!(a.get::<Rc<i32>>().eq(b.get())); assert!(a.get::<Rc<i32>>().eq(b.get()));
assert!(a.try_get::<Rc<i32>>().is_some()); assert!(a.try_get::<Rc<i32>>().is_some());
assert!(a.try_get::<Rc<usize>>().is_none()); assert!(a.try_get::<Rc<usize>>().is_none());