[PATCH] DVB: misc. updates to the dvb-core
[linux-2.6/history.git] / lib / radix-tree.c
bloba0a0902614e7c087304743c10de35571dcc783ab
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.
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/module.h>
24 #include <linux/radix-tree.h>
25 #include <linux/percpu.h>
26 #include <linux/slab.h>
27 #include <linux/notifier.h>
28 #include <linux/cpu.h>
29 #include <linux/gfp.h>
30 #include <linux/string.h>
31 #include <linux/bitops.h>
34 #ifdef __KERNEL__
35 #define RADIX_TREE_MAP_SHIFT 6
36 #else
37 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
38 #endif
39 #define RADIX_TREE_TAGS 2
41 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
42 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
44 #define RADIX_TREE_TAG_LONGS \
45 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
47 struct radix_tree_node {
48 unsigned int count;
49 void *slots[RADIX_TREE_MAP_SIZE];
50 unsigned long tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
53 struct radix_tree_path {
54 struct radix_tree_node *node, **slot;
55 int offset;
58 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
59 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
61 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH];
64 * Radix tree node cache.
66 static kmem_cache_t *radix_tree_node_cachep;
69 * Per-cpu pool of preloaded nodes
71 struct radix_tree_preload {
72 int nr;
73 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
75 DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
78 * This assumes that the caller has performed appropriate preallocation, and
79 * that the caller has pinned this thread of control to the current CPU.
81 static struct radix_tree_node *
82 radix_tree_node_alloc(struct radix_tree_root *root)
84 struct radix_tree_node *ret;
86 ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
87 if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
88 struct radix_tree_preload *rtp;
90 rtp = &__get_cpu_var(radix_tree_preloads);
91 if (rtp->nr) {
92 ret = rtp->nodes[rtp->nr - 1];
93 rtp->nodes[rtp->nr - 1] = NULL;
94 rtp->nr--;
97 return ret;
100 static inline void
101 radix_tree_node_free(struct radix_tree_node *node)
103 kmem_cache_free(radix_tree_node_cachep, node);
107 * Load up this CPU's radix_tree_node buffer with sufficient objects to
108 * ensure that the addition of a single element in the tree cannot fail. On
109 * success, return zero, with preemption disabled. On error, return -ENOMEM
110 * with preemption not disabled.
112 int radix_tree_preload(int gfp_mask)
114 struct radix_tree_preload *rtp;
115 struct radix_tree_node *node;
116 int ret = -ENOMEM;
118 preempt_disable();
119 rtp = &__get_cpu_var(radix_tree_preloads);
120 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
121 preempt_enable();
122 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
123 if (node == NULL)
124 goto out;
125 preempt_disable();
126 rtp = &__get_cpu_var(radix_tree_preloads);
127 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
128 rtp->nodes[rtp->nr++] = node;
129 else
130 kmem_cache_free(radix_tree_node_cachep, node);
132 ret = 0;
133 out:
134 return ret;
137 static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
139 if (!test_bit(offset, &node->tags[tag][0]))
140 __set_bit(offset, &node->tags[tag][0]);
143 static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
145 __clear_bit(offset, &node->tags[tag][0]);
148 static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
150 return test_bit(offset, &node->tags[tag][0]);
154 * Return the maximum key which can be store into a
155 * radix tree with height HEIGHT.
157 static inline unsigned long radix_tree_maxindex(unsigned int height)
159 return height_to_maxindex[height];
163 * Extend a radix tree so it can store key @index.
165 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
167 struct radix_tree_node *node;
168 unsigned int height;
169 char tags[RADIX_TREE_TAGS];
170 int tag;
172 /* Figure out what the height should be. */
173 height = root->height + 1;
174 while (index > radix_tree_maxindex(height))
175 height++;
177 if (root->rnode == NULL) {
178 root->height = height;
179 goto out;
183 * Prepare the tag status of the top-level node for propagation
184 * into the newly-pushed top-level node(s)
186 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
187 int idx;
189 tags[tag] = 0;
190 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
191 if (root->rnode->tags[tag][idx]) {
192 tags[tag] = 1;
193 break;
198 do {
199 if (!(node = radix_tree_node_alloc(root)))
200 return -ENOMEM;
202 /* Increase the height. */
203 node->slots[0] = root->rnode;
205 /* Propagate the aggregated tag info into the new root */
206 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
207 if (tags[tag])
208 tag_set(node, tag, 0);
211 node->count = 1;
212 root->rnode = node;
213 root->height++;
214 } while (height > root->height);
215 out:
216 return 0;
220 * radix_tree_insert - insert into a radix tree
221 * @root: radix tree root
222 * @index: index key
223 * @item: item to insert
225 * Insert an item into the radix tree at position @index.
227 int radix_tree_insert(struct radix_tree_root *root,
228 unsigned long index, void *item)
230 struct radix_tree_node *node = NULL, *tmp, **slot;
231 unsigned int height, shift;
232 int offset;
233 int error;
235 /* Make sure the tree is high enough. */
236 if ((!index && !root->rnode) ||
237 index > radix_tree_maxindex(root->height)) {
238 error = radix_tree_extend(root, index);
239 if (error)
240 return error;
243 slot = &root->rnode;
244 height = root->height;
245 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
247 offset = 0; /* uninitialised var warning */
248 while (height > 0) {
249 if (*slot == NULL) {
250 /* Have to add a child node. */
251 if (!(tmp = radix_tree_node_alloc(root)))
252 return -ENOMEM;
253 *slot = tmp;
254 if (node)
255 node->count++;
258 /* Go a level down */
259 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
260 node = *slot;
261 slot = (struct radix_tree_node **)(node->slots + offset);
262 shift -= RADIX_TREE_MAP_SHIFT;
263 height--;
266 if (*slot != NULL)
267 return -EEXIST;
268 if (node) {
269 node->count++;
270 BUG_ON(tag_get(node, 0, offset));
271 BUG_ON(tag_get(node, 1, offset));
274 *slot = item;
275 return 0;
277 EXPORT_SYMBOL(radix_tree_insert);
280 * radix_tree_lookup - perform lookup operation on a radix tree
281 * @root: radix tree root
282 * @index: index key
284 * Lookup the item at the position @index in the radix tree @root.
286 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
288 unsigned int height, shift;
289 struct radix_tree_node **slot;
291 height = root->height;
292 if (index > radix_tree_maxindex(height))
293 return NULL;
295 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
296 slot = &root->rnode;
298 while (height > 0) {
299 if (*slot == NULL)
300 return NULL;
302 slot = (struct radix_tree_node **)
303 ((*slot)->slots +
304 ((index >> shift) & RADIX_TREE_MAP_MASK));
305 shift -= RADIX_TREE_MAP_SHIFT;
306 height--;
309 return *slot;
311 EXPORT_SYMBOL(radix_tree_lookup);
314 * radix_tree_tag_set - set a tag on a radix tree node
315 * @root: radix tree root
316 * @index: index key
317 * @tag: tag index
319 * Set the search tag corresponging to @index in the radix tree. From
320 * the root all the way down to the leaf node.
322 * Returns the address of the tagged item. Setting a tag on a not-present
323 * item is a bug.
325 void *radix_tree_tag_set(struct radix_tree_root *root,
326 unsigned long index, int tag)
328 unsigned int height, shift;
329 struct radix_tree_node **slot;
331 height = root->height;
332 if (index > radix_tree_maxindex(height))
333 return NULL;
335 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
336 slot = &root->rnode;
338 while (height > 0) {
339 int offset;
341 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
342 tag_set(*slot, tag, offset);
343 slot = (struct radix_tree_node **)((*slot)->slots + offset);
344 BUG_ON(*slot == NULL);
345 shift -= RADIX_TREE_MAP_SHIFT;
346 height--;
349 return *slot;
351 EXPORT_SYMBOL(radix_tree_tag_set);
354 * radix_tree_tag_clear - clear a tag on a radix tree node
355 * @root: radix tree root
356 * @index: index key
357 * @tag: tag index
359 * Clear the search tag corresponging to @index in the radix tree. If
360 * this causes the leaf node to have no tags set then clear the tag in the
361 * next-to-leaf node, etc.
363 * Returns the address of the tagged item on success, else NULL. ie:
364 * has the same return value and semantics as radix_tree_lookup().
366 void *radix_tree_tag_clear(struct radix_tree_root *root,
367 unsigned long index, int tag)
369 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
370 unsigned int height, shift;
371 void *ret = NULL;
373 height = root->height;
374 if (index > radix_tree_maxindex(height))
375 goto out;
377 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
378 pathp->node = NULL;
379 pathp->slot = &root->rnode;
381 while (height > 0) {
382 int offset;
384 if (*pathp->slot == NULL)
385 goto out;
387 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
388 pathp[1].offset = offset;
389 pathp[1].node = *pathp[0].slot;
390 pathp[1].slot = (struct radix_tree_node **)
391 (pathp[1].node->slots + offset);
392 pathp++;
393 shift -= RADIX_TREE_MAP_SHIFT;
394 height--;
397 ret = *pathp[0].slot;
398 if (ret == NULL)
399 goto out;
401 do {
402 int idx;
404 tag_clear(pathp[0].node, tag, pathp[0].offset);
405 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
406 if (pathp[0].node->tags[tag][idx])
407 goto out;
409 pathp--;
410 } while (pathp[0].node);
411 out:
412 return ret;
414 EXPORT_SYMBOL(radix_tree_tag_clear);
416 #ifndef __KERNEL__ /* Only the test harness uses this at present */
418 * radix_tree_tag_get - get a tag on a radix tree node
419 * @root: radix tree root
420 * @index: index key
421 * @tag: tag index
423 * Return the search tag corresponging to @index in the radix tree.
425 * Returns zero if the tag is unset, or if there is no corresponding item
426 * in the tree.
428 int radix_tree_tag_get(struct radix_tree_root *root,
429 unsigned long index, int tag)
431 unsigned int height, shift;
432 struct radix_tree_node **slot;
433 int saw_unset_tag = 0;
435 height = root->height;
436 if (index > radix_tree_maxindex(height))
437 return 0;
439 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
440 slot = &root->rnode;
442 for ( ; ; ) {
443 int offset;
445 if (*slot == NULL)
446 return 0;
448 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
451 * This is just a debug check. Later, we can bale as soon as
452 * we see an unset tag.
454 if (!tag_get(*slot, tag, offset))
455 saw_unset_tag = 1;
456 if (height == 1) {
457 int ret = tag_get(*slot, tag, offset);
459 BUG_ON(ret && saw_unset_tag);
460 return ret;
462 slot = (struct radix_tree_node **)((*slot)->slots + offset);
463 shift -= RADIX_TREE_MAP_SHIFT;
464 height--;
467 EXPORT_SYMBOL(radix_tree_tag_get);
468 #endif
470 static unsigned int
471 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
472 unsigned int max_items, unsigned long *next_index)
474 unsigned int nr_found = 0;
475 unsigned int shift;
476 unsigned int height = root->height;
477 struct radix_tree_node *slot;
479 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
480 slot = root->rnode;
482 while (height > 0) {
483 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
485 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
486 if (slot->slots[i] != NULL)
487 break;
488 index &= ~((1UL << shift) - 1);
489 index += 1UL << shift;
490 if (index == 0)
491 goto out; /* 32-bit wraparound */
493 if (i == RADIX_TREE_MAP_SIZE)
494 goto out;
495 height--;
496 if (height == 0) { /* Bottom level: grab some items */
497 unsigned long j = index & RADIX_TREE_MAP_MASK;
499 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
500 index++;
501 if (slot->slots[j]) {
502 results[nr_found++] = slot->slots[j];
503 if (nr_found == max_items)
504 goto out;
508 shift -= RADIX_TREE_MAP_SHIFT;
509 slot = slot->slots[i];
511 out:
512 *next_index = index;
513 return nr_found;
517 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
518 * @root: radix tree root
519 * @results: where the results of the lookup are placed
520 * @first_index: start the lookup from this key
521 * @max_items: place up to this many items at *results
523 * Performs an index-ascending scan of the tree for present items. Places
524 * them at *@results and returns the number of items which were placed at
525 * *@results.
527 * The implementation is naive.
529 unsigned int
530 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
531 unsigned long first_index, unsigned int max_items)
533 const unsigned long max_index = radix_tree_maxindex(root->height);
534 unsigned long cur_index = first_index;
535 unsigned int ret = 0;
537 while (ret < max_items) {
538 unsigned int nr_found;
539 unsigned long next_index; /* Index of next search */
541 if (cur_index > max_index)
542 break;
543 nr_found = __lookup(root, results + ret, cur_index,
544 max_items - ret, &next_index);
545 ret += nr_found;
546 if (next_index == 0)
547 break;
548 cur_index = next_index;
550 return ret;
552 EXPORT_SYMBOL(radix_tree_gang_lookup);
555 * FIXME: the two tag_get()s here should use find_next_bit() instead of
556 * open-coding the search.
558 static unsigned int
559 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
560 unsigned int max_items, unsigned long *next_index, int tag)
562 unsigned int nr_found = 0;
563 unsigned int shift;
564 unsigned int height = root->height;
565 struct radix_tree_node *slot;
567 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
568 slot = root->rnode;
570 while (height > 0) {
571 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
573 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
574 if (tag_get(slot, tag, i)) {
575 BUG_ON(slot->slots[i] == NULL);
576 break;
578 index &= ~((1UL << shift) - 1);
579 index += 1UL << shift;
580 if (index == 0)
581 goto out; /* 32-bit wraparound */
583 if (i == RADIX_TREE_MAP_SIZE)
584 goto out;
585 height--;
586 if (height == 0) { /* Bottom level: grab some items */
587 unsigned long j = index & RADIX_TREE_MAP_MASK;
589 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
590 index++;
591 if (tag_get(slot, tag, j)) {
592 BUG_ON(slot->slots[j] == NULL);
593 results[nr_found++] = slot->slots[j];
594 if (nr_found == max_items)
595 goto out;
599 shift -= RADIX_TREE_MAP_SHIFT;
600 slot = slot->slots[i];
602 out:
603 *next_index = index;
604 return nr_found;
608 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
609 * based on a tag
610 * @root: radix tree root
611 * @results: where the results of the lookup are placed
612 * @first_index: start the lookup from this key
613 * @max_items: place up to this many items at *results
614 * @tag: the tag index
616 * Performs an index-ascending scan of the tree for present items which
617 * have the tag indexed by @tag set. Places the items at *@results and
618 * returns the number of items which were placed at *@results.
620 unsigned int
621 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
622 unsigned long first_index, unsigned int max_items, int tag)
624 const unsigned long max_index = radix_tree_maxindex(root->height);
625 unsigned long cur_index = first_index;
626 unsigned int ret = 0;
628 while (ret < max_items) {
629 unsigned int nr_found;
630 unsigned long next_index; /* Index of next search */
632 if (cur_index > max_index)
633 break;
634 nr_found = __lookup_tag(root, results + ret, cur_index,
635 max_items - ret, &next_index, tag);
636 ret += nr_found;
637 if (next_index == 0)
638 break;
639 cur_index = next_index;
641 return ret;
643 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
646 * radix_tree_delete - delete an item from a radix tree
647 * @root: radix tree root
648 * @index: index key
650 * Remove the item at @index from the radix tree rooted at @root.
652 * Returns the address of the deleted item, or NULL if it was not present.
654 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
656 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
657 struct radix_tree_path *orig_pathp;
658 unsigned int height, shift;
659 void *ret = NULL;
660 char tags[RADIX_TREE_TAGS];
661 int nr_cleared_tags;
663 height = root->height;
664 if (index > radix_tree_maxindex(height))
665 goto out;
667 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
668 pathp->node = NULL;
669 pathp->slot = &root->rnode;
671 while (height > 0) {
672 int offset;
674 if (*pathp->slot == NULL)
675 goto out;
677 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
678 pathp[1].offset = offset;
679 pathp[1].node = *pathp[0].slot;
680 pathp[1].slot = (struct radix_tree_node **)
681 (pathp[1].node->slots + offset);
682 pathp++;
683 shift -= RADIX_TREE_MAP_SHIFT;
684 height--;
687 ret = *pathp[0].slot;
688 if (ret == NULL)
689 goto out;
691 orig_pathp = pathp;
694 * Clear all tags associated with the just-deleted item
696 memset(tags, 0, sizeof(tags));
697 do {
698 int tag;
700 nr_cleared_tags = RADIX_TREE_TAGS;
701 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
702 int idx;
704 if (!tags[tag])
705 tag_clear(pathp[0].node, tag, pathp[0].offset);
707 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
708 if (pathp[0].node->tags[tag][idx]) {
709 tags[tag] = 1;
710 nr_cleared_tags--;
711 break;
715 pathp--;
716 } while (pathp[0].node && nr_cleared_tags);
718 pathp = orig_pathp;
719 *pathp[0].slot = NULL;
720 while (pathp[0].node && --pathp[0].node->count == 0) {
721 pathp--;
722 BUG_ON(*pathp[0].slot == NULL);
723 *pathp[0].slot = NULL;
724 radix_tree_node_free(pathp[1].node);
726 if (root->rnode == NULL)
727 root->height = 0;
728 out:
729 return ret;
731 EXPORT_SYMBOL(radix_tree_delete);
734 * radix_tree_tagged - test whether any items in the tree are tagged
735 * @root: radix tree root
736 * @tag: tag to test
738 int radix_tree_tagged(struct radix_tree_root *root, int tag)
740 int idx;
742 if (!root->rnode)
743 return 0;
744 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
745 if (root->rnode->tags[tag][idx])
746 return 1;
748 return 0;
750 EXPORT_SYMBOL(radix_tree_tagged);
752 static void
753 radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
755 memset(node, 0, sizeof(struct radix_tree_node));
758 static __init unsigned long __maxindex(unsigned int height)
760 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
761 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
763 if (tmp >= RADIX_TREE_INDEX_BITS)
764 index = ~0UL;
765 return index;
768 static __init void radix_tree_init_maxindex(void)
770 unsigned int i;
772 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
773 height_to_maxindex[i] = __maxindex(i);
776 #ifdef CONFIG_HOTPLUG_CPU
777 static int radix_tree_callback(struct notifier_block *nfb,
778 unsigned long action,
779 void *hcpu)
781 int cpu = (long)hcpu;
782 struct radix_tree_preload *rtp;
784 /* Free per-cpu pool of perloaded nodes */
785 if (action == CPU_DEAD) {
786 rtp = &per_cpu(radix_tree_preloads, cpu);
787 while (rtp->nr) {
788 kmem_cache_free(radix_tree_node_cachep,
789 rtp->nodes[rtp->nr-1]);
790 rtp->nodes[rtp->nr-1] = NULL;
791 rtp->nr--;
794 return NOTIFY_OK;
796 #endif /* CONFIG_HOTPLUG_CPU */
798 void __init radix_tree_init(void)
800 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
801 sizeof(struct radix_tree_node), 0,
802 SLAB_PANIC, radix_tree_node_ctor, NULL);
803 radix_tree_init_maxindex();
804 hotcpu_notifier(radix_tree_callback, 0);