@@ -9,12 +9,7 @@ use crate::assert_valid_number_of_channels;
9
9
use crate :: { MAX_CHANNELS , RENDER_QUANTUM_SIZE } ;
10
10
11
11
// object pool for `AudioRenderQuantumChannel`s, only allocate if the pool is empty
12
- pub ( crate ) struct Alloc {
13
- inner : Rc < AllocInner > ,
14
- }
15
-
16
- #[ derive( Debug ) ]
17
- struct AllocInner { }
12
+ pub ( crate ) struct Alloc ;
18
13
19
14
thread_local ! {
20
15
static POOL : RefCell <Vec <Rc <[ f32 ; RENDER_QUANTUM_SIZE ] >>> = RefCell :: new( Vec :: with_capacity( 32 ) ) ;
@@ -23,26 +18,17 @@ thread_local! {
23
18
24
19
impl Alloc {
25
20
pub fn with_capacity ( n : usize ) -> Self {
26
- let pool: Vec < _ > = ( 0 ..n) . map ( |_| Rc :: new ( [ 0. ; RENDER_QUANTUM_SIZE ] ) ) . collect ( ) ;
27
- POOL . set ( pool) ;
28
-
29
- Self {
30
- inner : Rc :: new ( AllocInner { } ) ,
31
- }
21
+ Self { }
32
22
}
33
23
34
24
#[ cfg( test) ]
35
25
pub fn allocate ( & self ) -> AudioRenderQuantumChannel {
36
- AudioRenderQuantumChannel {
37
- data : self . inner . allocate ( ) ,
38
- alloc : Rc :: clone ( & self . inner ) ,
39
- }
26
+ AudioRenderQuantumChannel { data : allocate ( ) }
40
27
}
41
28
42
29
pub fn silence ( & self ) -> AudioRenderQuantumChannel {
43
30
AudioRenderQuantumChannel {
44
31
data : ZEROES . with ( Rc :: clone) ,
45
- alloc : Rc :: clone ( & self . inner ) ,
46
32
}
47
33
}
48
34
@@ -52,20 +38,18 @@ impl Alloc {
52
38
}
53
39
}
54
40
55
- impl AllocInner {
56
- fn allocate ( & self ) -> Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > {
57
- if let Some ( rc) = POOL . with_borrow_mut ( |p| p. pop ( ) ) {
58
- // reuse from pool
59
- rc
60
- } else {
61
- // allocate
62
- Rc :: new ( [ 0. ; RENDER_QUANTUM_SIZE ] )
63
- }
41
+ fn allocate ( ) -> Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > {
42
+ if let Some ( rc) = POOL . with_borrow_mut ( |p| p. pop ( ) ) {
43
+ // reuse from pool
44
+ rc
45
+ } else {
46
+ // allocate
47
+ Rc :: new ( [ 0. ; RENDER_QUANTUM_SIZE ] )
64
48
}
49
+ }
65
50
66
- fn push ( & self , data : Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > ) {
67
- POOL . with_borrow_mut ( |p| p. push ( data) ) ;
68
- }
51
+ fn push ( data : Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > ) {
52
+ POOL . with_borrow_mut ( |p| p. push ( data) ) ;
69
53
}
70
54
71
55
/// Render thread channel buffer
@@ -84,13 +68,12 @@ impl AllocInner {
84
68
#[ derive( Clone , Debug ) ]
85
69
pub struct AudioRenderQuantumChannel {
86
70
data : Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > ,
87
- alloc : Rc < AllocInner > ,
88
71
}
89
72
90
73
impl AudioRenderQuantumChannel {
91
74
fn make_mut ( & mut self ) -> & mut [ f32 ; RENDER_QUANTUM_SIZE ] {
92
75
if Rc :: strong_count ( & self . data ) != 1 {
93
- let mut new = self . alloc . allocate ( ) ;
76
+ let mut new = allocate ( ) ;
94
77
Rc :: make_mut ( & mut new) . copy_from_slice ( self . data . deref ( ) ) ;
95
78
self . data = new;
96
79
}
@@ -117,7 +100,6 @@ impl AudioRenderQuantumChannel {
117
100
pub ( crate ) fn silence ( & self ) -> Self {
118
101
Self {
119
102
data : ZEROES . with ( Rc :: clone) ,
120
- alloc : Rc :: clone ( & self . alloc ) ,
121
103
}
122
104
}
123
105
}
@@ -149,7 +131,7 @@ impl std::ops::Drop for AudioRenderQuantumChannel {
149
131
if Rc :: strong_count ( & self . data ) == 1 {
150
132
let zeroes = ZEROES . with ( Rc :: clone) ;
151
133
let rc = std:: mem:: replace ( & mut self . data , zeroes) ;
152
- self . alloc . push ( rc) ;
134
+ push ( rc) ;
153
135
}
154
136
}
155
137
}
0 commit comments