Build fixes in Rust.

This commit is contained in:
Adam Ierymenko 2021-03-09 23:36:35 -05:00
parent 8c45fbe64c
commit 5d75ca0e30
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
5 changed files with 31 additions and 35 deletions

View file

@ -40,21 +40,13 @@ public:
{} {}
ZT_INLINE ~SharedPtr() ZT_INLINE ~SharedPtr()
{ { _release(); }
if (likely(m_ptr != nullptr)) {
if (unlikely(const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1))
delete m_ptr;
}
}
ZT_INLINE SharedPtr &operator=(const SharedPtr &sp) ZT_INLINE SharedPtr &operator=(const SharedPtr &sp)
{ {
if (likely(m_ptr != sp.m_ptr)) { if (likely(m_ptr != sp.m_ptr)) {
T *const p = sp._getAndInc(); T *const p = sp._getAndInc();
if (likely(m_ptr != nullptr)) { _release();
if (unlikely(const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1))
delete m_ptr;
}
m_ptr = p; m_ptr = p;
} }
return *this; return *this;
@ -62,12 +54,8 @@ public:
ZT_INLINE void set(T *ptr) noexcept ZT_INLINE void set(T *ptr) noexcept
{ {
if (likely(m_ptr != nullptr)) { _release();
if (unlikely(const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)) const_cast<std::atomic< int > *>(&((m_ptr = ptr)->__refCount))->fetch_add(1, std::memory_order_relaxed);
delete m_ptr;
}
const_cast<std::atomic< int > *>(&(ptr->__refCount))->fetch_add(1, std::memory_order_relaxed);
m_ptr = ptr;
} }
/** /**
@ -92,10 +80,7 @@ public:
*/ */
ZT_INLINE void move(SharedPtr &from) ZT_INLINE void move(SharedPtr &from)
{ {
if (m_ptr != nullptr) { _release();
if (const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)
delete m_ptr;
}
m_ptr = from.m_ptr; m_ptr = from.m_ptr;
from.m_ptr = nullptr; from.m_ptr = nullptr;
} }
@ -120,12 +105,9 @@ public:
*/ */
ZT_INLINE void zero() ZT_INLINE void zero()
{ {
if (likely(m_ptr != nullptr)) { _release();
if (unlikely(const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1))
delete m_ptr;
m_ptr = nullptr; m_ptr = nullptr;
} }
}
/** /**
* Set pointer to NULL and delete object if reference count is only 1 * Set pointer to NULL and delete object if reference count is only 1
@ -174,16 +156,16 @@ public:
{ return (m_ptr != sp.m_ptr); } { return (m_ptr != sp.m_ptr); }
ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept
{ return (m_ptr > sp.m_ptr); } { return (reinterpret_cast<const uint8_t *>(m_ptr) > reinterpret_cast<const uint8_t *>(sp.m_ptr)); }
ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept
{ return (m_ptr < sp.m_ptr); } { return (reinterpret_cast<const uint8_t *>(m_ptr) < reinterpret_cast<const uint8_t *>(sp.m_ptr)); }
ZT_INLINE bool operator>=(const SharedPtr &sp) const noexcept ZT_INLINE bool operator>=(const SharedPtr &sp) const noexcept
{ return (m_ptr >= sp.m_ptr); } { return (reinterpret_cast<const uint8_t *>(m_ptr) >= reinterpret_cast<const uint8_t *>(sp.m_ptr)); }
ZT_INLINE bool operator<=(const SharedPtr &sp) const noexcept ZT_INLINE bool operator<=(const SharedPtr &sp) const noexcept
{ return (m_ptr <= sp.m_ptr); } { return (reinterpret_cast<const uint8_t *>(m_ptr) <= reinterpret_cast<const uint8_t *>(sp.m_ptr)); }
private: private:
ZT_INLINE T *_getAndInc() const noexcept ZT_INLINE T *_getAndInc() const noexcept
@ -193,6 +175,12 @@ private:
return m_ptr; return m_ptr;
} }
ZT_INLINE void _release() const noexcept
{
if (unlikely((m_ptr != nullptr)&&(const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)))
delete m_ptr;
}
T *m_ptr; T *m_ptr;
}; };

View file

@ -1,4 +1,5 @@
#[allow(unused_assignments)] #[allow(unused_assignments)]
#[allow(unused_mut)]
fn main() { fn main() {
let d = env!("CARGO_MANIFEST_DIR"); let d = env!("CARGO_MANIFEST_DIR");
println!("cargo:rustc-link-search=native={}/../build/core", d); println!("cargo:rustc-link-search=native={}/../build/core", d);

View file

@ -13,7 +13,6 @@
use std::ffi::CString; use std::ffi::CString;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::mem::zeroed;
use std::os::raw::{c_char, c_uint, c_void}; use std::os::raw::{c_char, c_uint, c_void};
use std::ptr::{copy_nonoverlapping, null, null_mut}; use std::ptr::{copy_nonoverlapping, null, null_mut};
@ -23,6 +22,7 @@ use serde::{Deserialize, Serialize};
use crate::*; use crate::*;
use crate::capi as ztcore; use crate::capi as ztcore;
use std::mem::zeroed;
/// Maximum length of a string in a certificate (mostly for the certificate name fields). /// Maximum length of a string in a certificate (mostly for the certificate name fields).
pub const CERTIFICATE_MAX_STRING_LENGTH: isize = ztcore::ZT_CERTIFICATE_MAX_STRING_LENGTH as isize; pub const CERTIFICATE_MAX_STRING_LENGTH: isize = ztcore::ZT_CERTIFICATE_MAX_STRING_LENGTH as isize;
@ -47,12 +47,18 @@ pub struct CertificateSerialNo(pub [u8; 48]);
impl CertificateSerialNo { impl CertificateSerialNo {
#[inline(always)] #[inline(always)]
pub fn new() -> CertificateSerialNo { CertificateSerialNo([0; 48]) } pub fn new() -> CertificateSerialNo { CertificateSerialNo([0; 48]) }
pub fn new_from_string(s: &str) -> Result<CertificateSerialNo, ResultCode> { hex::decode(s).map_or_else(|_| { Err(ResultCode::ErrorBadParameter) }, |b| { Ok(CertificateSerialNo::from(b.unwrap().as_slice())) }) } pub fn new_from_string(s: &str) -> Result<CertificateSerialNo, ResultCode> { hex::decode(s).map_or_else(|_| { Err(ResultCode::ErrorBadParameter) }, |b| { Ok(CertificateSerialNo::from(b)) }) }
} }
impl From<&[u8; 48]> for CertificateSerialNo { impl<A: AsRef<[u8]>> From<A> for CertificateSerialNo {
#[inline(always)] fn from(a: A) -> CertificateSerialNo {
fn from(a: &[u8; 48]) -> CertificateSerialNo { CertificateSerialNo(*a) } let mut sn = CertificateSerialNo::new();
let aa = a.as_ref();
for i in 0..aa.len() {
sn.0[i] = aa[i];
}
sn
}
} }
impl Hash for CertificateSerialNo { impl Hash for CertificateSerialNo {

View file

@ -1,4 +1,5 @@
#[allow(unused_assignments)] #[allow(unused_assignments)]
#[allow(unused_mut)]
fn main() { fn main() {
let d = env!("CARGO_MANIFEST_DIR"); let d = env!("CARGO_MANIFEST_DIR");
println!("cargo:rustc-link-search=native={}/../build/core", d); println!("cargo:rustc-link-search=native={}/../build/core", d);

View file

@ -38,7 +38,7 @@ fn newsid<'a>(store: &Store, cli_args: Option<&ArgMatches<'a>>, auth_token: &Opt
std::fs::write(path, sid.as_bytes()).map_or_else(|e| { std::fs::write(path, sid.as_bytes()).map_or_else(|e| {
eprintln!("FATAL: error writing '{}': {}", path, e.to_string()); eprintln!("FATAL: error writing '{}': {}", path, e.to_string());
e.raw_os_error().unwrap_or(1) e.raw_os_error().unwrap_or(1)
}, || { }, |_| {
0 0
}) })
} }