[pulseaudio-commits] Branch 'next' - 4 commits - src/pulsecore

Tanu Kaskinen tanuk at kemper.freedesktop.org
Wed Jan 7 12:16:57 PST 2015


 src/pulsecore/dynarray.c  |   51 +++++++++++++++++++++++++++++++++++++++++++++-
 src/pulsecore/dynarray.h  |   25 +++++++++++++++++-----
 src/pulsecore/tokenizer.c |    3 --
 3 files changed, 69 insertions(+), 10 deletions(-)

New commits:
commit 2d69d8e0a12bccae318b38a466e42a7af2df89b8
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Jan 7 16:56:50 2015 +0200

    dynarray: Add PA_DYNARRAY_FOREACH
    
    The PA_DYNARRAY_FOREACH macro requires that pa_dynarray_get() returns
    NULL if the index is out of bounds.

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

commit 507d6e3a95cc99505bfe8cd14379ecfb4281031d
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Jan 7 16:56:49 2015 +0200

    dynarray: Add pa_dynarray_remove_by_data()

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index 392e7d6..d6ef3b8 100644
--- a/src/pulsecore/dynarray.c
+++ b/src/pulsecore/dynarray.c
@@ -106,6 +106,26 @@ int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i) {
     return 0;
 }
 
+int pa_dynarray_remove_by_data(pa_dynarray *array, void *p) {
+    unsigned i;
+
+    pa_assert(array);
+    pa_assert(p);
+
+    /* Iterate backwards, with the assumption that recently appended entries
+     * are likely to be removed first. */
+    i = array->n_entries;
+    while (i > 0) {
+        i--;
+        if (array->data[i] == p) {
+            pa_dynarray_remove_by_index(array, i);
+            return 0;
+        }
+    }
+
+    return -PA_ERR_NOENTITY;
+}
+
 void *pa_dynarray_steal_last(pa_dynarray *array) {
     pa_assert(array);
 
diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h
index 92352a5..cdc8eeb 100644
--- a/src/pulsecore/dynarray.h
+++ b/src/pulsecore/dynarray.h
@@ -53,6 +53,11 @@ void *pa_dynarray_last(pa_dynarray *array);
 /* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. */
 int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i);
 
+/* Returns -PA_ERR_NOENTITY if p is not found in the array, and zero
+ * otherwise. If the array contains multiple occurrencies of p, only one of
+ * them is removed (and it's unspecified which one). */
+int pa_dynarray_remove_by_data(pa_dynarray *array, void *p);
+
 /* Returns the removed item, or NULL if the array is empty. */
 void *pa_dynarray_steal_last(pa_dynarray *array);
 

commit 6c9a7f8e5fa0f900d722d58973c708a71ce72d84
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Jan 7 16:56:48 2015 +0200

    dynarray: Add pa_dynarray_remove_by_index()
    
    Also, remove the talk about "fast" variants of functions that remove
    entries from an array. Currently there's no need for order-preserving
    functions, so all functions are "fast".

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index 82db496..392e7d6 100644
--- a/src/pulsecore/dynarray.c
+++ b/src/pulsecore/dynarray.c
@@ -88,6 +88,24 @@ void *pa_dynarray_last(pa_dynarray *array) {
     return array->data[array->n_entries - 1];
 }
 
+int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i) {
+    void *entry;
+
+    pa_assert(array);
+
+    if (i >= array->n_entries)
+        return -PA_ERR_NOENTITY;
+
+    entry = array->data[i];
+    array->data[i] = array->data[array->n_entries - 1];
+    array->n_entries--;
+
+    if (array->free_cb)
+        array->free_cb(entry);
+
+    return 0;
+}
+
 void *pa_dynarray_steal_last(pa_dynarray *array) {
     pa_assert(array);
 
diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h
index 946357b..92352a5 100644
--- a/src/pulsecore/dynarray.h
+++ b/src/pulsecore/dynarray.h
@@ -35,12 +35,9 @@ typedef struct pa_dynarray pa_dynarray;
  * from the array without freeing them, while also having the free callback
  * set, the functions with "steal" in their name can be used.
  *
- * Removing items from the middle of the array causes the subsequent items to
- * be moved to fill the gap, so it's not efficient with large arrays. If the
- * order of the array is not important, however, functions with "fast" in their
- * name can be used, in which case the gap is filled by moving only the last
- * item(s). XXX: Currently there are no functions with "fast" in their name,
- * but such functions will be added if they are ever needed.
+ * Removing items from the middle of the array causes the last item to be
+ * moved to the place of the removed item. That is, array ordering is not
+ * preserved.
  *
  * The array doesn't support storing NULL pointers. */
 
@@ -53,6 +50,9 @@ void *pa_dynarray_get(pa_dynarray *array, unsigned i);
 /* Returns the last element, or NULL if the array is empty. */
 void *pa_dynarray_last(pa_dynarray *array);
 
+/* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. */
+int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i);
+
 /* Returns the removed item, or NULL if the array is empty. */
 void *pa_dynarray_steal_last(pa_dynarray *array);
 

commit 59406f645237d015a0f56bafe603a9ac3ad84b3e
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Jan 7 16:56:47 2015 +0200

    dynarray: Add pa_dynarray_last()

diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c
index b207eca..82db496 100644
--- a/src/pulsecore/dynarray.c
+++ b/src/pulsecore/dynarray.c
@@ -79,6 +79,15 @@ void *pa_dynarray_get(pa_dynarray *array, unsigned i) {
     return array->data[i];
 }
 
+void *pa_dynarray_last(pa_dynarray *array) {
+    pa_assert(array);
+
+    if (array->n_entries == 0)
+        return NULL;
+
+    return array->data[array->n_entries - 1];
+}
+
 void *pa_dynarray_steal_last(pa_dynarray *array) {
     pa_assert(array);
 
diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h
index 04dd2d2..946357b 100644
--- a/src/pulsecore/dynarray.h
+++ b/src/pulsecore/dynarray.h
@@ -50,6 +50,9 @@ void pa_dynarray_free(pa_dynarray *array);
 void pa_dynarray_append(pa_dynarray *array, void *p);
 void *pa_dynarray_get(pa_dynarray *array, unsigned i);
 
+/* Returns the last element, or NULL if the array is empty. */
+void *pa_dynarray_last(pa_dynarray *array);
+
 /* Returns the removed item, or NULL if the array is empty. */
 void *pa_dynarray_steal_last(pa_dynarray *array);
 



More information about the pulseaudio-commits mailing list