[PATCH v2 3/6] dynarray: Add pa_dynarray_remove_by_data()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 src/pulsecore/dynarray.c | 20 ++++++++++++++++++++
 src/pulsecore/dynarray.h |  5 +++++
 2 files changed, 25 insertions(+)

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



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux