]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/radix-tree.c
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "platform_defs.h"
25 #include "radix-tree.h"
28 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
31 #define RADIX_TREE_MAP_SHIFT 6
32 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
33 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
35 #ifdef RADIX_TREE_TAGS
36 #define RADIX_TREE_TAG_LONGS \
37 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
40 struct radix_tree_node
{
42 void *slots
[RADIX_TREE_MAP_SIZE
];
43 #ifdef RADIX_TREE_TAGS
44 unsigned long tags
[RADIX_TREE_MAX_TAGS
][RADIX_TREE_TAG_LONGS
];
48 struct radix_tree_path
{
49 struct radix_tree_node
*node
;
53 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
54 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
56 static unsigned long height_to_maxindex
[RADIX_TREE_MAX_PATH
];
59 * Radix tree node cache.
62 #define radix_tree_node_alloc(r) ((struct radix_tree_node *) \
63 calloc(1, sizeof(struct radix_tree_node)))
64 #define radix_tree_node_free(n) free(n)
66 #ifdef RADIX_TREE_TAGS
68 static inline void tag_set(struct radix_tree_node
*node
, unsigned int tag
,
71 *((uint32_t *)node
->tags
[tag
] + (offset
>> 5)) |= (1 << (offset
& 31));
74 static inline void tag_clear(struct radix_tree_node
*node
, unsigned int tag
,
77 uint32_t *p
= (uint32_t*)node
->tags
[tag
] + (offset
>> 5);
78 uint32_t m
= 1 << (offset
& 31);
82 static inline int tag_get(struct radix_tree_node
*node
, unsigned int tag
,
85 return 1 & (((const uint32_t *)node
->tags
[tag
])[offset
>> 5] >> (offset
& 31));
89 * Returns 1 if any slot in the node has this tag set.
90 * Otherwise returns 0.
92 static inline int any_tag_set(struct radix_tree_node
*node
, unsigned int tag
)
95 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
96 if (node
->tags
[tag
][idx
])
105 * Return the maximum key which can be store into a
106 * radix tree with height HEIGHT.
108 static inline unsigned long radix_tree_maxindex(unsigned int height
)
110 return height_to_maxindex
[height
];
114 * Extend a radix tree so it can store key @index.
116 static int radix_tree_extend(struct radix_tree_root
*root
, unsigned long index
)
118 struct radix_tree_node
*node
;
120 #ifdef RADIX_TREE_TAGS
121 char tags
[RADIX_TREE_MAX_TAGS
];
125 /* Figure out what the height should be. */
126 height
= root
->height
+ 1;
127 while (index
> radix_tree_maxindex(height
))
130 if (root
->rnode
== NULL
) {
131 root
->height
= height
;
135 #ifdef RADIX_TREE_TAGS
137 * Prepare the tag status of the top-level node for propagation
138 * into the newly-pushed top-level node(s)
140 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
142 if (any_tag_set(root
->rnode
, tag
))
147 if (!(node
= radix_tree_node_alloc(root
)))
150 /* Increase the height. */
151 node
->slots
[0] = root
->rnode
;
153 #ifdef RADIX_TREE_TAGS
154 /* Propagate the aggregated tag info into the new root */
155 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
157 tag_set(node
, tag
, 0);
163 } while (height
> root
->height
);
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
,
177 unsigned long index
, void *item
)
179 struct radix_tree_node
*node
= NULL
, *slot
;
180 unsigned int height
, shift
;
184 /* Make sure the tree is high enough. */
185 if ((!index
&& !root
->rnode
) ||
186 index
> radix_tree_maxindex(root
->height
)) {
187 error
= radix_tree_extend(root
, index
);
193 height
= root
->height
;
194 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
196 offset
= 0; /* uninitialised var warning */
199 /* Have to add a child node. */
200 if (!(slot
= radix_tree_node_alloc(root
)))
203 node
->slots
[offset
] = slot
;
209 /* Go a level down */
210 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
212 slot
= node
->slots
[offset
];
213 shift
-= RADIX_TREE_MAP_SHIFT
;
215 } while (height
> 0);
222 node
->slots
[offset
] = item
;
223 #ifdef RADIX_TREE_TAGS
224 ASSERT(!tag_get(node
, 0, offset
));
225 ASSERT(!tag_get(node
, 1, offset
));
230 static inline void **__lookup_slot(struct radix_tree_root
*root
,
233 unsigned int height
, shift
;
234 struct radix_tree_node
**slot
;
236 height
= root
->height
;
237 if (index
> radix_tree_maxindex(height
))
240 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
247 slot
= (struct radix_tree_node
**)
249 ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
250 shift
-= RADIX_TREE_MAP_SHIFT
;
254 return (void **)slot
;
258 * radix_tree_lookup_slot - lookup a slot in a radix tree
259 * @root: radix tree root
262 * Lookup the slot corresponding to the position @index in the radix tree
263 * @root. This is useful for update-if-exists operations.
265 void **radix_tree_lookup_slot(struct radix_tree_root
*root
, unsigned long index
)
267 return __lookup_slot(root
, index
);
271 * radix_tree_lookup - perform lookup operation on a radix tree
272 * @root: radix tree root
275 * Lookup the item at the position @index in the radix tree @root.
277 void *radix_tree_lookup(struct radix_tree_root
*root
, unsigned long index
)
281 slot
= __lookup_slot(root
, index
);
282 return slot
!= NULL
? *slot
: NULL
;
286 * raid_tree_first_key - find the first index key in the radix tree
287 * @root: radix tree root
288 * @index: where the first index will be placed
290 * Returns the first entry and index key in the radix tree @root.
292 void *radix_tree_lookup_first(struct radix_tree_root
*root
, unsigned long *index
)
294 unsigned int height
, shift
;
295 struct radix_tree_node
*slot
;
298 height
= root
->height
;
303 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
306 for (; height
> 1; height
--) {
307 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
308 if (slot
->slots
[i
] != NULL
)
311 ASSERT(i
< RADIX_TREE_MAP_SIZE
);
313 *index
|= (i
<< shift
);
314 shift
-= RADIX_TREE_MAP_SHIFT
;
315 slot
= slot
->slots
[i
];
317 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
318 if (slot
->slots
[i
] != NULL
) {
320 return slot
->slots
[i
];
326 #ifdef RADIX_TREE_TAGS
329 * radix_tree_tag_set - set a tag on a radix tree node
330 * @root: radix tree root
334 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
335 * corresponding to @index in the radix tree. From
336 * the root all the way down to the leaf node.
338 * Returns the address of the tagged item. Setting a tag on a not-present
341 void *radix_tree_tag_set(struct radix_tree_root
*root
,
342 unsigned long index
, unsigned int tag
)
344 unsigned int height
, shift
;
345 struct radix_tree_node
*slot
;
347 height
= root
->height
;
348 if (index
> radix_tree_maxindex(height
))
351 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
357 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
358 if (!tag_get(slot
, tag
, offset
))
359 tag_set(slot
, tag
, offset
);
360 slot
= slot
->slots
[offset
];
361 ASSERT(slot
!= NULL
);
362 shift
-= RADIX_TREE_MAP_SHIFT
;
370 * radix_tree_tag_clear - clear a tag on a radix tree node
371 * @root: radix tree root
375 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
376 * corresponding to @index in the radix tree. If
377 * this causes the leaf node to have no tags set then clear the tag in the
378 * next-to-leaf node, etc.
380 * Returns the address of the tagged item on success, else NULL. ie:
381 * has the same return value and semantics as radix_tree_lookup().
383 void *radix_tree_tag_clear(struct radix_tree_root
*root
,
384 unsigned long index
, unsigned int tag
)
386 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
387 struct radix_tree_node
*slot
;
388 unsigned int height
, shift
;
391 height
= root
->height
;
392 if (index
> radix_tree_maxindex(height
))
395 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
405 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
406 pathp
[1].offset
= offset
;
407 pathp
[1].node
= slot
;
408 slot
= slot
->slots
[offset
];
410 shift
-= RADIX_TREE_MAP_SHIFT
;
419 if (!tag_get(pathp
->node
, tag
, pathp
->offset
))
421 tag_clear(pathp
->node
, tag
, pathp
->offset
);
422 if (any_tag_set(pathp
->node
, tag
))
425 } while (pathp
->node
);
433 __lookup(struct radix_tree_root
*root
, void **results
, unsigned long index
,
434 unsigned int max_items
, unsigned long *next_index
)
436 unsigned int nr_found
= 0;
437 unsigned int shift
, height
;
438 struct radix_tree_node
*slot
;
441 height
= root
->height
;
445 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
448 for ( ; height
> 1; height
--) {
450 for (i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
451 i
< RADIX_TREE_MAP_SIZE
; i
++) {
452 if (slot
->slots
[i
] != NULL
)
454 index
&= ~((1UL << shift
) - 1);
455 index
+= 1UL << shift
;
457 goto out
; /* 32-bit wraparound */
459 if (i
== RADIX_TREE_MAP_SIZE
)
462 shift
-= RADIX_TREE_MAP_SHIFT
;
463 slot
= slot
->slots
[i
];
466 /* Bottom level: grab some items */
467 for (i
= index
& RADIX_TREE_MAP_MASK
; i
< RADIX_TREE_MAP_SIZE
; i
++) {
469 if (slot
->slots
[i
]) {
470 results
[nr_found
++] = slot
->slots
[i
];
471 if (nr_found
== max_items
)
481 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
482 * @root: radix tree root
483 * @results: where the results of the lookup are placed
484 * @first_index: start the lookup from this key
485 * @max_items: place up to this many items at *results
487 * Performs an index-ascending scan of the tree for present items. Places
488 * them at *@results and returns the number of items which were placed at
491 * The implementation is naive.
494 radix_tree_gang_lookup(struct radix_tree_root
*root
, void **results
,
495 unsigned long first_index
, unsigned int max_items
)
497 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
498 unsigned long cur_index
= first_index
;
499 unsigned int ret
= 0;
501 while (ret
< max_items
) {
502 unsigned int nr_found
;
503 unsigned long next_index
; /* Index of next search */
505 if (cur_index
> max_index
)
507 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
508 max_items
- ret
, &next_index
);
512 cur_index
= next_index
;
518 * radix_tree_gang_lookup_ex - perform multiple lookup on a radix tree
519 * @root: radix tree root
520 * @results: where the results of the lookup are placed
521 * @first_index: start the lookup from this key
522 * @last_index: don't lookup past this key
523 * @max_items: place up to this many items at *results
525 * Performs an index-ascending scan of the tree for present items starting
526 * @first_index until @last_index up to as many as @max_items. Places
527 * them at *@results and returns the number of items which were placed
530 * The implementation is naive.
533 radix_tree_gang_lookup_ex(struct radix_tree_root
*root
, void **results
,
534 unsigned long first_index
, unsigned long last_index
,
535 unsigned int max_items
)
537 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
538 unsigned long cur_index
= first_index
;
539 unsigned int ret
= 0;
541 while (ret
< max_items
&& cur_index
< last_index
) {
542 unsigned int nr_found
;
543 unsigned long next_index
; /* Index of next search */
545 if (cur_index
> max_index
)
547 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
548 max_items
- ret
, &next_index
);
552 cur_index
= next_index
;
557 #ifdef RADIX_TREE_TAGS
560 __lookup_tag(struct radix_tree_root
*root
, void **results
, unsigned long index
,
561 unsigned int max_items
, unsigned long *next_index
, unsigned int tag
)
563 unsigned int nr_found
= 0;
565 unsigned int height
= root
->height
;
566 struct radix_tree_node
*slot
;
568 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
572 unsigned long i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
574 for ( ; i
< RADIX_TREE_MAP_SIZE
; i
++) {
575 if (tag_get(slot
, tag
, i
)) {
576 ASSERT(slot
->slots
[i
] != NULL
);
579 index
&= ~((1UL << shift
) - 1);
580 index
+= 1UL << shift
;
582 goto out
; /* 32-bit wraparound */
584 if (i
== RADIX_TREE_MAP_SIZE
)
587 if (height
== 0) { /* Bottom level: grab some items */
588 unsigned long j
= index
& RADIX_TREE_MAP_MASK
;
590 for ( ; j
< RADIX_TREE_MAP_SIZE
; j
++) {
592 if (tag_get(slot
, tag
, j
)) {
593 ASSERT(slot
->slots
[j
] != NULL
);
594 results
[nr_found
++] = slot
->slots
[j
];
595 if (nr_found
== max_items
)
600 shift
-= RADIX_TREE_MAP_SHIFT
;
601 slot
= slot
->slots
[i
];
609 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
611 * @root: radix tree root
612 * @results: where the results of the lookup are placed
613 * @first_index: start the lookup from this key
614 * @max_items: place up to this many items at *results
615 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
617 * Performs an index-ascending scan of the tree for present items which
618 * have the tag indexed by @tag set. Places the items at *@results and
619 * returns the number of items which were placed at *@results.
622 radix_tree_gang_lookup_tag(struct radix_tree_root
*root
, void **results
,
623 unsigned long first_index
, unsigned int max_items
,
626 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
627 unsigned long cur_index
= first_index
;
628 unsigned int ret
= 0;
630 while (ret
< max_items
) {
631 unsigned int nr_found
;
632 unsigned long next_index
; /* Index of next search */
634 if (cur_index
> max_index
)
636 nr_found
= __lookup_tag(root
, results
+ ret
, cur_index
,
637 max_items
- ret
, &next_index
, tag
);
641 cur_index
= next_index
;
649 * radix_tree_shrink - shrink height of a radix tree to minimal
650 * @root radix tree root
652 static inline void radix_tree_shrink(struct radix_tree_root
*root
)
654 /* try to shrink tree height */
655 while (root
->height
> 1 &&
656 root
->rnode
->count
== 1 &&
657 root
->rnode
->slots
[0]) {
658 struct radix_tree_node
*to_free
= root
->rnode
;
660 root
->rnode
= to_free
->slots
[0];
662 /* must only free zeroed nodes into the slab */
663 #ifdef RADIX_TREE_TAGS
664 tag_clear(to_free
, 0, 0);
665 tag_clear(to_free
, 1, 0);
667 to_free
->slots
[0] = NULL
;
669 radix_tree_node_free(to_free
);
674 * radix_tree_delete - delete an item from a radix tree
675 * @root: radix tree root
678 * Remove the item at @index from the radix tree rooted at @root.
680 * Returns the address of the deleted item, or NULL if it was not present.
682 void *radix_tree_delete(struct radix_tree_root
*root
, unsigned long index
)
684 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
685 struct radix_tree_path
*orig_pathp
;
686 struct radix_tree_node
*slot
;
687 unsigned int height
, shift
;
689 #ifdef RADIX_TREE_TAGS
690 char tags
[RADIX_TREE_MAX_TAGS
];
696 height
= root
->height
;
697 if (index
> radix_tree_maxindex(height
))
700 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
704 for ( ; height
> 0; height
--) {
709 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
710 pathp
->offset
= offset
;
712 slot
= slot
->slots
[offset
];
713 shift
-= RADIX_TREE_MAP_SHIFT
;
722 #ifdef RADIX_TREE_TAGS
724 * Clear all tags associated with the just-deleted item
727 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
729 if (tag_get(pathp
->node
, tag
, pathp
->offset
)) {
730 tag_clear(pathp
->node
, tag
, pathp
->offset
);
731 if (!any_tag_set(pathp
->node
, tag
)) {
738 for (pathp
--; nr_cleared_tags
&& pathp
->node
; pathp
--) {
739 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
743 tag_clear(pathp
->node
, tag
, pathp
->offset
);
744 if (any_tag_set(pathp
->node
, tag
)) {
751 /* Now free the nodes we do not need anymore */
752 for (pathp
= orig_pathp
; pathp
->node
; pathp
--) {
753 pathp
->node
->slots
[pathp
->offset
] = NULL
;
754 pathp
->node
->count
--;
756 if (pathp
->node
->count
) {
757 if (pathp
->node
== root
->rnode
)
758 radix_tree_shrink(root
);
762 /* Node with zero slots in use so free it */
763 radix_tree_node_free(pathp
->node
);
771 #ifdef RADIX_TREE_TAGS
773 * radix_tree_tagged - test whether any items in the tree are tagged
774 * @root: radix tree root
777 int radix_tree_tagged(struct radix_tree_root
*root
, unsigned int tag
)
779 struct radix_tree_node
*rnode
;
783 return any_tag_set(rnode
, tag
);
787 static unsigned long __maxindex(unsigned int height
)
789 unsigned int width
= height
* RADIX_TREE_MAP_SHIFT
;
790 int shift
= RADIX_TREE_INDEX_BITS
- width
;
794 if (shift
>= BITS_PER_LONG
)
796 return ~0UL >> shift
;
799 static void radix_tree_init_maxindex(void)
803 for (i
= 0; i
< ARRAY_SIZE(height_to_maxindex
); i
++)
804 height_to_maxindex
[i
] = __maxindex(i
);
807 void radix_tree_init(void)
809 radix_tree_init_maxindex();