[pulseaudio-discuss] [PATCH RFCv2 06/27] packet: Hide internals of pa_packet, introduce pa_packet_data()

Peter Meerwald pmeerw at pmeerw.net
Tue Oct 28 12:46:19 PDT 2014


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

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/pulsecore/packet.c      | 18 ++++++++++++++++++
 src/pulsecore/packet.h      | 11 +++--------
 src/pulsecore/pdispatch.c   |  9 +++++----
 src/pulsecore/pstream.c     | 15 +++++++++------
 src/tests/srbchannel-test.c | 21 +++++++++++++++------
 5 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/src/pulsecore/packet.c b/src/pulsecore/packet.c
index cee468b..450c5c5 100644
--- a/src/pulsecore/packet.c
+++ b/src/pulsecore/packet.c
@@ -27,9 +27,17 @@
 
 #include <pulse/xmalloc.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/refcnt.h>
 
 #include "packet.h"
 
+typedef struct pa_packet {
+    PA_REFCNT_DECLARE;
+    enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
+    size_t length;
+    uint8_t *data;
+} pa_packet;
+
 pa_packet* pa_packet_new(size_t length) {
     pa_packet *p;
 
@@ -59,6 +67,16 @@ pa_packet* pa_packet_new_dynamic(void* data, size_t length) {
     return p;
 }
 
+const void* pa_packet_data(pa_packet *p, size_t *l) {
+    pa_assert(PA_REFCNT_VALUE(p) >= 1);
+    pa_assert(p->data);
+    pa_assert(l);
+
+    *l = p->length;
+
+    return p->data;
+}
+
 pa_packet* pa_packet_ref(pa_packet *p) {
     pa_assert(p);
     pa_assert(PA_REFCNT_VALUE(p) >= 1);
diff --git a/src/pulsecore/packet.h b/src/pulsecore/packet.h
index 5989b1f..a6d12c7 100644
--- a/src/pulsecore/packet.h
+++ b/src/pulsecore/packet.h
@@ -25,18 +25,13 @@
 #include <sys/types.h>
 #include <inttypes.h>
 
-#include <pulsecore/refcnt.h>
-
-typedef struct pa_packet {
-    PA_REFCNT_DECLARE;
-    enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
-    size_t length;
-    uint8_t *data;
-} pa_packet;
+typedef struct pa_packet pa_packet;
 
 pa_packet* pa_packet_new(size_t length);
 pa_packet* pa_packet_new_dynamic(void* data, size_t length);
 
+const void* pa_packet_data(pa_packet *p, size_t *l);
+
 pa_packet* pa_packet_ref(pa_packet *p);
 void pa_packet_unref(pa_packet *p);
 
diff --git a/src/pulsecore/pdispatch.c b/src/pulsecore/pdispatch.c
index 949d6fe..e8c061e 100644
--- a/src/pulsecore/pdispatch.c
+++ b/src/pulsecore/pdispatch.c
@@ -295,19 +295,20 @@ int pa_pdispatch_run(pa_pdispatch *pd, pa_packet*packet, const pa_ancil *ancil,
     uint32_t tag, command;
     pa_tagstruct *ts = NULL;
     int ret = -1;
+    const void *pdata;
+    size_t plen;
 
     pa_assert(pd);
     pa_assert(PA_REFCNT_VALUE(pd) >= 1);
     pa_assert(packet);
-    pa_assert(PA_REFCNT_VALUE(packet) >= 1);
-    pa_assert(packet->data);
 
     pa_pdispatch_ref(pd);
 
-    if (packet->length <= 8)
+    pdata = pa_packet_data(packet, &plen);
+    if (plen <= 8)
         goto finish;
 
-    ts = pa_tagstruct_new_fixed(packet->data, packet->length);
+    ts = pa_tagstruct_new_fixed(pdata, plen);
 
     if (pa_tagstruct_getu32(ts, &command) < 0 ||
         pa_tagstruct_getu32(ts, &tag) < 0)
diff --git a/src/pulsecore/pstream.c b/src/pulsecore/pstream.c
index ceda728..61f8c22 100644
--- a/src/pulsecore/pstream.c
+++ b/src/pulsecore/pstream.c
@@ -493,14 +493,16 @@ static void prepare_next_write_item(pa_pstream *p) {
     p->write.descriptor[PA_PSTREAM_DESCRIPTOR_FLAGS] = 0;
 
     if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
+        size_t plen;
 
         pa_assert(p->write.current->packet);
-        p->write.data = p->write.current->packet->data;
-        p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl((uint32_t) p->write.current->packet->length);
 
-        if (p->write.current->packet->length <= MINIBUF_SIZE - PA_PSTREAM_DESCRIPTOR_SIZE) {
-            memcpy(&p->write.minibuf[PA_PSTREAM_DESCRIPTOR_SIZE], p->write.data, p->write.current->packet->length);
-            p->write.minibuf_validsize = PA_PSTREAM_DESCRIPTOR_SIZE + p->write.current->packet->length;
+        p->write.data = (void *) pa_packet_data(p->write.current->packet, &plen);
+        p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl((uint32_t) plen);
+
+        if (plen <= MINIBUF_SIZE - PA_PSTREAM_DESCRIPTOR_SIZE) {
+            memcpy(&p->write.minibuf[PA_PSTREAM_DESCRIPTOR_SIZE], p->write.data, plen);
+            p->write.minibuf_validsize = PA_PSTREAM_DESCRIPTOR_SIZE + plen;
         }
 
     } else if (p->write.current->type == PA_PSTREAM_ITEM_SHMRELEASE) {
@@ -790,6 +792,7 @@ static int do_read(pa_pstream *p, struct pstream_read *re) {
         channel = ntohl(re->descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]);
 
         if (channel == (uint32_t) -1) {
+            size_t plen;
 
             if (flags != 0) {
                 pa_log_warn("Received packet frame with invalid flags value.");
@@ -798,7 +801,7 @@ static int do_read(pa_pstream *p, struct pstream_read *re) {
 
             /* Frame is a packet frame */
             re->packet = pa_packet_new(length);
-            re->data = re->packet->data;
+            re->data = (void *) pa_packet_data(re->packet, &plen);
 
         } else {
 
diff --git a/src/tests/srbchannel-test.c b/src/tests/srbchannel-test.c
index bf7b0f0..1ae617c 100644
--- a/src/tests/srbchannel-test.c
+++ b/src/tests/srbchannel-test.c
@@ -37,26 +37,35 @@ static unsigned packets_checksum;
 static size_t packets_length;
 
 static void packet_received(pa_pstream *p, pa_packet *packet, const pa_ancil *ancil, void *userdata) {
+    const uint8_t *pdata;
+    size_t plen;
     unsigned i;
-    fail_unless(packets_length == packet->length);
+
+    pdata = pa_packet_data(packet, &plen);
+    fail_unless(packets_length == plen);
+
     packets_received++;
-    for (i = 0; i < packet->length; i++)
-        packets_checksum += packet->data[i];
+    for (i = 0; i < plen; i++)
+        packets_checksum += pdata[i];
 }
 
 static void packet_test(unsigned npackets, size_t plength, pa_mainloop *ml, pa_pstream *p1, pa_pstream *p2) {
     pa_packet *packet = pa_packet_new(plength);
     unsigned i;
     unsigned psum = 0, totalsum = 0;
+    uint8_t *pdata;
+    size_t plen;
+
     pa_log_info("Sending %d packets of length %zd", npackets, plength);
     packets_received = 0;
     packets_checksum = 0;
     packets_length = plength;
     pa_pstream_set_receive_packet_callback(p2, packet_received, NULL);
 
-    for (i = 0; i < plength; i++) {
-        packet->data[i] = i;
-        psum += packet->data[i];
+    pdata = (uint8_t *) pa_packet_data(packet, &plen);
+    for (i = 0; i < plen; i++) {
+        pdata[i] = i;
+        psum += pdata[i];
     }
 
     for (i = 0; i < npackets; i++) {
-- 
1.9.1



More information about the pulseaudio-discuss mailing list