[PATCH] m68k: bitops update [3/20]
[linux-2.6/history.git] / lib / radix-tree.c
blob689a5448ea31b372f20ba70f5b3f1210e148fa9e
1 /*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/mempool.h>
24 #include <linux/module.h>
25 #include <linux/radix-tree.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
30 * Radix tree node definition.
32 #define RADIX_TREE_MAP_SHIFT 7
33 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
34 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
36 struct radix_tree_node {
37 unsigned int count;
38 void *slots[RADIX_TREE_MAP_SIZE];
41 struct radix_tree_path {
42 struct radix_tree_node *node, **slot;
45 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
48 * Radix tree node cache.
50 static kmem_cache_t *radix_tree_node_cachep;
51 static mempool_t *radix_tree_node_pool;
53 static inline struct radix_tree_node *
54 radix_tree_node_alloc(struct radix_tree_root *root)
56 return mempool_alloc(radix_tree_node_pool, root->gfp_mask);
59 static inline void
60 radix_tree_node_free(struct radix_tree_node *node)
62 mempool_free(node, radix_tree_node_pool);
66 * Return the maximum key which can be store into a
67 * radix tree with height HEIGHT.
69 static inline unsigned long radix_tree_maxindex(unsigned int height)
71 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
72 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
74 if (tmp >= RADIX_TREE_INDEX_BITS)
75 index = ~0UL;
76 return index;
81 * Extend a radix tree so it can store key @index.
83 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
85 struct radix_tree_node *node;
86 unsigned int height;
88 /* Figure out what the height should be. */
89 height = root->height + 1;
90 while (index > radix_tree_maxindex(height))
91 height++;
93 if (root->rnode) {
94 do {
95 if (!(node = radix_tree_node_alloc(root)))
96 return -ENOMEM;
98 /* Increase the height. */
99 node->slots[0] = root->rnode;
100 if (root->rnode)
101 node->count = 1;
102 root->rnode = node;
103 root->height++;
104 } while (height > root->height);
105 } else
106 root->height = height;
108 return 0;
113 * radix_tree_reserve - reserve space in a radix tree
114 * @root: radix tree root
115 * @index: index key
116 * @pslot: pointer to reserved slot
118 * Reserve a slot in a radix tree for the key @index.
120 int radix_tree_reserve(struct radix_tree_root *root, unsigned long index, void ***pslot)
122 struct radix_tree_node *node = NULL, *tmp, **slot;
123 unsigned int height, shift;
124 int error;
126 /* Make sure the tree is high enough. */
127 if (index > radix_tree_maxindex(root->height)) {
128 error = radix_tree_extend(root, index);
129 if (error)
130 return error;
133 slot = &root->rnode;
134 height = root->height;
135 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
137 while (height > 0) {
138 if (*slot == NULL) {
139 /* Have to add a child node. */
140 if (!(tmp = radix_tree_node_alloc(root)))
141 return -ENOMEM;
142 *slot = tmp;
143 if (node)
144 node->count++;
147 /* Go a level down. */
148 node = *slot;
149 slot = (struct radix_tree_node **)
150 (node->slots + ((index >> shift) & RADIX_TREE_MAP_MASK));
151 shift -= RADIX_TREE_MAP_SHIFT;
152 height--;
155 if (*slot != NULL)
156 return -EEXIST;
157 if (node)
158 node->count++;
160 *pslot = (void **)slot;
161 **pslot = RADIX_TREE_SLOT_RESERVED;
162 return 0;
165 EXPORT_SYMBOL(radix_tree_reserve);
169 * radix_tree_insert - insert into a radix tree
170 * @root: radix tree root
171 * @index: index key
172 * @item: item to insert
174 * Insert an item into the radix tree at position @index.
176 int radix_tree_insert(struct radix_tree_root *root, unsigned long index, void *item)
178 void **slot;
179 int error;
181 error = radix_tree_reserve(root, index, &slot);
182 if (!error)
183 *slot = item;
184 return error;
187 EXPORT_SYMBOL(radix_tree_insert);
191 * radix_tree_lookup - perform lookup operation on a radix tree
192 * @root: radix tree root
193 * @index: index key
195 * Lookup them item at the position @index in the radix tree @root.
197 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
199 unsigned int height, shift;
200 struct radix_tree_node **slot;
202 height = root->height;
203 if (index > radix_tree_maxindex(height))
204 return NULL;
206 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
207 slot = &root->rnode;
209 while (height > 0) {
210 if (*slot == NULL)
211 return NULL;
213 slot = (struct radix_tree_node **)
214 ((*slot)->slots + ((index >> shift) & RADIX_TREE_MAP_MASK));
215 shift -= RADIX_TREE_MAP_SHIFT;
216 height--;
219 return (void *) *slot;
222 EXPORT_SYMBOL(radix_tree_lookup);
226 * radix_tree_delete - delete an item from a radix tree
227 * @root: radix tree root
228 * @index: index key
230 * Remove the item at @index from the radix tree rooted at @root.
232 int radix_tree_delete(struct radix_tree_root *root, unsigned long index)
234 struct radix_tree_path path[RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2], *pathp = path;
235 unsigned int height, shift;
237 height = root->height;
238 if (index > radix_tree_maxindex(height))
239 return -ENOENT;
241 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
242 pathp->node = NULL;
243 pathp->slot = &root->rnode;
245 while (height > 0) {
246 if (*pathp->slot == NULL)
247 return -ENOENT;
249 pathp[1].node = *pathp[0].slot;
250 pathp[1].slot = (struct radix_tree_node **)
251 (pathp[1].node->slots + ((index >> shift) & RADIX_TREE_MAP_MASK));
252 pathp++;
253 shift -= RADIX_TREE_MAP_SHIFT;
254 height--;
257 if (*pathp[0].slot == NULL)
258 return -ENOENT;
260 *pathp[0].slot = NULL;
261 while (pathp[0].node && --pathp[0].node->count == 0) {
262 pathp--;
263 *pathp[0].slot = NULL;
264 radix_tree_node_free(pathp[1].node);
267 return 0;
270 EXPORT_SYMBOL(radix_tree_delete);
272 static void radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
274 memset(node, 0, sizeof(struct radix_tree_node));
277 static void *radix_tree_node_pool_alloc(int gfp_mask, void *data)
279 return kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
282 static void radix_tree_node_pool_free(void *node, void *data)
284 kmem_cache_free(radix_tree_node_cachep, node);
288 * FIXME! 512 nodes is 200-300k of memory. This needs to be
289 * scaled by the amount of available memory, and hopefully
290 * reduced also.
292 void __init radix_tree_init(void)
294 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
295 sizeof(struct radix_tree_node), 0,
296 SLAB_HWCACHE_ALIGN, radix_tree_node_ctor, NULL);
297 if (!radix_tree_node_cachep)
298 panic ("Failed to create radix_tree_node cache\n");
299 radix_tree_node_pool = mempool_create(512, radix_tree_node_pool_alloc,
300 radix_tree_node_pool_free, NULL);
301 if (!radix_tree_node_pool)
302 panic ("Failed to create radix_tree_node pool\n");