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,18 +83,16 @@ 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> {
93
- #[ allow( non_upper_case_globals) ]
94
- static $name: $crate:: pool:: arc:: ArcPoolImpl <$data_type> =
92
+ static POOL : $crate:: pool:: arc:: ArcPoolImpl <$data_type> =
95
93
$crate:: pool:: arc:: ArcPoolImpl :: new( ) ;
96
94
97
- & $name
95
+ & POOL
98
96
}
99
97
}
100
98
@@ -386,67 +384,67 @@ mod tests {
386
384
387
385
#[ test]
388
386
fn cannot_alloc_if_empty ( ) {
389
- arc_pool ! ( P : i32 ) ;
387
+ arc_pool ! ( MyArcPool : i32 ) ;
390
388
391
- assert_eq ! ( Err ( 42 ) , P . alloc( 42 ) , ) ;
389
+ assert_eq ! ( Err ( 42 ) , MyArcPool . alloc( 42 ) , ) ;
392
390
}
393
391
394
392
#[ test]
395
393
fn can_alloc_if_manages_one_block ( ) {
396
- arc_pool ! ( P : i32 ) ;
394
+ arc_pool ! ( MyArcPool : i32 ) ;
397
395
398
396
let block = unsafe {
399
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
400
- & mut B
397
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
398
+ & mut BLOCK
401
399
} ;
402
- P . manage ( block) ;
400
+ MyArcPool . manage ( block) ;
403
401
404
- assert_eq ! ( 42 , * P . alloc( 42 ) . unwrap( ) ) ;
402
+ assert_eq ! ( 42 , * MyArcPool . alloc( 42 ) . unwrap( ) ) ;
405
403
}
406
404
407
405
#[ test]
408
406
fn alloc_drop_alloc ( ) {
409
- arc_pool ! ( P : i32 ) ;
407
+ arc_pool ! ( MyArcPool : i32 ) ;
410
408
411
409
let block = unsafe {
412
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
413
- & mut B
410
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
411
+ & mut BLOCK
414
412
} ;
415
- P . manage ( block) ;
413
+ MyArcPool . manage ( block) ;
416
414
417
- let arc = P . alloc ( 1 ) . unwrap ( ) ;
415
+ let arc = MyArcPool . alloc ( 1 ) . unwrap ( ) ;
418
416
419
417
drop ( arc) ;
420
418
421
- assert_eq ! ( 2 , * P . alloc( 2 ) . unwrap( ) ) ;
419
+ assert_eq ! ( 2 , * MyArcPool . alloc( 2 ) . unwrap( ) ) ;
422
420
}
423
421
424
422
#[ test]
425
423
fn strong_count_starts_at_one ( ) {
426
- arc_pool ! ( P : i32 ) ;
424
+ arc_pool ! ( MyArcPool : i32 ) ;
427
425
428
426
let block = unsafe {
429
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
430
- & mut B
427
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
428
+ & mut BLOCK
431
429
} ;
432
- P . manage ( block) ;
430
+ MyArcPool . manage ( block) ;
433
431
434
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
432
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
435
433
436
434
assert_eq ! ( 1 , arc. inner( ) . strong. load( Ordering :: Relaxed ) ) ;
437
435
}
438
436
439
437
#[ test]
440
438
fn clone_increases_strong_count ( ) {
441
- arc_pool ! ( P : i32 ) ;
439
+ arc_pool ! ( MyArcPool : i32 ) ;
442
440
443
441
let block = unsafe {
444
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
445
- & mut B
442
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
443
+ & mut BLOCK
446
444
} ;
447
- P . manage ( block) ;
445
+ MyArcPool . manage ( block) ;
448
446
449
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
447
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
450
448
451
449
let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
452
450
@@ -459,15 +457,15 @@ mod tests {
459
457
460
458
#[ test]
461
459
fn drop_decreases_strong_count ( ) {
462
- arc_pool ! ( P : i32 ) ;
460
+ arc_pool ! ( MyArcPool : i32 ) ;
463
461
464
462
let block = unsafe {
465
- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
466
- & mut B
463
+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
464
+ & mut BLOCK
467
465
} ;
468
- P . manage ( block) ;
466
+ MyArcPool . manage ( block) ;
469
467
470
- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
468
+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
471
469
let arc2 = arc. clone ( ) ;
472
470
473
471
let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
@@ -482,23 +480,23 @@ mod tests {
482
480
fn runs_destructor_exactly_once_when_strong_count_reaches_zero ( ) {
483
481
static COUNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
484
482
485
- pub struct S ;
483
+ pub struct MyStruct ;
486
484
487
- impl Drop for S {
485
+ impl Drop for MyStruct {
488
486
fn drop ( & mut self ) {
489
487
COUNT . fetch_add ( 1 , Ordering :: Relaxed ) ;
490
488
}
491
489
}
492
490
493
- arc_pool ! ( P : S ) ;
491
+ arc_pool ! ( MyArcPool : MyStruct ) ;
494
492
495
493
let block = unsafe {
496
- static mut B : ArcBlock < S > = ArcBlock :: new ( ) ;
497
- & mut B
494
+ static mut BLOCK : ArcBlock < MyStruct > = ArcBlock :: new ( ) ;
495
+ & mut BLOCK
498
496
} ;
499
- P . manage ( block) ;
497
+ MyArcPool . manage ( block) ;
500
498
501
- let arc = P . alloc ( S ) . ok ( ) . unwrap ( ) ;
499
+ let arc = MyArcPool . alloc ( MyStruct ) . ok ( ) . unwrap ( ) ;
502
500
503
501
assert_eq ! ( 0 , COUNT . load( Ordering :: Relaxed ) ) ;
504
502
@@ -512,24 +510,17 @@ mod tests {
512
510
#[ repr( align( 4096 ) ) ]
513
511
pub struct Zst4096 ;
514
512
515
- arc_pool ! ( P : Zst4096 ) ;
513
+ arc_pool ! ( MyArcPool : Zst4096 ) ;
516
514
517
515
let block = unsafe {
518
- static mut B : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
519
- & mut B
516
+ static mut BLOCK : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
517
+ & mut BLOCK
520
518
} ;
521
- P . manage ( block) ;
519
+ MyArcPool . manage ( block) ;
522
520
523
- let arc = P . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
521
+ let arc = MyArcPool . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
524
522
525
523
let raw = & * arc as * const Zst4096 ;
526
524
assert_eq ! ( 0 , raw as usize % 4096 ) ;
527
525
}
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
526
}
0 commit comments