[pulseaudio-discuss] [PATCH RFCv3 04/51] tagstruct: Add type _APPENDED

Peter Meerwald pmeerw at pmeerw.net
Tue Nov 4 15:25:59 PST 2014


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

add 128 bytes of storage in each tagstruct that will initially
be used; if this storage is exceeded the type changes to _DYNAMIC

v2: (thanks Alexander Patrakov)
* replace constant 100 with GROW_TAG_SIZE (the increment in with a dynamic tagstruct grows when extend()ed)

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/pulsecore/tagstruct.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/pulsecore/tagstruct.c b/src/pulsecore/tagstruct.c
index 834a972..a504be8 100644
--- a/src/pulsecore/tagstruct.c
+++ b/src/pulsecore/tagstruct.c
@@ -41,6 +41,8 @@
 #include "tagstruct.h"
 
 #define MAX_TAG_SIZE (64*1024)
+#define MAX_APPENDED_SIZE 128
+#define GROW_TAG_SIZE 100
 
 struct pa_tagstruct {
     uint8_t *data;
@@ -50,17 +52,21 @@ struct pa_tagstruct {
     enum {
         PA_TAGSTRUCT_FIXED,
         PA_TAGSTRUCT_DYNAMIC,
+        PA_TAGSTRUCT_APPENDED,
     } type;
+    union {
+        uint8_t appended[MAX_APPENDED_SIZE];
+    } per_type;
 };
 
 pa_tagstruct *pa_tagstruct_new(void) {
     pa_tagstruct*t;
 
     t = pa_xnew(pa_tagstruct, 1);
-    t->data = NULL;
-    t->allocated = t->length = 0;
-    t->rindex = 0;
-    t->type = PA_TAGSTRUCT_DYNAMIC;
+    t->data = t->per_type.appended;
+    t->allocated = MAX_APPENDED_SIZE;
+    t->length = t->rindex = 0;
+    t->type = PA_TAGSTRUCT_APPENDED;
 
     return t;
 }
@@ -94,7 +100,13 @@ static void extend(pa_tagstruct*t, size_t l) {
     if (t->length+l <= t->allocated)
         return;
 
-    t->data = pa_xrealloc(t->data, t->allocated = t->length+l+100);
+    if (t->type == PA_TAGSTRUCT_DYNAMIC)
+        t->data = pa_xrealloc(t->data, t->allocated = t->length + l + GROW_TAG_SIZE);
+    else if (t->type == PA_TAGSTRUCT_APPENDED) {
+        t->type = PA_TAGSTRUCT_DYNAMIC;
+        t->data = pa_xmalloc(t->allocated = t->length + l + GROW_TAG_SIZE);
+        memcpy(t->data, t->per_type.appended, t->length);
+    }
 }
 
 void pa_tagstruct_puts(pa_tagstruct*t, const char *s) {
-- 
1.9.1



More information about the pulseaudio-discuss mailing list