[PATCH 13/18] include: linux/idr.h: implement more Linux API

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

 



Upcoming sync of SCMI with the kernel will start using IDR API, which we
lack in barebox, so let's retrofit it.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
 drivers/firmware/arm_scmi/driver.c |  2 +-
 include/linux/idr.h                | 69 ++++++++++++++++++++++++++----
 2 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index dcd15528f394..c3536d6e9fbd 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1001,7 +1001,7 @@ int scmi_protocol_device_request(const struct scmi_device_id *id_table)
 	 * Search for the matching protocol rdev list and then search
 	 * of any existent equally named device...fails if any duplicate found.
 	 */
-	__idr_for_each_entry(&scmi_requested_devices, idr) {
+	idr_for_each_entry(&scmi_requested_devices, idr) {
 		struct list_head *head = idr->ptr;
 		if (!phead) {
 			/* A list found registered in the IDR is never empty */
diff --git a/include/linux/idr.h b/include/linux/idr.h
index 12494b1f01cb..de1d5d78f546 100644
--- a/include/linux/idr.h
+++ b/include/linux/idr.h
@@ -24,21 +24,66 @@ struct idr {
 #define DEFINE_IDR(name)	\
 	struct idr name = { .list = LIST_HEAD_INIT((name).list) }
 
-#define __idr_for_each_entry(head, idr) \
-	list_for_each_entry((idr), &(head)->list, list)
+/**
+ * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
+ * @_idr: IDR handle.
+ * @_entry: The type * to use as cursor
+ * @_id: Entry ID.
+ *
+ * @_entry and @_id do not need to be initialized before the loop, and
+ * after normal termination @_entry is left with the value NULL.  This
+ * is convenient for a "not found" value.
+ */
+#define idr_for_each_entry(_idr, _entry, _id)		\
+	for (struct idr *iter = (_idr);				\
+	     &iter->list != &((_idr)->list) || (_entry = NULL);	\
+	     iter = list_next_entry(iter, list))		\
+	if ((_entry = iter->ptr, _id = iter->id, true))
 
-static inline struct idr *__idr_find(struct idr *head, int id)
+static inline struct idr *__idr_find(struct idr *head, int lookup_id)
 {
-	struct idr *idr;
+	struct idr *cursor;
 
-	__idr_for_each_entry(head, idr) {
-		if (idr->id == id)
-			return idr;
+	list_for_each_entry(cursor, &head->list, list) {
+		if (cursor->id == lookup_id)
+			return cursor;
 	}
 
 	return NULL;
 }
 
+/**
+ * idr_for_each() - Iterate through all stored pointers.
+ * @idr: IDR handle.
+ * @fn: Function to be called for each pointer.
+ * @data: Data passed to callback function.
+ *
+ * The callback function will be called for each entry in @idr, passing
+ * the ID, the entry and @data.
+ *
+ * If @fn returns anything other than %0, the iteration stops and that
+ * value is returned from this function.
+ */
+static inline int idr_for_each(const struct idr *idr,
+			       int (*fn)(int id, void *p, void *data), void *data)
+{
+	const struct idr *pos, *tmp;
+	int ret;
+
+	list_for_each_entry_safe(pos, tmp, &idr->list, list) {
+		ret = fn(pos->id, pos->ptr, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static inline int idr_is_empty(const struct idr *idr)
+{
+	return list_empty(&idr->list);
+}
+
 static inline void *idr_find(struct idr *head, int id)
 {
 	struct idr *idr = __idr_find(head, id);
@@ -70,12 +115,18 @@ static inline void idr_init(struct idr *idr)
 	INIT_LIST_HEAD(&idr->list);
 }
 
-static inline void idr_remove(struct idr *head, int id)
+static inline void idr_destroy(struct idr *idr)
 {
-	struct idr *idr = __idr_find(head, id);
+	if (!idr)
+		return;
 
 	list_del(&idr->list);
 	free(idr);
 }
 
+static inline void idr_remove(struct idr *head, int id)
+{
+	idr_destroy(__idr_find(head, id));
+}
+
 #endif /* __IDR_H__ */
-- 
2.39.2





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux