From: Tanu Kaskinen Date: Thu, 19 Dec 2013 19:29:50 +0000 (+0200) Subject: dynarray: Add PA_DYNARRAY_FOREACH X-Git-Tag: accepted/tizen/ivi/20140604.184622~35 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c34706c350728eb2d88ff623134171c6b27c4667;p=platform%2Fupstream%2Fpulseaudio.git dynarray: Add PA_DYNARRAY_FOREACH The PA_DYNARRAY_FOREACH macro requires that pa_dynarray_get() returns NULL if the index is out of bounds. Change-Id: If9db312516fbb079e8b67d94d35a44783ab3395a Signed-off-by: Jaska Uimonen --- diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c index b65fb62..8dd8fab 100644 --- a/src/pulsecore/dynarray.c +++ b/src/pulsecore/dynarray.c @@ -74,7 +74,9 @@ void pa_dynarray_append(pa_dynarray *array, void *p) { void *pa_dynarray_get(pa_dynarray *array, unsigned i) { pa_assert(array); - pa_assert(i < array->n_entries); + + if (i >= array->n_entries) + return NULL; return array->data[i]; } diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h index 078acec..65030f2 100644 --- a/src/pulsecore/dynarray.h +++ b/src/pulsecore/dynarray.h @@ -48,6 +48,8 @@ pa_dynarray* pa_dynarray_new(pa_free_cb_t free_cb); void pa_dynarray_free(pa_dynarray *array); void pa_dynarray_append(pa_dynarray *array, void *p); + +/* Returns NULL if i is out of bounds. */ void *pa_dynarray_get(pa_dynarray *array, unsigned i); /* Returns the removed item, or NULL if the array is empty. */ @@ -56,4 +58,7 @@ void *pa_dynarray_steal_last(pa_dynarray *array); unsigned pa_dynarray_size(pa_dynarray *array); void * const *pa_dynarray_get_raw_array(pa_dynarray *array); +#define PA_DYNARRAY_FOREACH(elem, array, idx) \ + for ((idx) = 0; ((elem) = pa_dynarray_get(array, idx)); (idx)++) + #endif diff --git a/src/pulsecore/tokenizer.c b/src/pulsecore/tokenizer.c index 4c610e8..d71a7da 100644 --- a/src/pulsecore/tokenizer.c +++ b/src/pulsecore/tokenizer.c @@ -80,8 +80,5 @@ const char *pa_tokenizer_get(pa_tokenizer *t, unsigned i) { pa_assert(a); - if (i >= pa_dynarray_size(a)) - return NULL; - return pa_dynarray_get(a, i); }