1 /* Copyright (c) 2010 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Radix Tree, modeled after Linux's (described here:
6 * http://lwn.net/Articles/175432/).
8 * It maps unsigned longs to void*s, growing the depth of the tree as needed to
9 * handle more items. It won't allow the insertion of existing keys, and it
10 * can fail due to lack of memory.
12 * There are some utility functions, probably unimplemented til we need them,
13 * that will make the tree have enough memory for future calls.
15 * You can also store a tag along with the void* for a given item, and do
16 * lookups based on those tags. Or you will be able to, once it is
21 #define LOG_RNODE_SLOTS 6
22 #define NR_RNODE_SLOTS (1 << LOG_RNODE_SLOTS)
24 #include <ros/common.h>
27 void *items[NR_RNODE_SLOTS];
28 unsigned int num_items;
30 struct radix_node *parent;
31 struct radix_node **my_slot;
34 /* Defines the whole tree. */
36 struct radix_node *root;
38 unsigned long upper_bound;
41 void radix_init(void); /* initializes the whole radix system */
42 #define RADIX_INITIALIZER {0, 0, 0}
43 void radix_tree_init(struct radix_tree *tree); /* inits one tree */
44 void radix_tree_destroy(struct radix_tree *tree);
47 int radix_insert(struct radix_tree *tree, unsigned long key, void *item,
49 void *radix_delete(struct radix_tree *tree, unsigned long key);
50 void *radix_lookup(struct radix_tree *tree, unsigned long key);
51 void **radix_lookup_slot(struct radix_tree *tree, unsigned long key);
52 int radix_gang_lookup(struct radix_tree *tree, void **results,
53 unsigned long first, unsigned int max_items);
55 /* Memory management */
56 int radix_grow(struct radix_tree *tree, unsigned long max);
57 int radix_preload(struct radix_tree *tree, int flags);
60 void *radix_tag_set(struct radix_tree *tree, unsigned long key, int tag);
61 void *radix_tag_clear(struct radix_tree *tree, unsigned long key, int tag);
62 int radix_tag_get(struct radix_tree *tree, unsigned long key, int tag);
63 int radix_tree_tagged(struct radix_tree *tree, int tag);
64 int radix_tag_gang_lookup(struct radix_tree *tree, void **results,
65 unsigned long first, unsigned int max_items, int tag);
68 void print_radix_tree(struct radix_tree *tree);