mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-07-27 04:32:51 +02:00
Build fixes in Rust.
This commit is contained in:
parent
8c45fbe64c
commit
5d75ca0e30
5 changed files with 31 additions and 35 deletions
|
@ -40,21 +40,13 @@ public:
|
|||
{}
|
||||
|
||||
ZT_INLINE ~SharedPtr()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
{ _release(); }
|
||||
|
||||
ZT_INLINE SharedPtr &operator=(const SharedPtr &sp)
|
||||
{
|
||||
if (likely(m_ptr != sp.m_ptr)) {
|
||||
T *const p = sp._getAndInc();
|
||||
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;
|
||||
}
|
||||
_release();
|
||||
m_ptr = p;
|
||||
}
|
||||
return *this;
|
||||
|
@ -62,12 +54,8 @@ public:
|
|||
|
||||
ZT_INLINE void set(T *ptr) noexcept
|
||||
{
|
||||
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;
|
||||
}
|
||||
const_cast<std::atomic< int > *>(&(ptr->__refCount))->fetch_add(1, std::memory_order_relaxed);
|
||||
m_ptr = ptr;
|
||||
_release();
|
||||
const_cast<std::atomic< int > *>(&((m_ptr = ptr)->__refCount))->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,10 +80,7 @@ public:
|
|||
*/
|
||||
ZT_INLINE void move(SharedPtr &from)
|
||||
{
|
||||
if (m_ptr != nullptr) {
|
||||
if (const_cast<std::atomic< int > *>(&(m_ptr->__refCount))->fetch_sub(1, std::memory_order_relaxed) <= 1)
|
||||
delete m_ptr;
|
||||
}
|
||||
_release();
|
||||
m_ptr = from.m_ptr;
|
||||
from.m_ptr = nullptr;
|
||||
}
|
||||
|
@ -120,12 +105,9 @@ public:
|
|||
*/
|
||||
ZT_INLINE void zero()
|
||||
{
|
||||
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;
|
||||
_release();
|
||||
m_ptr = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set pointer to NULL and delete object if reference count is only 1
|
||||
|
@ -174,16 +156,16 @@ public:
|
|||
{ return (m_ptr != sp.m_ptr); }
|
||||
|
||||
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
|
||||
{ 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
|
||||
{ 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
|
||||
{ return (m_ptr <= sp.m_ptr); }
|
||||
{ return (reinterpret_cast<const uint8_t *>(m_ptr) <= reinterpret_cast<const uint8_t *>(sp.m_ptr)); }
|
||||
|
||||
private:
|
||||
ZT_INLINE T *_getAndInc() const noexcept
|
||||
|
@ -193,6 +175,12 @@ private:
|
|||
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;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#[allow(unused_assignments)]
|
||||
#[allow(unused_mut)]
|
||||
fn main() {
|
||||
let d = env!("CARGO_MANIFEST_DIR");
|
||||
println!("cargo:rustc-link-search=native={}/../build/core", d);
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
|
||||
use std::ffi::CString;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::mem::zeroed;
|
||||
use std::os::raw::{c_char, c_uint, c_void};
|
||||
use std::ptr::{copy_nonoverlapping, null, null_mut};
|
||||
|
||||
|
@ -23,6 +22,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use crate::*;
|
||||
use crate::capi as ztcore;
|
||||
use std::mem::zeroed;
|
||||
|
||||
/// 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;
|
||||
|
@ -47,12 +47,18 @@ pub struct CertificateSerialNo(pub [u8; 48]);
|
|||
impl CertificateSerialNo {
|
||||
#[inline(always)]
|
||||
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 {
|
||||
#[inline(always)]
|
||||
fn from(a: &[u8; 48]) -> CertificateSerialNo { CertificateSerialNo(*a) }
|
||||
impl<A: AsRef<[u8]>> From<A> for CertificateSerialNo {
|
||||
fn from(a: A) -> CertificateSerialNo {
|
||||
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 {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#[allow(unused_assignments)]
|
||||
#[allow(unused_mut)]
|
||||
fn main() {
|
||||
let d = env!("CARGO_MANIFEST_DIR");
|
||||
println!("cargo:rustc-link-search=native={}/../build/core", d);
|
||||
|
|
|
@ -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| {
|
||||
eprintln!("FATAL: error writing '{}': {}", path, e.to_string());
|
||||
e.raw_os_error().unwrap_or(1)
|
||||
}, || {
|
||||
}, |_| {
|
||||
0
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue