summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Meerwald <p.meerwald@bct-electronic.com>2014-10-23 13:48:21 +0200
committerPeter Meerwald <pmeerw@pmeerw.net>2015-02-26 23:23:17 +0100
commitc1a7f0e3264bc399d289cbeb8556f193db3ab67b (patch)
treec8c4a11a1a132a7d7d86eea7c712674326f76758
parent5a2c41e5bf13d16f01dce1e73de956ea9456b447 (diff)
packet: Make pa_packet_new() create fixed-size packets
if length exceeds maximum appended size, create a packet of type dynamic instead of type appended this is a preparation to use a separate free-list for packets document semantics of pa_packet_new_*() functions Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
-rw-r--r--src/pulsecore/packet.c16
-rw-r--r--src/pulsecore/packet.h5
2 files changed, 18 insertions, 3 deletions
diff --git a/src/pulsecore/packet.c b/src/pulsecore/packet.c
index a819ee3d8..eacff9dd6 100644
--- a/src/pulsecore/packet.c
+++ b/src/pulsecore/packet.c
@@ -29,11 +29,16 @@
#include "packet.h"
+#define MAX_APPENDED_SIZE 128
+
typedef struct pa_packet {
PA_REFCNT_DECLARE;
enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
size_t length;
uint8_t *data;
+ union {
+ uint8_t appended[MAX_APPENDED_SIZE];
+ } per_type;
} pa_packet;
pa_packet* pa_packet_new(size_t length) {
@@ -41,11 +46,16 @@ pa_packet* pa_packet_new(size_t length) {
pa_assert(length > 0);
- p = pa_xmalloc(PA_ALIGN(sizeof(pa_packet)) + length);
+ p = pa_xnew(pa_packet, 1);
PA_REFCNT_INIT(p);
p->length = length;
- p->data = (uint8_t*) p + PA_ALIGN(sizeof(pa_packet));
- p->type = PA_PACKET_APPENDED;
+ if (length > MAX_APPENDED_SIZE) {
+ p->data = pa_xmalloc(length);
+ p->type = PA_PACKET_DYNAMIC;
+ } else {
+ p->data = p->per_type.appended;
+ p->type = PA_PACKET_APPENDED;
+ }
return p;
}
diff --git a/src/pulsecore/packet.h b/src/pulsecore/packet.h
index 83fa0b863..6d61e02e4 100644
--- a/src/pulsecore/packet.h
+++ b/src/pulsecore/packet.h
@@ -25,7 +25,12 @@
typedef struct pa_packet pa_packet;
+/* create empty packet (either of type appended or dynamic depending
+ * on length) */
pa_packet* pa_packet_new(size_t length);
+
+/* data must have been malloc()ed; the packet takes ownership of the memory,
+ * i.e. memory is free()d with the packet */
pa_packet* pa_packet_new_dynamic(void* data, size_t length);
const void* pa_packet_data(pa_packet *p, size_t *l);