1
From adb577c905f2e930bdd2bfe630d6ca0ea286d6cb Mon Sep 17 00:00:00 2001
2
From: Peter Meerwald <p.meerwald@bct-electronic.com>
3
Date: Wed, 22 Oct 2014 14:59:11 +0200
4
Subject: [PATCH 4/5] tagstruct: Add type _APPENDED
6
add 128 bytes of storage in each tagstruct that will initially
7
be used; if this storage is exceeded the type changes to _DYNAMIC
9
v3: (thanks David Henningson)
10
* add comments explaining how memory is handled by different tagstruct types
11
v2: (thanks Alexander Patrakov)
12
* replace constant 100 with GROW_TAG_SIZE (the increment in with a dynamic tagstruct grows when extend()ed)
14
Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
16
src/pulsecore/tagstruct.c | 26 +++++++++++++++++++-------
17
1 file changed, 19 insertions(+), 7 deletions(-)
19
diff --git a/src/pulsecore/tagstruct.c b/src/pulsecore/tagstruct.c
20
index e78fb4a..4206509 100644
21
--- a/src/pulsecore/tagstruct.c
22
+++ b/src/pulsecore/tagstruct.c
24
#include "tagstruct.h"
26
#define MAX_TAG_SIZE (64*1024)
27
+#define MAX_APPENDED_SIZE 128
28
+#define GROW_TAG_SIZE 100
32
@@ -46,19 +48,23 @@ struct pa_tagstruct {
37
- PA_TAGSTRUCT_DYNAMIC,
38
+ PA_TAGSTRUCT_FIXED, /* The tagstruct does not own the data, buffer was provided by caller. */
39
+ PA_TAGSTRUCT_DYNAMIC, /* Buffer owned by tagstruct, data must be freed. */
40
+ PA_TAGSTRUCT_APPENDED, /* Data points to appended buffer, used for small tagstructs. Will change to dynamic if needed. */
43
+ uint8_t appended[MAX_APPENDED_SIZE];
47
pa_tagstruct *pa_tagstruct_new(void) {
50
t = pa_xnew(pa_tagstruct, 1);
52
- t->allocated = t->length = 0;
54
- t->type = PA_TAGSTRUCT_DYNAMIC;
55
+ t->data = t->per_type.appended;
56
+ t->allocated = MAX_APPENDED_SIZE;
57
+ t->length = t->rindex = 0;
58
+ t->type = PA_TAGSTRUCT_APPENDED;
62
@@ -92,7 +98,13 @@ static inline void extend(pa_tagstruct*t, size_t l) {
63
if (t->length+l <= t->allocated)
66
- t->data = pa_xrealloc(t->data, t->allocated = t->length+l+100);
67
+ if (t->type == PA_TAGSTRUCT_DYNAMIC)
68
+ t->data = pa_xrealloc(t->data, t->allocated = t->length + l + GROW_TAG_SIZE);
69
+ else if (t->type == PA_TAGSTRUCT_APPENDED) {
70
+ t->type = PA_TAGSTRUCT_DYNAMIC;
71
+ t->data = pa_xmalloc(t->allocated = t->length + l + GROW_TAG_SIZE);
72
+ memcpy(t->data, t->per_type.appended, t->length);
76
static void write_u8(pa_tagstruct *t, uint8_t u) {