5
5
//! ```
6
6
//! use heapless::{arc_pool, pool::arc::{Arc, ArcBlock}};
7
7
//!
8
- //! arc_pool!(P : u128);
8
+ //! arc_pool!(MyArcPool : u128);
9
9
//!
10
10
//! // cannot allocate without first giving memory blocks to the pool
11
- //! assert!(P .alloc(42).is_err());
11
+ //! assert!(MyArcPool .alloc(42).is_err());
12
12
//!
13
13
//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
14
14
//! let block: &'static mut ArcBlock<u128> = unsafe {
15
- //! static mut B : ArcBlock<u128> = ArcBlock::new();
16
- //! &mut B
15
+ //! static mut BLOCK : ArcBlock<u128> = ArcBlock::new();
16
+ //! &mut BLOCK
17
17
//! };
18
18
//!
19
- //! P .manage(block);
19
+ //! MyArcPool .manage(block);
20
20
//!
21
- //! let arc = P .alloc(1).unwrap();
21
+ //! let arc = MyArcPool .alloc(1).unwrap();
22
22
//!
23
23
//! // number of smart pointers is limited to the number of blocks managed by the pool
24
- //! let res = P .alloc(2);
24
+ //! let res = MyArcPool .alloc(2);
25
25
//! assert!(res.is_err());
26
26
//!
27
27
//! // but cloning does not consume an `ArcBlock`
34
34
//! drop(arc); // release memory
35
35
//!
36
36
//! // it's now possible to allocate a new `Arc` smart pointer
37
- //! let res = P .alloc(3);
37
+ //! let res = MyArcPool .alloc(3);
38
38
//!
39
39
//! assert!(res.is_ok());
40
40
//! ```
47
47
//! ```
48
48
//! use heapless::{arc_pool, pool::arc::ArcBlock};
49
49
//!
50
- //! arc_pool!(P : u128);
50
+ //! arc_pool!(MyArcPool : u128);
51
51
//!
52
52
//! const POOL_CAPACITY: usize = 8;
53
53
//!
58
58
//! };
59
59
//!
60
60
//! for block in blocks {
61
- //! P .manage(block);
61
+ //! MyArcPool .manage(block);
62
62
//! }
63
63
//! ```
64
64
@@ -83,13 +83,14 @@ use super::treiber::{NonNullPtr, Stack, UnionNode};
83
83
#[ macro_export]
84
84
macro_rules! arc_pool {
85
85
( $name: ident: $data_type: ty) => {
86
- #[ allow( non_camel_case_types) ]
87
86
pub struct $name;
88
87
89
88
impl $crate:: pool:: arc:: ArcPool for $name {
90
89
type Data = $data_type;
91
90
92
91
fn singleton( ) -> & ' static $crate:: pool:: arc:: ArcPoolImpl <$data_type> {
92
+ // Even though the static variable is not exposed to user code, it is
93
+ // still useful to have a descriptive symbol name for debugging.
93
94
#[ allow( non_upper_case_globals) ]
94
95
static $name: $crate:: pool:: arc:: ArcPoolImpl <$data_type> =
95
96
$crate:: pool:: arc:: ArcPoolImpl :: new( ) ;
@@ -386,67 +387,67 @@ mod tests {
386
387
387
388
#[ test]
388
389
fn cannot_alloc_if_empty ( ) {
389
- arc_pool ! ( P : i32 ) ;
390
+ arc_pool ! ( MyArcPool : i32 ) ;
390
391
391
- assert_eq ! ( Err ( 42 ) , P . alloc( 42 ) , ) ;
392
+ assert_eq ! ( Err ( 42 ) , MyArcPool . alloc( 42 ) , ) ;
392
393
}
393
394
394
395
#[ test]
395
396
fn can_alloc_if_manages_one_block ( ) {
396
- arc_pool ! ( P : i32 ) ;
397
+ arc_pool ! ( MyArcPool : i32 ) ;
397
398
398
399
let block = unsafe {
399
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
400
- & mut B
400
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
401
+ & mut BLOCK
401
402
} ;
402
- P . manage ( block) ;
403
+ MyArcPool . manage ( block) ;
403
404
404
- assert_eq ! ( 42 , * P . alloc( 42 ) . unwrap( ) ) ;
405
+ assert_eq ! ( 42 , * MyArcPool . alloc( 42 ) . unwrap( ) ) ;
405
406
}
406
407
407
408
#[ test]
408
409
fn alloc_drop_alloc ( ) {
409
- arc_pool ! ( P : i32 ) ;
410
+ arc_pool ! ( MyArcPool : i32 ) ;
410
411
411
412
let block = unsafe {
412
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
413
- & mut B
413
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
414
+ & mut BLOCK
414
415
} ;
415
- P . manage ( block) ;
416
+ MyArcPool . manage ( block) ;
416
417
417
- let arc = P . alloc ( 1 ) . unwrap ( ) ;
418
+ let arc = MyArcPool . alloc ( 1 ) . unwrap ( ) ;
418
419
419
420
drop ( arc) ;
420
421
421
- assert_eq ! ( 2 , * P . alloc( 2 ) . unwrap( ) ) ;
422
+ assert_eq ! ( 2 , * MyArcPool . alloc( 2 ) . unwrap( ) ) ;
422
423
}
423
424
424
425
#[ test]
425
426
fn strong_count_starts_at_one ( ) {
426
- arc_pool ! ( P : i32 ) ;
427
+ arc_pool ! ( MyArcPool : i32 ) ;
427
428
428
429
let block = unsafe {
429
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
430
- & mut B
430
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
431
+ & mut BLOCK
431
432
} ;
432
- P . manage ( block) ;
433
+ MyArcPool . manage ( block) ;
433
434
434
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
435
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
435
436
436
437
assert_eq ! ( 1 , arc. inner( ) . strong. load( Ordering :: Relaxed ) ) ;
437
438
}
438
439
439
440
#[ test]
440
441
fn clone_increases_strong_count ( ) {
441
- arc_pool ! ( P : i32 ) ;
442
+ arc_pool ! ( MyArcPool : i32 ) ;
442
443
443
444
let block = unsafe {
444
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
445
- & mut B
445
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
446
+ & mut BLOCK
446
447
} ;
447
- P . manage ( block) ;
448
+ MyArcPool . manage ( block) ;
448
449
449
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
450
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
450
451
451
452
let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
452
453
@@ -459,15 +460,15 @@ mod tests {
459
460
460
461
#[ test]
461
462
fn drop_decreases_strong_count ( ) {
462
- arc_pool ! ( P : i32 ) ;
463
+ arc_pool ! ( MyArcPool : i32 ) ;
463
464
464
465
let block = unsafe {
465
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
466
- & mut B
466
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
467
+ & mut BLOCK
467
468
} ;
468
- P . manage ( block) ;
469
+ MyArcPool . manage ( block) ;
469
470
470
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
471
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
471
472
let arc2 = arc. clone ( ) ;
472
473
473
474
let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
@@ -482,23 +483,23 @@ mod tests {
482
483
fn runs_destructor_exactly_once_when_strong_count_reaches_zero ( ) {
483
484
static COUNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
484
485
485
- pub struct S ;
486
+ pub struct MyStruct ;
486
487
487
- impl Drop for S {
488
+ impl Drop for MyStruct {
488
489
fn drop ( & mut self ) {
489
490
COUNT . fetch_add ( 1 , Ordering :: Relaxed ) ;
490
491
}
491
492
}
492
493
493
- arc_pool ! ( P : S ) ;
494
+ arc_pool ! ( MyArcPool : MyStruct ) ;
494
495
495
496
let block = unsafe {
496
- static mut B : ArcBlock < S > = ArcBlock :: new ( ) ;
497
- & mut B
497
+ static mut BLOCK : ArcBlock < MyStruct > = ArcBlock :: new ( ) ;
498
+ & mut BLOCK
498
499
} ;
499
- P . manage ( block) ;
500
+ MyArcPool . manage ( block) ;
500
501
501
- let arc = P . alloc ( S ) . ok ( ) . unwrap ( ) ;
502
+ let arc = MyArcPool . alloc ( MyStruct ) . ok ( ) . unwrap ( ) ;
502
503
503
504
assert_eq ! ( 0 , COUNT . load( Ordering :: Relaxed ) ) ;
504
505
@@ -512,24 +513,17 @@ mod tests {
512
513
#[ repr( align( 4096 ) ) ]
513
514
pub struct Zst4096 ;
514
515
515
- arc_pool ! ( P : Zst4096 ) ;
516
+ arc_pool ! ( MyArcPool : Zst4096 ) ;
516
517
517
518
let block = unsafe {
518
- static mut B : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
519
- & mut B
519
+ static mut BLOCK : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
520
+ & mut BLOCK
520
521
} ;
521
- P . manage ( block) ;
522
+ MyArcPool . manage ( block) ;
522
523
523
- let arc = P . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
524
+ let arc = MyArcPool . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
524
525
525
526
let raw = & * arc as * const Zst4096 ;
526
527
assert_eq ! ( 0 , raw as usize % 4096 ) ;
527
528
}
528
-
529
- #[ test]
530
- fn arc_pool_case ( ) {
531
- // https://github.com/rust-embedded/heapless/issues/411
532
- arc_pool ! ( CamelCaseType : u128 ) ;
533
- arc_pool ! ( SCREAMING_SNAKE_CASE_TYPE : u128 ) ;
534
- }
535
529
}
0 commit comments