]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] tools: add a new get_next_id() function
authorWilly Tarreau <w@1wt.eu>
Sun, 4 Oct 2009 20:48:42 +0000 (22:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 4 Oct 2009 20:48:42 +0000 (22:48 +0200)
This function returns the next unused key in a tree. This will be
used to find spare IDs.

include/common/standard.h
src/standard.c

index 00144ccf5f3703e5a495e8698939186e1e3fd9ee..715ed240bc0eac8cfdb4b8919a273f2e5e0a9a54 100644 (file)
@@ -27,6 +27,7 @@
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <common/config.h>
+#include <common/eb32tree.h>
 #include <proto/fd.h>
 
 /****** string-specific macros and functions ******/
@@ -331,4 +332,9 @@ static inline unsigned int mul32hi(unsigned int a, unsigned int b)
 /* copies at most <n> characters from <src> 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 <key> in
+ * ID tree <root>. Zero is returned if no place is found.
+ */
+unsigned int get_next_id(struct eb_root *root, unsigned int key);
+
 #endif /* _COMMON_STANDARD_H */
index eecc36069b764308f9fc9673f88e20f175b5fd2b..6b0be2d0a16f0bcc197f18d4d38c2db959b7b6fa 100644 (file)
@@ -20,6 +20,7 @@
 #include <arpa/inet.h>
 
 #include <common/config.h>
+#include <common/eb32tree.h>
 #include <common/standard.h>
 #include <proto/log.h>
 
@@ -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 <key> in
+ * ID tree <root>. 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