Skip to content

Commit

Permalink
aubuf: use new mem_pool to avoid malloc/free
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Jan 20, 2025
1 parent d248c6f commit 07b4f05
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions rem/aubuf/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

#define AUBUF_DEBUG 0

enum { POOL_FRAMES = 25 };

/** Locked audio-buffer with almost zero-copy */
struct aubuf {
struct list afl;
struct mem_pool *pool;
struct pl *id; /**< Audio buffer Identifier */
mtx_t *lock;
size_t wish_sz;
Expand Down Expand Up @@ -49,13 +51,13 @@ struct frame {
struct le le;
struct mbuf *mb;
struct auframe af;
struct mem_pool_entry *e;
};


static void frame_destructor(void *arg)
static void frame_destructor(void *data)
{
struct frame *f = arg;

struct frame *f = data;
list_unlink(&f->le);
mem_deref(f->mb);
}
Expand All @@ -65,10 +67,10 @@ static void aubuf_destructor(void *arg)
{
struct aubuf *ab = arg;

list_flush(&ab->afl);
mem_deref(ab->lock);
mem_deref(ab->ajb);
mem_deref(ab->id);
mem_deref(ab->pool);
}


Expand Down Expand Up @@ -100,7 +102,7 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)
}

if (!mbuf_get_left(f->mb)) {
mem_deref(f);
mem_pool_release(ab->pool, f->e);
}
else if (af->srate && af->ch && sample_size) {

Expand Down Expand Up @@ -139,6 +141,11 @@ int aubuf_alloc(struct aubuf **abp, size_t min_sz, size_t max_sz)
if (!ab)
return ENOMEM;

err = mem_pool_alloc(&ab->pool, POOL_FRAMES, sizeof(struct frame),
frame_destructor);
if (err)
goto out;

err = mutex_alloc(&ab->lock);
if (err)
goto out;
Expand Down Expand Up @@ -269,10 +276,13 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,
if (!ab || !mb)
return EINVAL;

f = mem_zalloc(sizeof(*f), frame_destructor);
if (!f)
struct mem_pool_entry *e = mem_pool_borrow_extend(ab->pool);
if (!e)
return ENOMEM;

f = mem_pool_member(e);
f->e = e;

f->mb = mem_ref(mb);
if (af)
f->af = *af;
Expand All @@ -299,7 +309,7 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,
f = list_ledata(ab->afl.head);
if (f) {
ab->cur_sz -= mbuf_get_left(f->mb);
mem_deref(f);
mem_pool_release(ab->pool, f->e);
}
}

Expand Down Expand Up @@ -415,7 +425,7 @@ void aubuf_read_auframe(struct aubuf *ab, struct auframe *af)
struct frame *f = list_ledata(ab->afl.head);
if (f) {
ab->cur_sz -= mbuf_get_left(f->mb);
mem_deref(f);
mem_pool_release(ab->pool, f->e);
}
}

Expand Down Expand Up @@ -499,7 +509,8 @@ void aubuf_flush(struct aubuf *ab)

mtx_lock(ab->lock);

list_flush(&ab->afl);
list_clear(&ab->afl);
mem_pool_flush(ab->pool);
ab->fill_sz = ab->wish_sz;
ab->cur_sz = 0;
ab->wr_sz = 0;
Expand Down

0 comments on commit 07b4f05

Please sign in to comment.