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.
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
{
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
);
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
)
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
;
88 /* Figure out what the height should be. */
89 height
= root
->height
+ 1;
90 while (index
> radix_tree_maxindex(height
))
95 if (!(node
= radix_tree_node_alloc(root
)))
98 /* Increase the height. */
99 node
->slots
[0] = root
->rnode
;
104 } while (height
> root
->height
);
106 root
->height
= height
;
113 * radix_tree_reserve - reserve space in a radix tree
114 * @root: radix tree root
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
;
126 /* Make sure the tree is high enough. */
127 if (index
> radix_tree_maxindex(root
->height
)) {
128 error
= radix_tree_extend(root
, index
);
134 height
= root
->height
;
135 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
139 /* Have to add a child node. */
140 if (!(tmp
= radix_tree_node_alloc(root
)))
147 /* Go a level down. */
149 slot
= (struct radix_tree_node
**)
150 (node
->slots
+ ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
151 shift
-= RADIX_TREE_MAP_SHIFT
;
160 *pslot
= (void **)slot
;
161 **pslot
= RADIX_TREE_SLOT_RESERVED
;
165 EXPORT_SYMBOL(radix_tree_reserve
);
169 * radix_tree_insert - insert into a radix tree
170 * @root: radix tree root
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
)
181 error
= radix_tree_reserve(root
, index
, &slot
);
187 EXPORT_SYMBOL(radix_tree_insert
);
191 * radix_tree_lookup - perform lookup operation on a radix tree
192 * @root: radix tree root
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
))
206 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
213 slot
= (struct radix_tree_node
**)
214 ((*slot
)->slots
+ ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
215 shift
-= RADIX_TREE_MAP_SHIFT
;
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
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
))
241 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
243 pathp
->slot
= &root
->rnode
;
246 if (*pathp
->slot
== NULL
)
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
));
253 shift
-= RADIX_TREE_MAP_SHIFT
;
257 if (*pathp
[0].slot
== NULL
)
260 *pathp
[0].slot
= NULL
;
261 while (pathp
[0].node
&& --pathp
[0].node
->count
== 0) {
263 *pathp
[0].slot
= NULL
;
264 radix_tree_node_free(pathp
[1].node
);
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
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");