summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source4/lib/idtree.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/source4/lib/idtree.c b/source4/lib/idtree.c
index 82c1cdd42a..a67a80940a 100644
--- a/source4/lib/idtree.c
+++ b/source4/lib/idtree.c
@@ -345,6 +345,24 @@ int idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id, int l
}
/*
+ allocate a new id randomly in the given range
+*/
+int idr_get_new_random(struct idr_context *idp, void *ptr, int limit)
+{
+ int id;
+
+ /* first try a random starting point in the whole range, and if that fails,
+ then start randomly in the bottom half of the range. This can only
+ fail if the range is over half full */
+ id = idr_get_new_above(idp, ptr, 1+(generate_random() % limit), limit);
+ if (id == -1) {
+ id = idr_get_new_above(idp, ptr, 1+(generate_random()%(limit/2)), limit);
+ }
+
+ return id;
+}
+
+/*
find a pointer value previously set with idr_get_new given an id
*/
void *idr_find(struct idr_context *idp, int id)