[RFC] queue: Add queue_foreach_if

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

 



From: Andrei Emeltchenko <andrei.emeltchenko@xxxxxxxxx>

Function allows to search for a second level queue inside first level
queue. In the current implementation since head is hidden this is not
possible.
---
 src/shared/queue.c | 33 +++++++++++++++++++++++++++++++++
 src/shared/queue.h |  3 +++
 2 files changed, 36 insertions(+)

diff --git a/src/shared/queue.c b/src/shared/queue.c
index 4013293..454b328 100644
--- a/src/shared/queue.c
+++ b/src/shared/queue.c
@@ -224,6 +224,39 @@ void queue_foreach(struct queue *queue, queue_foreach_func_t function,
 	queue_unref(queue);
 }
 
+void *queue_foreach_if(struct queue *queue, queue_foreach_if_func_t function,
+							void *user_data)
+{
+	struct queue_entry *entry;
+
+	if (!queue || !function)
+		return NULL;
+
+	entry = queue->head;
+	if (!entry)
+		return NULL;
+
+	queue_ref(queue);
+	while (entry && queue->ref_count > 1) {
+		struct queue_entry *tmp = entry;
+		void *ret;
+
+		entry = tmp->next;
+
+		ret = function(tmp->data, user_data);
+		if (ret) {
+			queue_unref(queue);
+			return ret;
+		}
+
+		if (!queue_find_entry(queue, entry))
+			break;
+	}
+	queue_unref(queue);
+
+	return NULL;
+}
+
 static bool direct_match(const void *a, const void *b)
 {
 	return a == b;
diff --git a/src/shared/queue.h b/src/shared/queue.h
index 709590b..bf5bf8f 100644
--- a/src/shared/queue.h
+++ b/src/shared/queue.h
@@ -37,9 +37,12 @@ void *queue_peek_head(struct queue *queue);
 void *queue_peek_tail(struct queue *queue);
 
 typedef void (*queue_foreach_func_t)(void *data, void *user_data);
+typedef void *(*queue_foreach_if_func_t)(void *data, void *user_data);
 
 void queue_foreach(struct queue *queue, queue_foreach_func_t function,
 							void *user_data);
+void *queue_foreach_if(struct queue *queue, queue_foreach_if_func_t function,
+							void *user_data);
 
 typedef bool (*queue_match_func_t)(const void *a, const void *b);
 
-- 
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux