From 6a55df78aaff7b29d352d2e89fa3273ab5524d93 Mon Sep 17 00:00:00 2001
From: Peter Meerwald 
Date: Thu, 23 Oct 2014 14:19:44 +0200
Subject: [PATCH] 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 
---
 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 9dc886817..e275d23c7 100644
--- a/src/pulsecore/packet.c
+++ b/src/pulsecore/packet.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #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);
     }
 }