aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-08-29 20:50:36 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-08-29 20:50:36 +0000
commit53f8d43e2945038fe3059fd6c1c00dd4ec117505 (patch)
tree0f3fc04bcb07ac328d54ffb2fed0333dfffb44de /main
parent562decf81af443d24afd3f1fc3d133b8b2b7cee3 (diff)
Merge team/russell/frame_caching
There are some situations in Asterisk where ast_frame and/or iax_frame structures are rapidly allocatted and freed (at least 50 times per second for one call). This code significantly improves the performance of ast_frame_header_new(), ast_frdup(), ast_frfree(), iax_frame_new(), and iax_frame_free() by keeping a thread-local cache of these structures and using frames from the cache whenever possible instead of calling malloc/free every time. This commit also converts the ast_frame and iax_frame structures to use the linked list macros. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@41278 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/abstract_jb.c5
-rw-r--r--main/channel.c157
-rw-r--r--main/frame.c172
-rw-r--r--main/slinfactory.c17
-rw-r--r--main/udptl.c35
5 files changed, 194 insertions, 192 deletions
diff --git a/main/abstract_jb.c b/main/abstract_jb.c
index 1525d81b0..6f5fa4ce6 100644
--- a/main/abstract_jb.c
+++ b/main/abstract_jb.c
@@ -324,10 +324,7 @@ int ast_jb_put(struct ast_channel *chan, struct ast_frame *f)
return -1;
}
- if (f->mallocd & AST_MALLOCD_HDR)
- frr = ast_frdup(f);
- else
- frr = ast_frisolate(f);
+ frr = ast_frdup(f);
if (!frr) {
ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", chan->name);
diff --git a/main/channel.c b/main/channel.c
index f2cc5fa14..84e54b806 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -706,7 +706,7 @@ struct ast_channel *ast_channel_alloc(int needqueue)
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
{
struct ast_frame *f;
- struct ast_frame *prev, *cur;
+ struct ast_frame *cur;
int blah = 1;
int qlen = 0;
@@ -716,17 +716,19 @@ int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
return -1;
}
ast_channel_lock(chan);
- prev = NULL;
- for (cur = chan->readq; cur; cur = cur->next) {
- if ((cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
- /* Don't bother actually queueing anything after a hangup */
- ast_frfree(f);
- ast_channel_unlock(chan);
- return 0;
- }
- prev = cur;
+
+ /* See if the last frame on the queue is a hangup, if so don't queue anything */
+ if ((cur = AST_LIST_LAST(&chan->readq)) && (cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
+ ast_frfree(f);
+ ast_channel_unlock(chan);
+ return 0;
+ }
+
+ /* Count how many frames exist on the queue */
+ AST_LIST_TRAVERSE(&chan->readq, cur, frame_list) {
qlen++;
}
+
/* Allow up to 96 voice frames outstanding, and up to 128 total frames */
if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
if (fin->frametype != AST_FRAME_VOICE) {
@@ -739,10 +741,7 @@ int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
return 0;
}
}
- if (prev)
- prev->next = f;
- else
- chan->readq = f;
+ AST_LIST_INSERT_TAIL(&chan->readq, f, frame_list);
if (chan->alertpipe[1] > -1) {
if (write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
@@ -973,7 +972,7 @@ void ast_channel_free(struct ast_channel *chan)
{
int fd;
struct ast_var_t *vardata;
- struct ast_frame *f, *fp;
+ struct ast_frame *f;
struct varshead *headp;
struct ast_datastore *datastore = NULL;
char name[AST_CHANNEL_NAME];
@@ -1024,13 +1023,8 @@ void ast_channel_free(struct ast_channel *chan)
close(fd);
if ((fd = chan->timingfd) > -1)
close(fd);
- f = chan->readq;
- chan->readq = NULL;
- while(f) {
- fp = f;
- f = f->next;
- ast_frfree(fp);
- }
+ while ((f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list)))
+ ast_frfree(f);
/* Get rid of each of the data stores on the channel */
while ((datastore = AST_LIST_REMOVE_HEAD(&chan->datastores, entry)))
@@ -1247,14 +1241,11 @@ void ast_channel_spy_remove(struct ast_channel *chan, struct ast_channel_spy *sp
spy->chan = NULL;
- for (f = spy->read_queue.head; f; f = spy->read_queue.head) {
- spy->read_queue.head = f->next;
+ while ((f = AST_LIST_REMOVE_HEAD(&spy->read_queue.list, frame_list)))
ast_frfree(f);
- }
- for (f = spy->write_queue.head; f; f = spy->write_queue.head) {
- spy->write_queue.head = f->next;
+
+ while ((f = AST_LIST_REMOVE_HEAD(&spy->write_queue.list, frame_list)))
ast_frfree(f);
- }
if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
ast_cond_destroy(&spy->trigger);
@@ -1337,8 +1328,6 @@ static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f,
trans = (dir == SPY_READ) ? &chan->spies->read_translator : &chan->spies->write_translator;
AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
- struct ast_frame *last;
- struct ast_frame *f1; /* the frame to append */
struct ast_channel_spy_queue *queue;
ast_mutex_lock(&spy->lock);
@@ -1370,7 +1359,7 @@ static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f,
break;
}
}
- f1 = translated_frame;
+ AST_LIST_INSERT_TAIL(&queue->list, ast_frdup(f), frame_list);
} else {
if (f->subclass != queue->format) {
ast_log(LOG_WARNING, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n",
@@ -1379,17 +1368,8 @@ static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f,
ast_mutex_unlock(&spy->lock);
continue;
}
- f1 = f;
+ AST_LIST_INSERT_TAIL(&queue->list, ast_frdup(f), frame_list);
}
- /* duplicate and append f1 to the tail */
- f1 = ast_frdup(f1);
-
- for (last = queue->head; last && last->next; last = last->next)
- ;
- if (last)
- last->next = f1;
- else
- queue->head = f1;
queue->samples += f->samples;
@@ -1425,10 +1405,8 @@ static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f,
ast_log(LOG_DEBUG, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n",
spy->type, chan->name, (dir == SPY_READ) ? "read" : "write");
while (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
- struct ast_frame *drop = queue->head;
-
+ struct ast_frame *drop = AST_LIST_REMOVE_HEAD(&queue->list, frame_list);
queue->samples -= drop->samples;
- queue->head = drop->next;
ast_frfree(drop);
}
}
@@ -1946,7 +1924,7 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
blah = ZT_EVENT_TIMER_EXPIRED;
if (blah == ZT_EVENT_TIMER_PING) {
- if (!chan->readq || !chan->readq->next) {
+ if (AST_LIST_EMPTY(&chan->readq) || !AST_LIST_NEXT(AST_LIST_FIRST(&chan->readq), frame_list)) {
/* Acknowledge PONG unless we need it again */
if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) {
ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno));
@@ -1985,10 +1963,8 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
}
/* Check for pending read queue */
- if (chan->readq) {
- f = chan->readq;
- chan->readq = f->next;
- f->next = NULL;
+ if (!AST_LIST_EMPTY(&chan->readq)) {
+ f = AST_LIST_REMOVE_HEAD(&chan->readq, frame_list);
/* Interpret hangup and return NULL */
/* XXX why not the same for frames from the channel ? */
if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP) {
@@ -2014,11 +1990,13 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
if (f) {
/* if the channel driver returned more than one frame, stuff the excess
- into the readq for the next ast_read call
+ into the readq for the next ast_read call (note that we can safely assume
+ that the readq is empty, because otherwise we would not have called into
+ the channel driver and f would be only a single frame)
*/
- if (f->next) {
- chan->readq = f->next;
- f->next = NULL;
+ if (AST_LIST_NEXT(f, frame_list)) {
+ AST_LIST_HEAD_SET_NOLOCK(&chan->readq, AST_LIST_NEXT(f, frame_list));
+ AST_LIST_NEXT(f, frame_list) = NULL;
}
switch (f->frametype) {
@@ -3068,7 +3046,7 @@ int ast_do_masquerade(struct ast_channel *original)
int x,i;
int res=0;
int origstate;
- struct ast_frame *cur, *prev;
+ struct ast_frame *cur;
const struct ast_channel_tech *t;
void *t_pvt;
struct ast_callerid tmpcid;
@@ -3132,9 +3110,9 @@ int ast_do_masquerade(struct ast_channel *original)
clone->tech_pvt = t_pvt;
/* Swap the readq's */
- cur = original->readq;
- original->readq = clone->readq;
- clone->readq = cur;
+ cur = AST_LIST_FIRST(&original->readq);
+ AST_LIST_HEAD_SET_NOLOCK(&original->readq, AST_LIST_FIRST(&clone->readq));
+ AST_LIST_HEAD_SET_NOLOCK(&clone->readq, cur);
/* Swap the alertpipes */
for (i = 0; i < 2; i++) {
@@ -3153,23 +3131,22 @@ int ast_do_masquerade(struct ast_channel *original)
/* Save any pending frames on both sides. Start by counting
* how many we're going to need... */
- prev = NULL;
x = 0;
- for (cur = clone->readq; cur; cur = cur->next) {
- x++;
- prev = cur;
+ if (original->alertpipe[1] > -1) {
+ AST_LIST_TRAVERSE(&clone->readq, cur, frame_list)
+ x++;
}
- /* If we had any, prepend them to the ones already in the queue, and
+
+ /* If we had any, prepend them to the ones already in the queue, and
* load up the alertpipe */
- if (prev) {
- prev->next = original->readq;
- original->readq = clone->readq;
- clone->readq = NULL;
- if (original->alertpipe[1] > -1) {
- for (i = 0; i < x; i++)
- write(original->alertpipe[1], &x, sizeof(x));
- }
+ if (AST_LIST_FIRST(&clone->readq)) {
+ AST_LIST_INSERT_TAIL(&clone->readq, AST_LIST_FIRST(&original->readq), frame_list);
+ AST_LIST_HEAD_SET_NOLOCK(&original->readq, AST_LIST_FIRST(&clone->readq));
+ AST_LIST_HEAD_SET_NOLOCK(&clone->readq, NULL);
+ for (i = 0; i < x; i++)
+ write(original->alertpipe[1], &x, sizeof(x));
}
+
clone->_softhangup = AST_SOFTHANGUP_DEV;
@@ -4086,9 +4063,7 @@ static void copy_data_from_queue(struct ast_channel_spy_queue *queue, short *buf
int bytestocopy;
while (samples) {
- f = queue->head;
-
- if (!f) {
+ if (!(f = AST_LIST_FIRST(&queue->list))) {
ast_log(LOG_ERROR, "Ran out of frames before buffer filled!\n");
break;
}
@@ -4103,10 +4078,9 @@ static void copy_data_from_queue(struct ast_channel_spy_queue *queue, short *buf
f->datalen -= bytestocopy;
f->offset += bytestocopy;
queue->samples -= tocopy;
- if (!f->samples) {
- queue->head = f->next;
- ast_frfree(f);
- }
+
+ if (!f->samples)
+ ast_frfree(AST_LIST_REMOVE_HEAD(&queue->list, frame_list));
}
}
@@ -4136,19 +4110,19 @@ struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsign
if (ast_test_flag(spy, CHANSPY_TRIGGER_FLUSH)) {
if (spy->read_queue.samples > spy->write_queue.samples) {
if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST)) {
- for (result = spy->read_queue.head; result; result = result->next)
+ AST_LIST_TRAVERSE(&spy->read_queue.list, result, frame_list)
ast_frame_adjust_volume(result, spy->read_vol_adjustment);
}
- result = spy->read_queue.head;
- spy->read_queue.head = NULL;
+ result = AST_LIST_FIRST(&spy->read_queue.list);
+ AST_LIST_HEAD_SET_NOLOCK(&spy->read_queue.list, NULL);
spy->read_queue.samples = 0;
} else {
if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST)) {
- for (result = spy->write_queue.head; result; result = result->next)
+ AST_LIST_TRAVERSE(&spy->write_queue.list, result, frame_list)
ast_frame_adjust_volume(result, spy->write_vol_adjustment);
}
- result = spy->write_queue.head;
- spy->write_queue.head = NULL;
+ result = AST_LIST_FIRST(&spy->write_queue.list);
+ AST_LIST_HEAD_SET_NOLOCK(&spy->write_queue.list, NULL);
spy->write_queue.samples = 0;
}
ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
@@ -4159,15 +4133,10 @@ struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsign
return NULL;
/* short-circuit if both head frames have exactly what we want */
- if ((spy->read_queue.head->samples == samples) &&
- (spy->write_queue.head->samples == samples)) {
- read_frame = spy->read_queue.head;
- spy->read_queue.head = read_frame->next;
- read_frame->next = NULL;
-
- write_frame = spy->write_queue.head;
- spy->write_queue.head = write_frame->next;
- write_frame->next = NULL;
+ if ((AST_LIST_FIRST(&spy->read_queue.list)->samples == samples) &&
+ (AST_LIST_FIRST(&spy->write_queue.list)->samples == samples)) {
+ read_frame = AST_LIST_REMOVE_HEAD(&spy->read_queue.list, frame_list);
+ write_frame = AST_LIST_REMOVE_HEAD(&spy->write_queue.list, frame_list);
spy->read_queue.samples -= samples;
spy->write_queue.samples -= samples;
@@ -4200,10 +4169,10 @@ struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsign
} else {
if (need_dup) {
result = ast_frdup(read_frame);
- result->next = ast_frdup(write_frame);
+ AST_LIST_NEXT(result, frame_list) = ast_frdup(write_frame);
} else {
result = read_frame;
- result->next = write_frame;
+ AST_LIST_NEXT(result, frame_list) = write_frame;
}
}
diff --git a/main/frame.c b/main/frame.c
index 0cc44a7eb..5aab5b5c0 100644
--- a/main/frame.c
+++ b/main/frame.c
@@ -41,13 +41,39 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/cli.h"
#include "asterisk/term.h"
#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/linkedlists.h"
#ifdef TRACE_FRAMES
static int headers = 0;
-static struct ast_frame *headerlist = NULL;
-AST_MUTEX_DEFINE_STATIC(framelock);
+static AST_LIST_HEAD_STATIC(headerlist, ast_frame);
#endif
+static void frame_cache_cleanup(void *data);
+
+/*! \brief A per-thread cache of frame headers */
+AST_THREADSTORAGE_CUSTOM(frame_cache, frame_cache_init, frame_cache_cleanup);
+
+/*!
+ * \brief Maximum ast_frame cache size
+ *
+ * In most cases where the frame header cache will be useful, the size
+ * of the cache will stay very small. However, it is not always the case that
+ * the same thread that allocates the frame will be the one freeing them, so
+ * sometimes a thread will never have any frames in its cache, or the cache
+ * will never be pulled from. For the latter case, we limit the maximum size.
+ */
+#define FRAME_CACHE_MAX_SIZE 10
+
+/*! \brief This is just so ast_frames, a list head struct for holding a list of
+ * ast_frame structures, is defined. */
+AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
+
+struct ast_frame_cache {
+ struct ast_frames list;
+ size_t size;
+};
+
#define SMOOTHER_SIZE 8000
enum frame_type {
@@ -252,28 +278,63 @@ void ast_smoother_free(struct ast_smoother *s)
static struct ast_frame *ast_frame_header_new(void)
{
- struct ast_frame *f = ast_calloc(1, sizeof(*f));
-#ifdef TRACE_FRAMES
- if (f) {
- f->prev = NULL;
- ast_mutex_lock(&framelock);
- headers++;
- f->next = headerlist;
- if (headerlist)
- headerlist->prev = f;
- headerlist = f;
- ast_mutex_unlock(&framelock);
+ struct ast_frame *f;
+ struct ast_frame_cache *frames;
+
+ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
+ if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
+ size_t mallocd_len = f->mallocd_hdr_len;
+ memset(f, 0, sizeof(*f));
+ f->mallocd_hdr_len = mallocd_len;
+ f->mallocd = AST_MALLOCD_HDR;
+ frames->size--;
+ return f;
+ }
}
+
+ if (!(f = ast_calloc(1, sizeof(*f))))
+ return NULL;
+
+ f->mallocd_hdr_len = sizeof(*f);
+#ifdef TRACE_FRAMES
+ AST_LIST_LOCK(&headerlist);
+ headers++;
+ AST_LIST_INSERT_HEAD(&headerlist, f, frame_list);
+ AST_LIST_UNLOCK(&headerlist);
#endif
+
return f;
}
-/*!
- * \todo Important: I should be made more efficient. Frame headers should
- * most definitely be cached
- */
+static void frame_cache_cleanup(void *data)
+{
+ struct ast_frame_cache *frames = data;
+ struct ast_frame *f;
+
+ while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
+ free(f);
+
+ free(frames);
+}
+
void ast_frfree(struct ast_frame *fr)
{
+ if (!fr->mallocd)
+ return;
+
+ if (fr->mallocd == AST_MALLOCD_HDR) {
+ /* Cool, only the header is malloc'd, let's just cache those for now
+ * to keep things simple... */
+ struct ast_frame_cache *frames;
+
+ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))
+ && frames->size < FRAME_CACHE_MAX_SIZE) {
+ AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
+ frames->size++;
+ return;
+ }
+ }
+
if (fr->mallocd & AST_MALLOCD_DATA) {
if (fr->data)
free(fr->data - fr->offset);
@@ -284,15 +345,10 @@ void ast_frfree(struct ast_frame *fr)
}
if (fr->mallocd & AST_MALLOCD_HDR) {
#ifdef TRACE_FRAMES
- ast_mutex_lock(&framelock);
+ AST_LIST_LOCK(&headerlist);
headers--;
- if (fr->next)
- fr->next->prev = fr->prev;
- if (fr->prev)
- fr->prev->next = fr->next;
- else
- headerlist = fr->next;
- ast_mutex_unlock(&framelock);
+ AST_LIST_REMOVE(&headerlist, fr, frame_list);
+ AST_LIST_UNLOCK(&headerlist);
#endif
free(fr);
}
@@ -361,9 +417,11 @@ struct ast_frame *ast_frisolate(struct ast_frame *fr)
struct ast_frame *ast_frdup(const struct ast_frame *f)
{
+ struct ast_frame_cache *frames;
struct ast_frame *out;
int len, srclen = 0;
- void *buf;
+ void *buf = NULL;
+
/* Start with standard stuff */
len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
/* If we have a source, add space for it */
@@ -375,16 +433,35 @@ struct ast_frame *ast_frdup(const struct ast_frame *f)
srclen = strlen(f->src);
if (srclen > 0)
len += srclen + 1;
- if (!(buf = ast_calloc(1, len)))
- return NULL;
- out = buf;
- /* Set us as having malloc'd header only, so it will eventually
- get freed. */
+
+ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
+ if (out->mallocd_hdr_len >= len) {
+ size_t mallocd_len = out->mallocd_hdr_len;
+ AST_LIST_REMOVE_CURRENT(&frames->list, frame_list);
+ memset(out, 0, sizeof(*out));
+ out->mallocd_hdr_len = mallocd_len;
+ buf = out;
+ frames->size--;
+ break;
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+ }
+ if (!buf) {
+ if (!(buf = ast_calloc(1, len)))
+ return NULL;
+ out = buf;
+ out->mallocd_hdr_len = len;
+ }
+
out->frametype = f->frametype;
out->subclass = f->subclass;
out->datalen = f->datalen;
out->samples = f->samples;
out->delivery = f->delivery;
+ /* Set us as having malloc'd header only, so it will eventually
+ get freed. */
out->mallocd = AST_MALLOCD_HDR;
out->offset = AST_FRIENDLY_OFFSET;
if (out->datalen) {
@@ -860,15 +937,14 @@ static int show_frame_stats(int fd, int argc, char *argv[])
int x=1;
if (argc != 3)
return RESULT_SHOWUSAGE;
- ast_mutex_lock(&framelock);
+ AST_LIST_LOCK(&headerlist);
ast_cli(fd, " Framer Statistics \n");
ast_cli(fd, "---------------------------\n");
ast_cli(fd, "Total allocated headers: %d\n", headers);
ast_cli(fd, "Queue Dump:\n");
- for (f=headerlist; f; f = f->next) {
+ AST_LIST_TRAVERSE(&headerlist, f, frame_list)
ast_cli(fd, "%d. Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
- }
- ast_mutex_unlock(&framelock);
+ AST_LIST_UNLOCK(&headerlist);
return RESULT_SUCCESS;
}
@@ -889,6 +965,7 @@ static struct ast_cli_entry my_clis[] = {
#endif
};
+
int init_framer(void)
{
ast_cli_register_multiple(my_clis, sizeof(my_clis)/sizeof(my_clis[0]) );
@@ -1342,28 +1419,3 @@ int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
return 0;
}
-
-struct ast_frame *ast_frame_enqueue(struct ast_frame *head, struct ast_frame *f, int maxlen, int dupe)
-{
- struct ast_frame *cur, *oldhead;
- int len=0;
- if (f && dupe)
- f = ast_frdup(f);
- if (!f)
- return head;
-
- f->next = NULL;
- if (!head)
- return f;
- cur = head;
- while(cur->next) {
- cur = cur->next;
- len++;
- if (len >= maxlen) {
- oldhead = head;
- head = head->next;
- ast_frfree(oldhead);
- }
- }
- return head;
-}
diff --git a/main/slinfactory.c b/main/slinfactory.c
index 62a863d87..0520dbcef 100644
--- a/main/slinfactory.c
+++ b/main/slinfactory.c
@@ -39,7 +39,6 @@ void ast_slinfactory_init(struct ast_slinfactory *sf)
{
memset(sf, 0, sizeof(*sf));
sf->offset = sf->hold;
- sf->queue = NULL;
}
void ast_slinfactory_destroy(struct ast_slinfactory *sf)
@@ -51,10 +50,8 @@ void ast_slinfactory_destroy(struct ast_slinfactory *sf)
sf->trans = NULL;
}
- while ((f = sf->queue)) {
- sf->queue = f->next;
+ while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
ast_frfree(f);
- }
}
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
@@ -85,15 +82,12 @@ int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
if (!frame)
return 0;
- for (x = 0, frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr = frame_ptr->next)
+ x = 0;
+ AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
x++;
- if (frame_ptr)
- frame_ptr->next = frame;
- else
- sf->queue = frame;
+ AST_LIST_INSERT_TAIL(&sf->queue, frame, frame_list);
- frame->next = NULL;
sf->size += frame->samples;
return x;
@@ -125,8 +119,7 @@ int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
continue;
}
- if ((frame_ptr = sf->queue)) {
- sf->queue = frame_ptr->next;
+ if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
frame_data = frame_ptr->data;
if ((sofar + frame_ptr->samples) <= ineed) {
diff --git a/main/udptl.c b/main/udptl.c
index 9e8fdf147..1264c23fc 100644
--- a/main/udptl.c
+++ b/main/udptl.c
@@ -296,8 +296,7 @@ static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
ptr = 0;
ifp_no = 0;
- s->f[0].prev = NULL;
- s->f[0].next = NULL;
+ memset(&s->f[0], 0, sizeof(s->f[0]));
/* Decode seq_number */
if (ptr + 2 > len)
@@ -342,11 +341,9 @@ static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
s->f[ifp_no].offset = 0;
s->f[ifp_no].src = "UDPTL";
- if (ifp_no > 0) {
- s->f[ifp_no].prev = &s->f[ifp_no - 1];
- s->f[ifp_no - 1].next = &s->f[ifp_no];
- }
- s->f[ifp_no].next = NULL;
+ if (ifp_no > 0)
+ AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
+ AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
ifp_no++;
}
}
@@ -364,11 +361,9 @@ static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
s->f[ifp_no].data = (uint8_t *) ifp;
s->f[ifp_no].offset = 0;
s->f[ifp_no].src = "UDPTL";
- if (ifp_no > 0) {
- s->f[ifp_no].prev = &s->f[ifp_no - 1];
- s->f[ifp_no - 1].next = &s->f[ifp_no];
- }
- s->f[ifp_no].next = NULL;
+ if (ifp_no > 0)
+ AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
+ AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
}
}
else
@@ -465,11 +460,9 @@ static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
s->f[ifp_no].data = s->rx[l].buf;
s->f[ifp_no].offset = 0;
s->f[ifp_no].src = "UDPTL";
- if (ifp_no > 0) {
- s->f[ifp_no].prev = &s->f[ifp_no - 1];
- s->f[ifp_no - 1].next = &s->f[ifp_no];
- }
- s->f[ifp_no].next = NULL;
+ if (ifp_no > 0)
+ AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
+ AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
ifp_no++;
}
}
@@ -483,11 +476,9 @@ static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
s->f[ifp_no].data = (uint8_t *) ifp;
s->f[ifp_no].offset = 0;
s->f[ifp_no].src = "UDPTL";
- if (ifp_no > 0) {
- s->f[ifp_no].prev = &s->f[ifp_no - 1];
- s->f[ifp_no - 1].next = &s->f[ifp_no];
- }
- s->f[ifp_no].next = NULL;
+ if (ifp_no > 0)
+ AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
+ AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
}
s->rx_seq_no = seq_no + 1;