From 6e8e26cc40ed571a4ce70f337ac897ced5bbcc62 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 10 Sep 2021 16:16:58 +0200 Subject: [PATCH] add grow_exact + docs --- src/pool/singleton/arc.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/pool/singleton/arc.rs b/src/pool/singleton/arc.rs index 90a915f2..2eb55683 100644 --- a/src/pool/singleton/arc.rs +++ b/src/pool/singleton/arc.rs @@ -47,6 +47,30 @@ //! // .. //! } //! ``` +//! +//! The `grow_exact` API is also available on the "Arc pool". It requires using +//! `Node>` as the array element type. Example below: +//! +//! ``` ignore +//! use heapless::pool::{singleton::arc::ArcInner, Node}; +//! +//! pub struct BigStruct { /* .. */ } +//! +//! arc_pool!(P: BigStruct); +//! +//! #[cortex_m_rt::entry] +//! fn main() -> ! { +//! static mut MEMORY: MaybeUninit<[Node>; 2]> = MaybeUninit::uninit(); +//! +//! P::grow_exact(MEMORY); +//! +//! // 2 allocations are guaranteed to work +//! let x = P::alloc(BigStruct::new()).ok().expect("OOM"); +//! let y = P::alloc(BigStruct::new()).ok().expect("OOM"); +//! +//! // .. +//! } +//! ``` use core::{ cmp, fmt, @@ -114,6 +138,16 @@ macro_rules! arc_pool { pub fn grow(memory: &'static mut [u8]) -> usize { ::ptr().grow(memory) } + + /// Increases the capacity of the pool + /// + /// Unlike `grow`, this method fully utilizes the given memory block + pub fn grow_exact(memory: &'static mut MaybeUninit) -> usize + where + A: AsMut<[$crate::pool::Node<$crate::pool::singleton::arc::ArcInner<$ty>>]>, + { + ::ptr().grow_exact(memory) + } } }; } @@ -348,7 +382,7 @@ where impl

Unpin for Arc

where P: Pool {} -#[doc(hidden)] +/// Unfortunate implementation detail required to use the `grow_exact` API pub struct ArcInner { data: T, strong: AtomicUsize,