1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Object pool API
//!
//! # Example usage
//!
//! ```
//! use heapless::{object_pool, pool::object::{Object, ObjectBlock}};
//!
//! object_pool!(P: [u8; 128]);
//!
//! // cannot request objects without first giving object blocks to the pool
//! assert!(P.request().is_none());
//!
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
//! let block: &'static mut ObjectBlock<[u8; 128]> = unsafe {
//!     // unlike the memory pool APIs, an initial value must be specified here
//!     static mut B: ObjectBlock<[u8; 128]>= ObjectBlock::new([0; 128]);
//!     &mut B
//! };
//!
//! // give object block to the pool
//! P.manage(block);
//!
//! // it's now possible to request objects
//! // unlike the memory pool APIs, no initial value is required here
//! let mut object = P.request().unwrap();
//!
//! // mutation is possible
//! object.iter_mut().for_each(|byte| *byte = byte.wrapping_add(1));
//!
//! // the number of live objects is limited to the number of blocks managed by the pool
//! let res = P.request();
//! assert!(res.is_none());
//!
//! // `object`'s destructor returns the object to the pool
//! drop(object);
//!
//! // it's possible to request an `Object` again
//! let res = P.request();
//!
//! assert!(res.is_some());
//! ```
//!
//! # Array block initialization
//!
//! You can create a static variable that contains an array of memory blocks and give all the blocks
//! to the `ObjectPool`. This requires an intermediate `const` value as shown below:
//!
//! ```
//! use heapless::{object_pool, pool::object::ObjectBlock};
//!
//! object_pool!(P: [u8; 128]);
//!
//! const POOL_CAPACITY: usize = 8;
//!
//! 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 }
//! };
//!
//! for block in blocks {
//!     P.manage(block);
//! }
//! ```

use core::{
    cmp::Ordering,
    fmt,
    hash::{Hash, Hasher},
    mem::ManuallyDrop,
    ops, ptr,
};

use stable_deref_trait::StableDeref;

use super::treiber::{AtomicPtr, NonNullPtr, Stack, StructNode};

/// Creates a new `ObjectPool` singleton with the given `$name` that manages the specified
/// `$data_type`
///
/// For more extensive documentation see the [module level documentation](crate::pool::object)
#[macro_export]
macro_rules! object_pool {
    ($name:ident: $data_type:ty) => {
        pub struct $name;

        impl $crate::pool::object::ObjectPool for $name {
            type Data = $data_type;

            fn singleton() -> &'static $crate::pool::object::ObjectPoolImpl<$data_type> {
                static $name: $crate::pool::object::ObjectPoolImpl<$data_type> =
                    $crate::pool::object::ObjectPoolImpl::new();

                &$name
            }
        }

        impl $name {
            /// Inherent method version of `ObjectPool::request`
            #[allow(dead_code)]
            pub fn request(&self) -> Option<$crate::pool::object::Object<$name>> {
                <$name as $crate::pool::object::ObjectPool>::request()
            }

            /// Inherent method version of `ObjectPool::manage`
            #[allow(dead_code)]
            pub fn manage(
                &self,
                block: &'static mut $crate::pool::object::ObjectBlock<$data_type>,
            ) {
                <$name as $crate::pool::object::ObjectPool>::manage(block)
            }
        }
    };
}

/// A singleton that manages `pool::object::Object`s
pub trait ObjectPool: Sized {
    /// The data type of the objects managed by the object pool
    type Data: 'static;

    /// `object_pool!` implementation detail
    #[doc(hidden)]
    fn singleton() -> &'static ObjectPoolImpl<Self::Data>;

    /// Request a new object from the pool
    fn request() -> Option<Object<Self>> {
        Self::singleton()
            .request()
            .map(|node_ptr| Object { node_ptr })
    }

    /// Adds a statically allocate object to the pool
    fn manage(block: &'static mut ObjectBlock<Self::Data>) {
        Self::singleton().manage(block)
    }
}

/// `object_pool!` implementation detail
#[doc(hidden)]
pub struct ObjectPoolImpl<T> {
    stack: Stack<StructNode<T>>,
}

impl<T> ObjectPoolImpl<T> {
    /// `object_pool!` implementation detail
    #[doc(hidden)]
    pub const fn new() -> Self {
        Self {
            stack: Stack::new(),
        }
    }

    fn request(&self) -> Option<NonNullPtr<StructNode<T>>> {
        self.stack.try_pop()
    }

    fn manage(&self, block: &'static mut ObjectBlock<T>) {
        let node: &'static mut _ = &mut block.node;

        unsafe { self.stack.push(NonNullPtr::from_static_mut_ref(node)) }
    }
}

// `T needs` to be Send because returning an object from a thread and then
// requesting it from another is effectively a cross-thread 'send' operation
unsafe impl<T> Sync for ObjectPoolImpl<T> where T: Send {}

/// An object managed by object pool `P`
pub struct Object<P>
where
    P: ObjectPool,
{
    node_ptr: NonNullPtr<StructNode<P::Data>>,
}

