[pulseaudio-discuss] [PATCH RFC 09/17] packet: Use flist to save calls to malloc()/free()

Peter Meerwald pmeerw at pmeerw.net
Fri Oct 24 14:21:33 PDT 2014


From: Peter Meerwald <p.meerwald at bct-electronic.com>

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 at pmeerw.net>
---
 src/pulsecore/packet.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/pulsecore/packet.c b/src/pulsecore/packet.c
index 2f8eacf..e48aeb3 100644
--- a/src/pulsecore/packet.c
+++ b/src/pulsecore/packet.c
@@ -28,6 +28,7 @@
 #include <pulse/xmalloc.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/refcnt.h>
+#include <pulsecore/flist.h>
 
 #include "packet.h"
 
@@ -43,12 +44,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);
     }
 }
-- 
1.9.1



More information about the pulseaudio-discuss mailing list