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

David Henningsson david.henningsson at canonical.com
Sun Nov 9 22:54:44 PST 2014



On 2014-11-05 00:25, Peter Meerwald wrote:
> 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,

How about adding comments like this:

  PA_TAGSTRUCT_FIXED, /* The tagstruct does not own the data, buffer was 
provided by caller */
  PA_TAGSTRUCT_DYNAMIC, /* Buffer owned by tagstruct, data must be freed */
  PA_TAGSTRUCT_APPENDED, /* Data points to per_type.appended minibuffer, 
used for small tagstructs. Will change to dynamic if needed */


>       } 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) {
>

-- 
David Henningsson, Canonical Ltd.
https://launchpad.net/~diwic


More information about the pulseaudio-discuss mailing list