impl<A, T, const N: usize> AsMut<[T]> for Object<A>
where
    A: ObjectPool<Data = [T; N]>,
{
    fn as_mut(&mut self) -> &mut [T] {
        &mut **self
    }
}

impl<A, T, const N: usize> AsRef<[T]> for Object<A>
where
    A: ObjectPool<Data = [T; N]>,
{
    fn as_ref(&self) -> &[T] {
        &**self
    }
}

impl<A> fmt::Debug for Object<A>
where
    A: ObjectPool,
    A::Data: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        A::Data::fmt(self, f)
    }
}

impl<A> ops::Deref for Object<A>
where
    A: ObjectPool,
{
    type Target = A::Data;

    fn deref(&self) -> &Self::Target {
        unsafe { &*ptr::addr_of!((*self.node_ptr.as_ptr()).data) }
    }
}

impl<A> ops::DerefMut for Object<A>
where
    A: ObjectPool,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *ptr::addr_of_mut!((*self.node_ptr.as_ptr()).data) }
    }
}

unsafe impl<A> StableDeref for Object<A> where A: ObjectPool {}

impl<A> fmt::Display for Object<A>
where
    A: ObjectPool,
    A::Data: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        A::Data::fmt(self, f)
    }
}

impl<P> Drop for Object<P>
where
    P: ObjectPool,
{
    fn drop(&mut self) {
        unsafe { P::singleton().stack.push(self.node_ptr) }
    }
}

impl<A> Eq for Object<A>
where
    A: ObjectPool,
    A::Data: Eq,
{
}

impl<A> Hash for Object<A>
where
    A: ObjectPool,
    A::Data: Hash,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        (**self).hash(state)
    }
}

impl<A> Ord for Object<A>
where
    A: ObjectPool,
    A::Data: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        A::Data::cmp(self, other)
    }
}

impl<A, B> PartialEq<Object<B>> for Object<A>
where
    A: ObjectPool,
    B: ObjectPool,
    A::Data: PartialEq<B::Data>,
{
    fn eq(&self, other: &Object<B>) -> bool {
        A::Data::eq(self, other)
    }
}

impl<A, B> PartialOrd<Object<B>> for Object<A>
where
    A: ObjectPool,
    B: ObjectPool,
    A::Data: PartialOrd<B::Data>,
{
    fn partial_cmp(&self, other: &Object<B>) -> Option<Ordering> {
        A::Data::partial_cmp(self, other)
    }
}

unsafe impl<P> Send for Object<P>
where
    P: ObjectPool,
    P::Data: Send,
{
}

unsafe impl<P> Sync for Object<P>
where
    P: ObjectPool,
    P::Data: Sync,
{
}

/// An object "block" of data type `T` that has not yet been associated to an `ObjectPool`
pub struct ObjectBlock<T> {
    node: StructNode<T>,
}

impl<T> ObjectBlock<T> {
    /// Creates a new object block with the given `initial_value`
    pub const fn new(initial_value: T) -> Self {
        Self {
            node: StructNode {
                next: ManuallyDrop::new(AtomicPtr::null()),
                data: ManuallyDrop::new(initial_value),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use core::sync::atomic::{self, AtomicUsize};

    use super::*;

    #[test]
    fn cannot_request_if_empty() {
        object_pool!(P: i32);

        assert_eq!(None, P.request());
    }

    #[test]
    fn can_request_if_manages_one_block() {
        object_pool!(P: i32);

        let block = unsafe {
            static mut B: ObjectBlock<i32> = ObjectBlock::new(1);
            &mut B
        };
        P.manage(block);

        assert_eq!(1, *P.request().unwrap());
    }

    #[test]
    fn request_drop_request() {
        object_pool!(P: i32);

        let block = unsafe {
            static mut B: ObjectBlock<i32> = ObjectBlock::new(1);
            &mut B
        };
        P.manage(block);

        let mut object = P.request().unwrap();

        *object = 2;
        drop(object);

        assert_eq!(2, *P.request().unwrap());
    }

    #[test]
    fn destructor_does_not_run_on_drop() {
        static COUNT: AtomicUsize = AtomicUsize::new(0);

        pub struct S;

        impl Drop for S {
            fn drop(&mut self) {
                COUNT.fetch_add(1, atomic::Ordering::Relaxed);
            }
        }

        object_pool!(P: S);

        let block = unsafe {
            static mut B: ObjectBlock<S> = ObjectBlock::new(S);
            &mut B
        };
        P.manage(block);

        let object = P.request().unwrap();

        assert_eq!(0, COUNT.load(atomic::Ordering::Relaxed));

        drop(object);

        assert_eq!(0, COUNT.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn zst_is_well_aligned() {
        #[repr(align(4096))]
        pub struct Zst4096;

        object_pool!(P: Zst4096);

        let block = unsafe {
            static mut B: ObjectBlock<Zst4096> = ObjectBlock::new(Zst4096);
            &mut B
        };
        P.manage(block);

        let object = P.request().unwrap();

        let raw = &*object as *const Zst4096;
        assert_eq!(0, raw as usize % 4096);
    }
}