|
47 | 47 | //! // ..
|
48 | 48 | //! }
|
49 | 49 | //! ```
|
| 50 | +//! |
| 51 | +//! The `grow_exact` API is also available on the "Arc pool". It requires using |
| 52 | +//! `Node<ArcInner<Type>>` as the array element type. Example below: |
| 53 | +//! |
| 54 | +//! ``` ignore |
| 55 | +//! use heapless::pool::{singleton::arc::ArcInner, Node}; |
| 56 | +//! |
| 57 | +//! pub struct BigStruct { /* .. */ } |
| 58 | +//! |
| 59 | +//! arc_pool!(P: BigStruct); |
| 60 | +//! |
| 61 | +//! #[cortex_m_rt::entry] |
| 62 | +//! fn main() -> ! { |
| 63 | +//! static mut MEMORY: MaybeUninit<[Node<ArcInner<BigStruct>>; 2]> = MaybeUninit::uninit(); |
| 64 | +//! |
| 65 | +//! P::grow_exact(MEMORY); |
| 66 | +//! |
| 67 | +//! // 2 allocations are guaranteed to work |
| 68 | +//! let x = P::alloc(BigStruct::new()).ok().expect("OOM"); |
| 69 | +//! let y = P::alloc(BigStruct::new()).ok().expect("OOM"); |
| 70 | +//! |
| 71 | +//! // .. |
| 72 | +//! } |
| 73 | +//! ``` |
50 | 74 |
|
51 | 75 | use core::{
|
52 | 76 | cmp, fmt,
|
@@ -114,6 +138,16 @@ macro_rules! arc_pool {
|
114 | 138 | pub fn grow(memory: &'static mut [u8]) -> usize {
|
115 | 139 | <Self as $crate::pool::singleton::arc::Pool>::ptr().grow(memory)
|
116 | 140 | }
|
| 141 | + |
| 142 | + /// Increases the capacity of the pool |
| 143 | + /// |
| 144 | + /// Unlike `grow`, this method fully utilizes the given memory block |
| 145 | + pub fn grow_exact<A>(memory: &'static mut MaybeUninit<A>) -> usize |
| 146 | + where |
| 147 | + A: AsMut<[$crate::pool::Node<$crate::pool::singleton::arc::ArcInner<$ty>>]>, |
| 148 | + { |
| 149 | + <Self as $crate::pool::singleton::arc::Pool>::ptr().grow_exact(memory) |
| 150 | + } |
117 | 151 | }
|
118 | 152 | };
|
119 | 153 | }
|
@@ -348,7 +382,7 @@ where
|
348 | 382 |
|
349 | 383 | impl<P> Unpin for Arc<P> where P: Pool {}
|
350 | 384 |
|
351 |
| -#[doc(hidden)] |
| 385 | +/// Unfortunate implementation detail required to use the `grow_exact` API |
352 | 386 | pub struct ArcInner<T> {
|
353 | 387 | data: T,
|
354 | 388 | strong: AtomicUsize,
|
|
0 commit comments