summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Meerwald <p.meerwald@bct-electronic.com>2014-10-23 14:19:44 +0200
committerPeter Meerwald <pmeerw@pmeerw.net>2015-02-26 23:23:17 +0100
commit6a55df78aaff7b29d352d2e89fa3273ab5524d93 (patch)
tree9699f43e9dacc1e9db2dd3b9a49167495e2ddd63
parentf92300cc92e5e51b9f42e75d7afc8b330d676528 (diff)
packet: Use flist to save calls to malloc()/free()
a separate free-list is used to recycle memory of fixed-sized packets with up to MAX_APPENDED_SIZE of data Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
-rw-r--r--src/pulsecore/packet.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/pulsecore/packet.c b/src/pulsecore/packet.c
index 9dc886817..e275d23c7 100644
--- a/src/pulsecore/packet.c
+++ b/src/pulsecore/packet.c
@@ -26,6 +26,7 @@
#include <pulse/xmalloc.h>
#include <pulsecore/macro.h>
#include <pulsecore/refcnt.h>
+#include <pulsecore/flist.h>
#include "packet.h"
@@ -41,12 +42,15 @@ typedef struct pa_packet {
} per_type;
} pa_packet;
+PA_STATIC_FLIST_DECLARE(packets, 0, pa_xfree);
+
pa_packet* pa_packet_new(size_t length) {
pa_packet *p;
pa_assert(length > 0);
- p = pa_xnew(pa_packet, 1);
+ if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(packets))))
+ p = pa_xnew(pa_packet, 1);
PA_REFCNT_INIT(p);
p->length = length;
if (length > MAX_APPENDED_SIZE) {
@@ -77,7 +81,8 @@ pa_packet* pa_packet_new_dynamic(void* data, size_t length) {
pa_assert(data);
pa_assert(length > 0);
- p = pa_xnew(pa_packet, 1);
+ if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(packets))))
+ p = pa_xnew(pa_packet, 1);
PA_REFCNT_INIT(p);
p->length = length;
p->data = data;
@@ -111,6 +116,7 @@ void pa_packet_unref(pa_packet *p) {
if (PA_REFCNT_DEC(p) <= 0) {
if (p->type == PA_PACKET_DYNAMIC)
pa_xfree(p->data);
- pa_xfree(p);
+ if (pa_flist_push(PA_STATIC_FLIST_GET(packets), p) < 0)
+ pa_xfree(p);
}
}