[pulseaudio-discuss] [PATCH 02/17] dynarray: Add PA_DYNARRAY_FOREACH

Tanu Kaskinen tanu.kaskinen at linux.intel.com
Mon Dec 30 05:04:37 PST 2013


The PA_DYNARRAY_FOREACH macro requires that pa_dynarray_get() returns
NULL if the index is out of bounds.
---
 src/pulsecore/dynarray.c  | 4 +++-
 src/pulsecore/dynarray.h  | 5 +++++
 src/pulsecore/tokenizer.c | 3 ---
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index b207eca..569c5cb 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 04dd2d2..3aacde8 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. */
@@ -55,4 +57,7 @@ void *pa_dynarray_steal_last(pa_dynarray *array);
 
 unsigned pa_dynarray_size(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);
 }
-- 
1.8.3.1



More information about the pulseaudio-discuss mailing list