[pulseaudio-discuss] [PATCH v2 4/6] dynarray: Add PA_DYNARRAY_FOREACH
Tanu Kaskinen
tanu.kaskinen at linux.intel.com
Wed Jan 7 06:56:50 PST 2015
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 d6ef3b8..f2b2069 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 cdc8eeb..3849e06 100644
--- a/src/pulsecore/dynarray.h
+++ b/src/pulsecore/dynarray.h
@@ -45,6 +45,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 the element at index i, or NULL if i is out of bounds. */
void *pa_dynarray_get(pa_dynarray *array, unsigned i);
/* Returns the last element, or NULL if the array is empty. */
@@ -63,4 +65,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.9.3
More information about the pulseaudio-discuss
mailing list