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 Trees! Just the basics, doesn't do tagging or anything fancy. */
13 struct kmem_cache *radix_kcache;
14 static struct radix_node *__radix_lookup_node(struct radix_tree *tree,
17 static void __radix_remove_slot(struct radix_node *r_node, struct radix_node **slot);
19 /* Initializes the radix tree system, mostly just builds the kcache */
22 radix_kcache = kmem_cache_create("radix_nodes", sizeof(struct radix_node),
23 __alignof__(struct radix_node), 0, 0, 0);
26 /* Initializes a tree dynamically */
27 void radix_tree_init(struct radix_tree *tree)
31 tree->upper_bound = 0;
34 /* Will clean up all the memory associated with a tree. Shouldn't be necessary
35 * if you delete all of the items, which you should do anyways since they are
36 * usually void*. Might expand this to have a function to call on every leaf
38 void radix_tree_destroy(struct radix_tree *tree)
40 /* Currently, we may have a root node, even if all the elements were removed
42 panic("Not implemented");
45 /* Attempts to insert an item in the tree at the given key. ENOMEM if we ran
46 * out of memory, EEXIST if an item is already in the tree. On success, will
47 * also return the slot pointer, if requested. */
48 int radix_insert(struct radix_tree *tree, unsigned long key, void *item,
51 printd("RADIX: insert %p at %d\n", item, key);
52 struct radix_node *r_node;
54 /* Is the tree tall enough? if not, it needs to grow a level. This will
55 * also create the initial node (upper bound starts at 0). */
56 while (key >= tree->upper_bound) {
57 r_node = kmem_cache_alloc(radix_kcache, 0);
60 memset(r_node, 0, sizeof(struct radix_node));
62 /* tree->root is the old root, now a child of the future root */
63 r_node->items[0] = tree->root;
64 tree->root->parent = r_node;
65 tree->root->my_slot = (struct radix_node**)&r_node->items[0];
66 r_node->num_items = 1;
68 /* if there was no root before, we're both the root and a leaf */
73 r_node->my_slot = &tree->root;
75 tree->upper_bound = 1 << (LOG_RNODE_SLOTS * tree->depth);
78 /* the tree now thinks it is tall enough, so find the last node, insert in
80 r_node = __radix_lookup_node(tree, key, TRUE);
81 assert(r_node); /* we want an ENOMEM actually, but i want to see this */
82 slot = &r_node->items[key & (NR_RNODE_SLOTS - 1)];
92 /* Removes an item from it's parent's structure, freeing the parent if there is
93 * nothing left, potentially recursively. */
94 static void __radix_remove_slot(struct radix_node *r_node, struct radix_node **slot)
96 assert(*slot); /* make sure there is something there */
99 /* this check excludes the root, but the if else handles it. For now, once
100 * we have a root, we'll always keep it (will need some changing in
102 if (!r_node->num_items && r_node->parent) {
104 __radix_remove_slot(r_node->parent, r_node->my_slot);
105 else /* we're the last node, attached to the actual tree */
106 *(r_node->my_slot) = 0;
107 kmem_cache_free(radix_kcache, r_node);
111 /* Removes a key/item from the tree, returning that item (the void*). If it
112 * detects a radix_node is now unused, it will dealloc that node. Though the
113 * tree will still think it is tall enough to handle its old upper_bound. It
115 void *radix_delete(struct radix_tree *tree, unsigned long key)
117 printd("RADIX: delete %d\n", key);
120 struct radix_node *r_node = __radix_lookup_node(tree, key, 0);
123 slot = &r_node->items[key & (NR_RNODE_SLOTS - 1)];
126 __radix_remove_slot(r_node, (struct radix_node**)slot);
128 /* it's okay to delete an empty, but i want to know about it for now */
129 warn("Tried to remove a non-existant item from a radix tree!");
134 /* Returns the item for a given key. 0 means no item, etc. */
135 void *radix_lookup(struct radix_tree *tree, unsigned long key)
137 printd("RADIX: lookup %d\n", key);
138 void **slot = radix_lookup_slot(tree, key);
144 /* Returns a pointer to the radix_node holding a given key. 0 if there is no
145 * such node, due to the tree being too small or something.
147 * If the depth is greater than one, we need to walk down the tree a level. The
148 * key is 'partitioned' among the levels of the tree, like so:
149 * ......444444333333222222111111
151 * If an interior node of the tree is missing, this will add one if it was
152 * directed to extend the tree. */
153 static struct radix_node *__radix_lookup_node(struct radix_tree *tree,
154 unsigned long key, bool extend)
156 printd("RADIX: lookup_node %d, %d\n", key, extend);
158 struct radix_node *child_node, *r_node = tree->root;
159 if (key >= tree->upper_bound) {
161 warn("Bound (%d) not set for key %d!\n", tree->upper_bound, key);
164 for (int i = tree->depth; i > 1; i--) { /* i = ..., 4, 3, 2 */
165 idx = (key >> (LOG_RNODE_SLOTS * (i - 1))) & (NR_RNODE_SLOTS - 1);
166 /* There might not be a node at this part of the tree */
167 if (!r_node->items[idx]) {
171 /* so build one, possibly returning 0 if we couldn't */
172 child_node = kmem_cache_alloc(radix_kcache, 0);
175 r_node->items[idx] = child_node;
176 memset(child_node, 0, sizeof(struct radix_node));
177 /* when we are on the last iteration (i == 2), the child will be
179 child_node->leaf = (i == 2) ? TRUE : FALSE;
180 child_node->parent = r_node;
181 child_node->my_slot = (struct radix_node**)&r_node->items[idx];
183 r_node = (struct radix_node*)r_node->items[idx];
186 r_node = (struct radix_node*)r_node->items[idx];
192 /* Returns a pointer to the slot for the given key. 0 if there is no such slot,
194 void **radix_lookup_slot(struct radix_tree *tree, unsigned long key)
196 printd("RADIX: lookup slot %d\n", key);
197 struct radix_node *r_node = __radix_lookup_node(tree, key, FALSE);
200 key = key & (NR_RNODE_SLOTS - 1);
201 return &r_node->items[key];
204 int radix_gang_lookup(struct radix_tree *tree, void **results,
205 unsigned long first, unsigned int max_items)
207 panic("Not implemented");
208 return -1; /* TODO! */
212 int radix_grow(struct radix_tree *tree, unsigned long max)
214 panic("Not implemented");
215 return -1; /* TODO! */
218 int radix_preload(struct radix_tree *tree, int flags)
220 panic("Not implemented");
221 return -1; /* TODO! */
225 void *radix_tag_set(struct radix_tree *tree, unsigned long key, int tag)
227 panic("Tagging not implemented!");
228 return (void*)-1; /* TODO! */
231 void *radix_tag_clear(struct radix_tree *tree, unsigned long key, int tag)
233 panic("Tagging not implemented!");
234 return (void*)-1; /* TODO! */
237 int radix_tag_get(struct radix_tree *tree, unsigned long key, int tag)
239 panic("Tagging not implemented!");
240 return -1; /* TODO! */
243 int radix_tree_tagged(struct radix_tree *tree, int tag)
245 panic("Tagging not implemented!");
246 return -1; /* TODO! */
249 int radix_tag_gang_lookup(struct radix_tree *tree, void **results,
250 unsigned long first, unsigned int max_items, int tag)
252 panic("Tagging not implemented!");
253 return -1; /* TODO! */
256 void print_radix_tree(struct radix_tree *tree)
258 printk("Tree %p, Depth: %d, Bound: %d\n", tree, tree->depth,
261 void print_rnode(struct radix_node *r_node, int depth)
266 for (int i = 0; i < depth; i++)
268 printk("%sRnode %p, parent %p, myslot %p, %d items, leaf? %d\n",
269 buf, r_node, r_node->parent, r_node->my_slot, r_node->num_items,
271 for (int i = 0; i < NR_RNODE_SLOTS; i++) {
272 if (!r_node->items[i])
275 printk("\t%sRnode Item %d: %p\n", buf, i, r_node->items[i]);
277 print_rnode(r_node->items[i], depth + 1);
280 print_rnode(tree->root, 0);