From: Willy Tarreau Date: Sun, 4 Oct 2009 20:48:42 +0000 (+0200) Subject: [MINOR] tools: add a new get_next_id() function X-Git-Tag: v1.4-dev4~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=482b00d1b46337f2f22de47dca5032e31e88976f;p=thirdparty%2Fhaproxy.git [MINOR] tools: add a new get_next_id() function This function returns the next unused key in a tree. This will be used to find spare IDs. --- diff --git a/include/common/standard.h b/include/common/standard.h index 00144ccf5f..715ed240bc 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -27,6 +27,7 @@ #include #include #include +#include #include /****** string-specific macros and functions ******/ @@ -331,4 +332,9 @@ static inline unsigned int mul32hi(unsigned int a, unsigned int b) /* copies at most characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n); +/* This function returns the first unused key greater than or equal to in + * ID tree . Zero is returned if no place is found. + */ +unsigned int get_next_id(struct eb_root *root, unsigned int key); + #endif /* _COMMON_STANDARD_H */ diff --git a/src/standard.c b/src/standard.c index eecc36069b..6b0be2d0a1 100644 --- a/src/standard.c +++ b/src/standard.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -730,6 +731,23 @@ char *my_strndup(const char *src, int n) return ret; } +/* This function returns the first unused key greater than or equal to in + * ID tree . Zero is returned if no place is found. + */ +unsigned int get_next_id(struct eb_root *root, unsigned int key) +{ + struct eb32_node *used; + + do { + used = eb32_lookup_ge(root, key); + if (!used || used->key > key) + return key; /* key is available */ + key++; + } while (key); + return key; +} + + /* * Local variables: * c-indent-level: 8