diff --git a/build.rs b/build.rs index 1373281224..14061de10c 100644 --- a/build.rs +++ b/build.rs @@ -9,6 +9,9 @@ use std::{ }; fn main() -> Result<(), Box> { + println!("cargo::rustc-check-cfg=cfg(arm_llsc)"); + println!("cargo::rustc-check-cfg=cfg(has_atomic_load_store)"); + let target = env::var("TARGET")?; // Manually list targets that have atomic load/store, but no CAS. diff --git a/src/pool/arc.rs b/src/pool/arc.rs index d6cc29e3f9..572da2ff70 100644 --- a/src/pool/arc.rs +++ b/src/pool/arc.rs @@ -13,7 +13,7 @@ //! // (some `no_std` runtimes have safe APIs to create `&'static mut` references) //! let block: &'static mut ArcBlock = unsafe { //! static mut BLOCK: ArcBlock = ArcBlock::new(); -//! &mut BLOCK +//! addr_of_mut!(BLOCK).as_mut().unwrap() //! }; //! //! MyArcPool.manage(block); @@ -54,7 +54,7 @@ //! let blocks: &'static mut [ArcBlock] = { //! const BLOCK: ArcBlock = ArcBlock::new(); // <= //! static mut BLOCKS: [ArcBlock; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY]; -//! unsafe { &mut BLOCKS } +//! unsafe { addr_of_mut!(BLOCK).as_mut().unwrap()S } //! }; //! //! for block in blocks { @@ -162,6 +162,7 @@ pub struct ArcPoolImpl { impl ArcPoolImpl { /// `arc_pool!` implementation detail #[doc(hidden)] + #[allow(clippy::new_without_default)] pub const fn new() -> Self { Self { stack: Stack::new(), @@ -387,9 +388,16 @@ impl ArcBlock { } } +impl Default for ArcBlock { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use super::*; + use std::ptr::addr_of_mut; #[test] fn cannot_alloc_if_empty() { @@ -404,7 +412,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -417,7 +425,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -434,7 +442,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -449,7 +457,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -470,7 +478,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -501,7 +509,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); @@ -523,7 +531,7 @@ mod tests { let block = unsafe { static mut BLOCK: ArcBlock = ArcBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyArcPool.manage(block); diff --git a/src/pool/boxed.rs b/src/pool/boxed.rs index bf880e92f4..807b6748f3 100644 --- a/src/pool/boxed.rs +++ b/src/pool/boxed.rs @@ -13,7 +13,7 @@ //! // (some `no_std` runtimes have safe APIs to create `&'static mut` references) //! let block: &'static mut BoxBlock = unsafe { //! static mut BLOCK: BoxBlock = BoxBlock::new(); -//! &mut BLOCK +//! addr_of_mut!(BLOCK).as_mut().unwrap() //! }; //! //! // give block of memory to the pool @@ -33,7 +33,7 @@ //! // give another memory block to the pool //! MyBoxPool.manage(unsafe { //! static mut BLOCK: BoxBlock = BoxBlock::new(); -//! &mut BLOCK +//! addr_of_mut!(BLOCK).as_mut().unwrap() //! }); //! //! // cloning also consumes a memory block from the pool @@ -70,7 +70,7 @@ //! #[allow(clippy::declare_interior_mutable_const)] //! const BLOCK: BoxBlock = BoxBlock::new(); // <= //! static mut BLOCKS: [BoxBlock; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY]; -//! unsafe { &mut BLOCKS } +//! unsafe { addr_of_mut!(BLOCK).as_mut().unwrap()S } //! }; //! //! for block in blocks { @@ -317,6 +317,7 @@ pub struct BoxPoolImpl { } impl BoxPoolImpl { + #[allow(clippy::new_without_default)] pub const fn new() -> Self { Self { stack: Stack::new(), @@ -358,9 +359,16 @@ impl BoxBlock { } } +impl Default for BoxBlock { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + use std::ptr::addr_of_mut; use std::thread; use super::*; @@ -378,7 +386,7 @@ mod tests { let block = unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyBoxPool.manage(block); @@ -391,7 +399,7 @@ mod tests { let block = unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyBoxPool.manage(block); @@ -418,7 +426,7 @@ mod tests { let block = unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyBoxPool.manage(block); @@ -440,7 +448,7 @@ mod tests { let block = unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyBoxPool.manage(block); @@ -467,11 +475,11 @@ mod tests { MyBoxPool.manage(unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }); MyBoxPool.manage(unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }); let first = MyBoxPool.alloc(MyStruct).ok().unwrap(); @@ -500,7 +508,7 @@ mod tests { MyBoxPool.manage(unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }); let first = MyBoxPool.alloc(MyStruct).ok().unwrap(); @@ -534,11 +542,11 @@ mod tests { MyBoxPool.manage(unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }); MyBoxPool.manage(unsafe { static mut BLOCK: BoxBlock = BoxBlock::new(); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }); let boxed = MyBoxPool.alloc(MyStruct).ok().unwrap(); diff --git a/src/pool/object.rs b/src/pool/object.rs index 16364a470e..96b107939e 100644 --- a/src/pool/object.rs +++ b/src/pool/object.rs @@ -14,7 +14,7 @@ //! let block: &'static mut ObjectBlock<[u8; 128]> = unsafe { //! // unlike the memory pool APIs, an initial value must be specified here //! static mut BLOCK: ObjectBlock<[u8; 128]>= ObjectBlock::new([0; 128]); -//! &mut BLOCK +//! addr_of_mut!(BLOCK).as_mut().unwrap() //! }; //! //! // give object block to the pool @@ -55,7 +55,7 @@ //! let blocks: &'static mut [ObjectBlock<[u8; 128]>] = { //! const BLOCK: ObjectBlock<[u8; 128]> = ObjectBlock::new([0; 128]); // <= //! static mut BLOCKS: [ObjectBlock<[u8; 128]>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY]; -//! unsafe { &mut BLOCKS } +//! unsafe { addr_of_mut!(BLOCK).as_mut().unwrap()S } //! }; //! //! for block in blocks { @@ -332,6 +332,7 @@ impl ObjectBlock { #[cfg(test)] mod tests { use core::sync::atomic::{self, AtomicUsize}; + use std::ptr::addr_of_mut; use super::*; @@ -348,7 +349,7 @@ mod tests { let block = unsafe { static mut BLOCK: ObjectBlock = ObjectBlock::new(1); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyObjectPool.manage(block); @@ -361,7 +362,7 @@ mod tests { let block = unsafe { static mut BLOCK: ObjectBlock = ObjectBlock::new(1); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyObjectPool.manage(block); @@ -389,7 +390,7 @@ mod tests { let block = unsafe { static mut BLOCK: ObjectBlock = ObjectBlock::new(MyStruct); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyObjectPool.manage(block); @@ -411,7 +412,7 @@ mod tests { let block = unsafe { static mut BLOCK: ObjectBlock = ObjectBlock::new(Zst4096); - &mut BLOCK + addr_of_mut!(BLOCK).as_mut().unwrap() }; MyObjectPool.manage(block); diff --git a/src/pool/treiber.rs b/src/pool/treiber.rs index ca5c4b5835..15fb05b763 100644 --- a/src/pool/treiber.rs +++ b/src/pool/treiber.rs @@ -39,6 +39,8 @@ pub trait Node: Sized { type Data; fn next(&self) -> &AtomicPtr; + + #[allow(dead_code)] // used conditionally fn next_mut(&mut self) -> &mut AtomicPtr; }