mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-01 14:30:33 +00:00
commit
07c072c759
3
build.rs
3
build.rs
@ -9,6 +9,9 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
println!("cargo::rustc-check-cfg=cfg(arm_llsc)");
|
||||||
|
println!("cargo::rustc-check-cfg=cfg(has_atomic_load_store)");
|
||||||
|
|
||||||
let target = env::var("TARGET")?;
|
let target = env::var("TARGET")?;
|
||||||
|
|
||||||
// Manually list targets that have atomic load/store, but no CAS.
|
// Manually list targets that have atomic load/store, but no CAS.
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
|
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
|
||||||
//! let block: &'static mut ArcBlock<u128> = unsafe {
|
//! let block: &'static mut ArcBlock<u128> = unsafe {
|
||||||
//! static mut BLOCK: ArcBlock<u128> = ArcBlock::new();
|
//! static mut BLOCK: ArcBlock<u128> = ArcBlock::new();
|
||||||
//! &mut BLOCK
|
//! addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! MyArcPool.manage(block);
|
//! MyArcPool.manage(block);
|
||||||
@ -54,7 +54,7 @@
|
|||||||
//! let blocks: &'static mut [ArcBlock<u128>] = {
|
//! let blocks: &'static mut [ArcBlock<u128>] = {
|
||||||
//! const BLOCK: ArcBlock<u128> = ArcBlock::new(); // <=
|
//! const BLOCK: ArcBlock<u128> = ArcBlock::new(); // <=
|
||||||
//! static mut BLOCKS: [ArcBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
//! static mut BLOCKS: [ArcBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
||||||
//! unsafe { &mut BLOCKS }
|
//! unsafe { addr_of_mut!(BLOCK).as_mut().unwrap()S }
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! for block in blocks {
|
//! for block in blocks {
|
||||||
@ -162,6 +162,7 @@ pub struct ArcPoolImpl<T> {
|
|||||||
impl<T> ArcPoolImpl<T> {
|
impl<T> ArcPoolImpl<T> {
|
||||||
/// `arc_pool!` implementation detail
|
/// `arc_pool!` implementation detail
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
stack: Stack::new(),
|
stack: Stack::new(),
|
||||||
@ -387,9 +388,16 @@ impl<T> ArcBlock<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for ArcBlock<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::ptr::addr_of_mut;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cannot_alloc_if_empty() {
|
fn cannot_alloc_if_empty() {
|
||||||
@ -404,7 +412,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -417,7 +425,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -434,7 +442,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -449,7 +457,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -470,7 +478,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<i32> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -501,7 +509,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<MyStruct> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<MyStruct> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
@ -523,7 +531,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ArcBlock<Zst4096> = ArcBlock::new();
|
static mut BLOCK: ArcBlock<Zst4096> = ArcBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyArcPool.manage(block);
|
MyArcPool.manage(block);
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
|
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
|
||||||
//! let block: &'static mut BoxBlock<u128> = unsafe {
|
//! let block: &'static mut BoxBlock<u128> = unsafe {
|
||||||
//! static mut BLOCK: BoxBlock <u128>= BoxBlock::new();
|
//! static mut BLOCK: BoxBlock <u128>= BoxBlock::new();
|
||||||
//! &mut BLOCK
|
//! addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! // give block of memory to the pool
|
//! // give block of memory to the pool
|
||||||
@ -33,7 +33,7 @@
|
|||||||
//! // give another memory block to the pool
|
//! // give another memory block to the pool
|
||||||
//! MyBoxPool.manage(unsafe {
|
//! MyBoxPool.manage(unsafe {
|
||||||
//! static mut BLOCK: BoxBlock<u128> = BoxBlock::new();
|
//! static mut BLOCK: BoxBlock<u128> = BoxBlock::new();
|
||||||
//! &mut BLOCK
|
//! addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
//! });
|
//! });
|
||||||
//!
|
//!
|
||||||
//! // cloning also consumes a memory block from the pool
|
//! // cloning also consumes a memory block from the pool
|
||||||
@ -70,7 +70,7 @@
|
|||||||
//! #[allow(clippy::declare_interior_mutable_const)]
|
//! #[allow(clippy::declare_interior_mutable_const)]
|
||||||
//! const BLOCK: BoxBlock<u128> = BoxBlock::new(); // <=
|
//! const BLOCK: BoxBlock<u128> = BoxBlock::new(); // <=
|
||||||
//! static mut BLOCKS: [BoxBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
//! static mut BLOCKS: [BoxBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
||||||
//! unsafe { &mut BLOCKS }
|
//! unsafe { addr_of_mut!(BLOCK).as_mut().unwrap()S }
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! for block in blocks {
|
//! for block in blocks {
|
||||||
@ -317,6 +317,7 @@ pub struct BoxPoolImpl<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> BoxPoolImpl<T> {
|
impl<T> BoxPoolImpl<T> {
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
stack: Stack::new(),
|
stack: Stack::new(),
|
||||||
@ -358,9 +359,16 @@ impl<T> BoxBlock<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for BoxBlock<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
|
use std::ptr::addr_of_mut;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -378,7 +386,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: BoxBlock<i32> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<i32> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyBoxPool.manage(block);
|
MyBoxPool.manage(block);
|
||||||
|
|
||||||
@ -391,7 +399,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: BoxBlock<i32> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<i32> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyBoxPool.manage(block);
|
MyBoxPool.manage(block);
|
||||||
|
|
||||||
@ -418,7 +426,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyBoxPool.manage(block);
|
MyBoxPool.manage(block);
|
||||||
|
|
||||||
@ -440,7 +448,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: BoxBlock<Zst4096> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<Zst4096> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyBoxPool.manage(block);
|
MyBoxPool.manage(block);
|
||||||
|
|
||||||
@ -467,11 +475,11 @@ mod tests {
|
|||||||
|
|
||||||
MyBoxPool.manage(unsafe {
|
MyBoxPool.manage(unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
});
|
});
|
||||||
MyBoxPool.manage(unsafe {
|
MyBoxPool.manage(unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
let first = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
let first = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
||||||
@ -500,7 +508,7 @@ mod tests {
|
|||||||
|
|
||||||
MyBoxPool.manage(unsafe {
|
MyBoxPool.manage(unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
let first = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
let first = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
||||||
@ -534,11 +542,11 @@ mod tests {
|
|||||||
|
|
||||||
MyBoxPool.manage(unsafe {
|
MyBoxPool.manage(unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
});
|
});
|
||||||
MyBoxPool.manage(unsafe {
|
MyBoxPool.manage(unsafe {
|
||||||
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
static mut BLOCK: BoxBlock<MyStruct> = BoxBlock::new();
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
let boxed = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
let boxed = MyBoxPool.alloc(MyStruct).ok().unwrap();
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
//! let block: &'static mut ObjectBlock<[u8; 128]> = unsafe {
|
//! let block: &'static mut ObjectBlock<[u8; 128]> = unsafe {
|
||||||
//! // unlike the memory pool APIs, an initial value must be specified here
|
//! // unlike the memory pool APIs, an initial value must be specified here
|
||||||
//! static mut BLOCK: ObjectBlock<[u8; 128]>= ObjectBlock::new([0; 128]);
|
//! 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
|
//! // give object block to the pool
|
||||||
@ -55,7 +55,7 @@
|
|||||||
//! let blocks: &'static mut [ObjectBlock<[u8; 128]>] = {
|
//! let blocks: &'static mut [ObjectBlock<[u8; 128]>] = {
|
||||||
//! const BLOCK: ObjectBlock<[u8; 128]> = ObjectBlock::new([0; 128]); // <=
|
//! const BLOCK: ObjectBlock<[u8; 128]> = ObjectBlock::new([0; 128]); // <=
|
||||||
//! static mut BLOCKS: [ObjectBlock<[u8; 128]>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
//! 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 {
|
//! for block in blocks {
|
||||||
@ -332,6 +332,7 @@ impl<T> ObjectBlock<T> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use core::sync::atomic::{self, AtomicUsize};
|
use core::sync::atomic::{self, AtomicUsize};
|
||||||
|
use std::ptr::addr_of_mut;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@ -348,7 +349,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ObjectBlock<i32> = ObjectBlock::new(1);
|
static mut BLOCK: ObjectBlock<i32> = ObjectBlock::new(1);
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyObjectPool.manage(block);
|
MyObjectPool.manage(block);
|
||||||
|
|
||||||
@ -361,7 +362,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ObjectBlock<i32> = ObjectBlock::new(1);
|
static mut BLOCK: ObjectBlock<i32> = ObjectBlock::new(1);
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyObjectPool.manage(block);
|
MyObjectPool.manage(block);
|
||||||
|
|
||||||
@ -389,7 +390,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ObjectBlock<MyStruct> = ObjectBlock::new(MyStruct);
|
static mut BLOCK: ObjectBlock<MyStruct> = ObjectBlock::new(MyStruct);
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyObjectPool.manage(block);
|
MyObjectPool.manage(block);
|
||||||
|
|
||||||
@ -411,7 +412,7 @@ mod tests {
|
|||||||
|
|
||||||
let block = unsafe {
|
let block = unsafe {
|
||||||
static mut BLOCK: ObjectBlock<Zst4096> = ObjectBlock::new(Zst4096);
|
static mut BLOCK: ObjectBlock<Zst4096> = ObjectBlock::new(Zst4096);
|
||||||
&mut BLOCK
|
addr_of_mut!(BLOCK).as_mut().unwrap()
|
||||||
};
|
};
|
||||||
MyObjectPool.manage(block);
|
MyObjectPool.manage(block);
|
||||||
|
|
||||||
|
@ -39,6 +39,8 @@ pub trait Node: Sized {
|
|||||||
type Data;
|
type Data;
|
||||||
|
|
||||||
fn next(&self) -> &AtomicPtr<Self>;
|
fn next(&self) -> &AtomicPtr<Self>;
|
||||||
|
|
||||||
|
#[allow(dead_code)] // used conditionally
|
||||||
fn next_mut(&mut self) -> &mut AtomicPtr<Self>;
|
fn next_mut(&mut self) -> &mut AtomicPtr<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user