From 61d95606d06b7ee9541b2097c693afd042459f31 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 May 2005 01:55:18 +0000 Subject: r6687: added a idr helper function for creating random IDs (This used to be commit ef573df2a012e9a192487f207502ef1027c66d4b) --- source4/lib/idtree.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source4/lib/idtree.c') 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 @@ -344,6 +344,24 @@ int idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id, int l return ret; } +/* + 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 */ -- cgit