Skip to contentSkip to conversation

lib: add idr_for_each()

PATCH In review Kristian Høgsberg 2 participants 3 messages Jun 1, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: add idr_for_each()

This patch adds an iterator function for the idr data structure. Compared to just iterating through the idr with an integer and idr_find, this iterator is (almost, but not quite) linear in the number of elements, as opposed to the number of integers in the range covered by the idr. This makes a difference for sparse idrs, but more importantly, it's a nicer way to iterate through the elements.

The drm subsystem is moving to idr for tracking contexts and drawables, and with this change, we can use the idr exclusively for tracking these resources.

---
 include/linux/idr.h |    2 +
 lib/idr.c           |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)
include/linux/idr.h +2 -0
index 8268034..d71735e 100644
@@ -78,6 +78,8 @@ void *idr_find(struct idr *idp, int id);
 int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
 int idr_get_new(struct idr *idp, void *ptr, int *id);
 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
+int idr_for_each(struct idr *idp,
+		 int (*fn)(int id, void *p, void *data), void *data);
 void *idr_replace(struct idr *idp, void *ptr, int id);
 void idr_remove(struct idr *idp, int id);
 void idr_destroy(struct idr *idp);
lib/idr.c +55 -0

61 lines not on this page · raw message

Signature · 1 line
1.5.0.6
#1 lore raw

PATCH 1/1 lib: add idr_remove_all

Remove all ids from the given idr tree. idr_destroy() only frees up unused, cached idp_layers, but this function will remove all id mappings and leave all idp_layers unused.

A typical clean-up sequence for objects stored in an idr tree, will use idr_for_each() to free all objects, if necessay, then idr_remove_all() to remove all ids, and idr_destroy() to free up the cached idr_layers.

---
 include/linux/idr.h |    1 +
 lib/idr.c           |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)
include/linux/idr.h +1 -0
index d71735e..2d1e105 100644
@@ -82,6 +82,7 @@ int idr_for_each(struct idr *idp,
 		 int (*fn)(int id, void *p, void *data), void *data);
 void *idr_replace(struct idr *idp, void *ptr, int id);
 void idr_remove(struct idr *idp, int id);
+void idr_remove_all(struct idr *idp);
 void idr_destroy(struct idr *idp);
 void idr_init(struct idr *idp);
 
lib/idr.c +47 -0

53 lines not on this page · raw message

Signature · 1 line
1.5.0.6
#2 lore raw
In reply to Kristian Høgsberg
Quoted 2 lines · Kristian Høgsberg

/**
+ * idr_for_each - iterate through all stored pointers

...

Quoted 1 line

+ * The caller must serialize idr_find() vs idr_get_new() and idr_remove().

                                ^^^^^^^^

lib/idr.c void *idr_find(struct idr *idp, int id) · line 426

Quoted 4 lines

+ */

lib/idr.c +3 -0
+int idr_for_each(struct idr *idp,
+		 int (*fn)(int id, void *p, void *data), void *data)
+{

...must serialize idr_for_each() vs...

Signature · 3 lines
Stefan Richter
-=====-=-=== -==- ----=
http://arcgraph.de/sr/