18
18
19
19
#define AUBUF_DEBUG 0
20
20
21
+ enum { POOL_FRAMES = 25 };
21
22
22
23
/** Locked audio-buffer with almost zero-copy */
23
24
struct aubuf {
24
25
struct list afl ;
26
+ struct mem_pool * pool ;
25
27
struct pl * id ; /**< Audio buffer Identifier */
26
28
mtx_t * lock ;
27
29
size_t wish_sz ;
@@ -49,13 +51,13 @@ struct frame {
49
51
struct le le ;
50
52
struct mbuf * mb ;
51
53
struct auframe af ;
54
+ struct mem_pool_entry * e ;
52
55
};
53
56
54
57
55
- static void frame_destructor (void * arg )
58
+ static void frame_destructor (void * data )
56
59
{
57
- struct frame * f = arg ;
58
-
60
+ struct frame * f = data ;
59
61
list_unlink (& f -> le );
60
62
mem_deref (f -> mb );
61
63
}
@@ -65,10 +67,10 @@ static void aubuf_destructor(void *arg)
65
67
{
66
68
struct aubuf * ab = arg ;
67
69
68
- list_flush (& ab -> afl );
69
70
mem_deref (ab -> lock );
70
71
mem_deref (ab -> ajb );
71
72
mem_deref (ab -> id );
73
+ mem_deref (ab -> pool );
72
74
}
73
75
74
76
@@ -100,7 +102,7 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)
100
102
}
101
103
102
104
if (!mbuf_get_left (f -> mb )) {
103
- mem_deref ( f );
105
+ mem_pool_release ( ab -> pool , f -> e );
104
106
}
105
107
else if (af -> srate && af -> ch && sample_size ) {
106
108
@@ -139,6 +141,11 @@ int aubuf_alloc(struct aubuf **abp, size_t min_sz, size_t max_sz)
139
141
if (!ab )
140
142
return ENOMEM ;
141
143
144
+ err = mem_pool_alloc (& ab -> pool , POOL_FRAMES , sizeof (struct frame ),
145
+ frame_destructor );
146
+ if (err )
147
+ goto out ;
148
+
142
149
err = mutex_alloc (& ab -> lock );
143
150
if (err )
144
151
goto out ;
@@ -269,10 +276,13 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,
269
276
if (!ab || !mb )
270
277
return EINVAL ;
271
278
272
- f = mem_zalloc ( sizeof ( * f ), frame_destructor );
273
- if (!f )
279
+ struct mem_pool_entry * e = mem_pool_borrow_extend ( ab -> pool );
280
+ if (!e )
274
281
return ENOMEM ;
275
282
283
+ f = mem_pool_member (e );
284
+ f -> e = e ;
285
+
276
286
f -> mb = mem_ref (mb );
277
287
if (af )
278
288
f -> af = * af ;
@@ -299,7 +309,7 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,
299
309
f = list_ledata (ab -> afl .head );
300
310
if (f ) {
301
311
ab -> cur_sz -= mbuf_get_left (f -> mb );
302
- mem_deref ( f );
312
+ mem_pool_release ( ab -> pool , f -> e );
303
313
}
304
314
}
305
315
@@ -415,7 +425,7 @@ void aubuf_read_auframe(struct aubuf *ab, struct auframe *af)
415
425
struct frame * f = list_ledata (ab -> afl .head );
416
426
if (f ) {
417
427
ab -> cur_sz -= mbuf_get_left (f -> mb );
418
- mem_deref ( f );
428
+ mem_pool_release ( ab -> pool , f -> e );
419
429
}
420
430
}
421
431
@@ -499,7 +509,8 @@ void aubuf_flush(struct aubuf *ab)
499
509
500
510
mtx_lock (ab -> lock );
501
511
502
- list_flush (& ab -> afl );
512
+ list_clear (& ab -> afl );
513
+ mem_pool_flush (ab -> pool );
503
514
ab -> fill_sz = ab -> wish_sz ;
504
515
ab -> cur_sz = 0 ;
505
516
ab -> wr_sz = 0 ;
0 commit comments