]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/radix-tree.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2001 Momchil Velikov
4 * Portions Copyright (C) 2001 Christoph Hellwig
5 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
11 #include "platform_defs.h"
12 #include "radix-tree.h"
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18 #define RADIX_TREE_MAP_SHIFT 6
19 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
20 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
22 #ifdef RADIX_TREE_TAGS
23 #define RADIX_TREE_TAG_LONGS \
24 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
27 struct radix_tree_node
{
29 void *slots
[RADIX_TREE_MAP_SIZE
];
30 #ifdef RADIX_TREE_TAGS
31 unsigned long tags
[RADIX_TREE_MAX_TAGS
][RADIX_TREE_TAG_LONGS
];
35 struct radix_tree_path
{
36 struct radix_tree_node
*node
;
40 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
41 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
43 static unsigned long height_to_maxindex
[RADIX_TREE_MAX_PATH
];
46 * Radix tree node cache.
49 #define radix_tree_node_alloc(r) ((struct radix_tree_node *) \
50 calloc(1, sizeof(struct radix_tree_node)))
51 #define radix_tree_node_free(n) free(n)
53 #ifdef RADIX_TREE_TAGS
55 static inline void tag_set(struct radix_tree_node
*node
, unsigned int tag
,
58 *((uint32_t *)node
->tags
[tag
] + (offset
>> 5)) |= (1 << (offset
& 31));
61 static inline void tag_clear(struct radix_tree_node
*node
, unsigned int tag
,
64 uint32_t *p
= (uint32_t*)node
->tags
[tag
] + (offset
>> 5);
65 uint32_t m
= 1 << (offset
& 31);
69 static inline int tag_get(struct radix_tree_node
*node
, unsigned int tag
,
72 return 1 & (((const uint32_t *)node
->tags
[tag
])[offset
>> 5] >> (offset
& 31));
76 * Returns 1 if any slot in the node has this tag set.
77 * Otherwise returns 0.
79 static inline int any_tag_set(struct radix_tree_node
*node
, unsigned int tag
)
82 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
83 if (node
->tags
[tag
][idx
])
92 * Return the maximum key which can be store into a
93 * radix tree with height HEIGHT.
95 static inline unsigned long radix_tree_maxindex(unsigned int height
)
97 return height_to_maxindex
[height
];
101 * Extend a radix tree so it can store key @index.
103 static int radix_tree_extend(struct radix_tree_root
*root
, unsigned long index
)
105 struct radix_tree_node
*node
;
107 #ifdef RADIX_TREE_TAGS
108 char tags
[RADIX_TREE_MAX_TAGS
];
112 /* Figure out what the height should be. */
113 height
= root
->height
+ 1;
114 while (index
> radix_tree_maxindex(height
))
117 if (root
->rnode
== NULL
) {
118 root
->height
= height
;
122 #ifdef RADIX_TREE_TAGS
124 * Prepare the tag status of the top-level node for propagation
125 * into the newly-pushed top-level node(s)
127 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
129 if (any_tag_set(root
->rnode
, tag
))
134 if (!(node
= radix_tree_node_alloc(root
)))
137 /* Increase the height. */
138 node
->slots
[0] = root
->rnode
;
140 #ifdef RADIX_TREE_TAGS
141 /* Propagate the aggregated tag info into the new root */
142 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
144 tag_set(node
, tag
, 0);
150 } while (height
> root
->height
);
156 * radix_tree_insert - insert into a radix tree
157 * @root: radix tree root
159 * @item: item to insert
161 * Insert an item into the radix tree at position @index.
163 int radix_tree_insert(struct radix_tree_root
*root
,
164 unsigned long index
, void *item
)
166 struct radix_tree_node
*node
= NULL
, *slot
;
167 unsigned int height
, shift
;
171 /* Make sure the tree is high enough. */
172 if ((!index
&& !root
->rnode
) ||
173 index
> radix_tree_maxindex(root
->height
)) {
174 error
= radix_tree_extend(root
, index
);
180 height
= root
->height
;
181 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
183 offset
= 0; /* uninitialised var warning */
186 /* Have to add a child node. */
187 if (!(slot
= radix_tree_node_alloc(root
)))
190 node
->slots
[offset
] = slot
;
196 /* Go a level down */
197 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
199 slot
= node
->slots
[offset
];
200 shift
-= RADIX_TREE_MAP_SHIFT
;
202 } while (height
> 0);
209 node
->slots
[offset
] = item
;
210 #ifdef RADIX_TREE_TAGS
211 ASSERT(!tag_get(node
, 0, offset
));
212 ASSERT(!tag_get(node
, 1, offset
));
217 static inline void **__lookup_slot(struct radix_tree_root
*root
,
220 unsigned int height
, shift
;
221 struct radix_tree_node
**slot
;
223 height
= root
->height
;
224 if (index
> radix_tree_maxindex(height
))
227 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
234 slot
= (struct radix_tree_node
**)
236 ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
237 shift
-= RADIX_TREE_MAP_SHIFT
;
241 return (void **)slot
;
245 * radix_tree_lookup_slot - lookup a slot in a radix tree
246 * @root: radix tree root
249 * Lookup the slot corresponding to the position @index in the radix tree
250 * @root. This is useful for update-if-exists operations.
252 void **radix_tree_lookup_slot(struct radix_tree_root
*root
, unsigned long index
)
254 return __lookup_slot(root
, index
);
258 * radix_tree_lookup - perform lookup operation on a radix tree
259 * @root: radix tree root
262 * Lookup the item at the position @index in the radix tree @root.
264 void *radix_tree_lookup(struct radix_tree_root
*root
, unsigned long index
)
268 slot
= __lookup_slot(root
, index
);
269 return slot
!= NULL
? *slot
: NULL
;
273 * raid_tree_first_key - find the first index key in the radix tree
274 * @root: radix tree root
275 * @index: where the first index will be placed
277 * Returns the first entry and index key in the radix tree @root.
279 void *radix_tree_lookup_first(struct radix_tree_root
*root
, unsigned long *index
)
281 unsigned int height
, shift
;
282 struct radix_tree_node
*slot
;
285 height
= root
->height
;
290 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
293 for (; height
> 1; height
--) {
294 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
295 if (slot
->slots
[i
] != NULL
)
298 ASSERT(i
< RADIX_TREE_MAP_SIZE
);
300 *index
|= (i
<< shift
);
301 shift
-= RADIX_TREE_MAP_SHIFT
;
302 slot
= slot
->slots
[i
];
304 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
305 if (slot
->slots
[i
] != NULL
) {
307 return slot
->slots
[i
];
313 #ifdef RADIX_TREE_TAGS
316 * radix_tree_tag_set - set a tag on a radix tree node
317 * @root: radix tree root
321 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
322 * corresponding to @index in the radix tree. From
323 * the root all the way down to the leaf node.
325 * Returns the address of the tagged item. Setting a tag on a not-present
328 void *radix_tree_tag_set(struct radix_tree_root
*root
,
329 unsigned long index
, unsigned int tag
)
331 unsigned int height
, shift
;
332 struct radix_tree_node
*slot
;
334 height
= root
->height
;
335 if (index
> radix_tree_maxindex(height
))
338 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
344 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
345 if (!tag_get(slot
, tag
, offset
))
346 tag_set(slot
, tag
, offset
);
347 slot
= slot
->slots
[offset
];
348 ASSERT(slot
!= NULL
);
349 shift
-= RADIX_TREE_MAP_SHIFT
;
357 * radix_tree_tag_clear - clear a tag on a radix tree node
358 * @root: radix tree root
362 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
363 * corresponding to @index in the radix tree. If
364 * this causes the leaf node to have no tags set then clear the tag in the
365 * next-to-leaf node, etc.
367 * Returns the address of the tagged item on success, else NULL. ie:
368 * has the same return value and semantics as radix_tree_lookup().
370 void *radix_tree_tag_clear(struct radix_tree_root
*root
,
371 unsigned long index
, unsigned int tag
)
373 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
374 struct radix_tree_node
*slot
;
375 unsigned int height
, shift
;
378 height
= root
->height
;
379 if (index
> radix_tree_maxindex(height
))
382 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
392 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
393 pathp
[1].offset
= offset
;
394 pathp
[1].node
= slot
;
395 slot
= slot
->slots
[offset
];
397 shift
-= RADIX_TREE_MAP_SHIFT
;
406 if (!tag_get(pathp
->node
, tag
, pathp
->offset
))
408 tag_clear(pathp
->node
, tag
, pathp
->offset
);
409 if (any_tag_set(pathp
->node
, tag
))
412 } while (pathp
->node
);
420 __lookup(struct radix_tree_root
*root
, void **results
, unsigned long index
,
421 unsigned int max_items
, unsigned long *next_index
)
423 unsigned int nr_found
= 0;
424 unsigned int shift
, height
;
425 struct radix_tree_node
*slot
;
428 height
= root
->height
;
432 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
435 for ( ; height
> 1; height
--) {
437 for (i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
438 i
< RADIX_TREE_MAP_SIZE
; i
++) {
439 if (slot
->slots
[i
] != NULL
)
441 index
&= ~((1UL << shift
) - 1);
442 index
+= 1UL << shift
;
444 goto out
; /* 32-bit wraparound */
446 if (i
== RADIX_TREE_MAP_SIZE
)
449 shift
-= RADIX_TREE_MAP_SHIFT
;
450 slot
= slot
->slots
[i
];
453 /* Bottom level: grab some items */
454 for (i
= index
& RADIX_TREE_MAP_MASK
; i
< RADIX_TREE_MAP_SIZE
; i
++) {
456 if (slot
->slots
[i
]) {
457 results
[nr_found
++] = slot
->slots
[i
];
458 if (nr_found
== max_items
)
468 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
469 * @root: radix tree root
470 * @results: where the results of the lookup are placed
471 * @first_index: start the lookup from this key
472 * @max_items: place up to this many items at *results
474 * Performs an index-ascending scan of the tree for present items. Places
475 * them at *@results and returns the number of items which were placed at
478 * The implementation is naive.
481 radix_tree_gang_lookup(struct radix_tree_root
*root
, void **results
,
482 unsigned long first_index
, unsigned int max_items
)
484 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
485 unsigned long cur_index
= first_index
;
486 unsigned int ret
= 0;
488 while (ret
< max_items
) {
489 unsigned int nr_found
;
490 unsigned long next_index
; /* Index of next search */
492 if (cur_index
> max_index
)
494 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
495 max_items
- ret
, &next_index
);
499 cur_index
= next_index
;
505 * radix_tree_gang_lookup_ex - perform multiple lookup on a radix tree
506 * @root: radix tree root
507 * @results: where the results of the lookup are placed
508 * @first_index: start the lookup from this key
509 * @last_index: don't lookup past this key
510 * @max_items: place up to this many items at *results
512 * Performs an index-ascending scan of the tree for present items starting
513 * @first_index until @last_index up to as many as @max_items. Places
514 * them at *@results and returns the number of items which were placed
517 * The implementation is naive.
520 radix_tree_gang_lookup_ex(struct radix_tree_root
*root
, void **results
,
521 unsigned long first_index
, unsigned long last_index
,
522 unsigned int max_items
)
524 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
525 unsigned long cur_index
= first_index
;
526 unsigned int ret
= 0;
528 while (ret
< max_items
&& cur_index
< last_index
) {
529 unsigned int nr_found
;
530 unsigned long next_index
; /* Index of next search */
532 if (cur_index
> max_index
)
534 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
535 max_items
- ret
, &next_index
);
539 cur_index
= next_index
;
544 #ifdef RADIX_TREE_TAGS
547 __lookup_tag(struct radix_tree_root
*root
, void **results
, unsigned long index
,
548 unsigned int max_items
, unsigned long *next_index
, unsigned int tag
)
550 unsigned int nr_found
= 0;
552 unsigned int height
= root
->height
;
553 struct radix_tree_node
*slot
;
555 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
559 unsigned long i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
561 for ( ; i
< RADIX_TREE_MAP_SIZE
; i
++) {
562 if (tag_get(slot
, tag
, i
)) {
563 ASSERT(slot
->slots
[i
] != NULL
);
566 index
&= ~((1UL << shift
) - 1);
567 index
+= 1UL << shift
;
569 goto out
; /* 32-bit wraparound */
571 if (i
== RADIX_TREE_MAP_SIZE
)
574 if (height
== 0) { /* Bottom level: grab some items */
575 unsigned long j
= index
& RADIX_TREE_MAP_MASK
;
577 for ( ; j
< RADIX_TREE_MAP_SIZE
; j
++) {
579 if (tag_get(slot
, tag
, j
)) {
580 ASSERT(slot
->slots
[j
] != NULL
);
581 results
[nr_found
++] = slot
->slots
[j
];
582 if (nr_found
== max_items
)
587 shift
-= RADIX_TREE_MAP_SHIFT
;
588 slot
= slot
->slots
[i
];
596 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
598 * @root: radix tree root
599 * @results: where the results of the lookup are placed
600 * @first_index: start the lookup from this key
601 * @max_items: place up to this many items at *results
602 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
604 * Performs an index-ascending scan of the tree for present items which
605 * have the tag indexed by @tag set. Places the items at *@results and
606 * returns the number of items which were placed at *@results.
609 radix_tree_gang_lookup_tag(struct radix_tree_root
*root
, void **results
,
610 unsigned long first_index
, unsigned int max_items
,
613 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
614 unsigned long cur_index
= first_index
;
615 unsigned int ret
= 0;
617 while (ret
< max_items
) {
618 unsigned int nr_found
;
619 unsigned long next_index
; /* Index of next search */
621 if (cur_index
> max_index
)
623 nr_found
= __lookup_tag(root
, results
+ ret
, cur_index
,
624 max_items
- ret
, &next_index
, tag
);
628 cur_index
= next_index
;
636 * radix_tree_shrink - shrink height of a radix tree to minimal
637 * @root radix tree root
639 static inline void radix_tree_shrink(struct radix_tree_root
*root
)
641 /* try to shrink tree height */
642 while (root
->height
> 1 &&
643 root
->rnode
->count
== 1 &&
644 root
->rnode
->slots
[0]) {
645 struct radix_tree_node
*to_free
= root
->rnode
;
647 root
->rnode
= to_free
->slots
[0];
649 /* must only free zeroed nodes into the slab */
650 #ifdef RADIX_TREE_TAGS
651 tag_clear(to_free
, 0, 0);
652 tag_clear(to_free
, 1, 0);
654 to_free
->slots
[0] = NULL
;
656 radix_tree_node_free(to_free
);
661 * radix_tree_delete - delete an item from a radix tree
662 * @root: radix tree root
665 * Remove the item at @index from the radix tree rooted at @root.
667 * Returns the address of the deleted item, or NULL if it was not present.
669 void *radix_tree_delete(struct radix_tree_root
*root
, unsigned long index
)
671 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
672 struct radix_tree_path
*orig_pathp
;
673 struct radix_tree_node
*slot
;
674 unsigned int height
, shift
;
676 #ifdef RADIX_TREE_TAGS
677 char tags
[RADIX_TREE_MAX_TAGS
];
683 height
= root
->height
;
684 if (index
> radix_tree_maxindex(height
))
687 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
691 for ( ; height
> 0; height
--) {
696 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
697 pathp
->offset
= offset
;
699 slot
= slot
->slots
[offset
];
700 shift
-= RADIX_TREE_MAP_SHIFT
;
709 #ifdef RADIX_TREE_TAGS
711 * Clear all tags associated with the just-deleted item
714 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
716 if (tag_get(pathp
->node
, tag
, pathp
->offset
)) {
717 tag_clear(pathp
->node
, tag
, pathp
->offset
);
718 if (!any_tag_set(pathp
->node
, tag
)) {
725 for (pathp
--; nr_cleared_tags
&& pathp
->node
; pathp
--) {
726 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
730 tag_clear(pathp
->node
, tag
, pathp
->offset
);
731 if (any_tag_set(pathp
->node
, tag
)) {
738 /* Now free the nodes we do not need anymore */
739 for (pathp
= orig_pathp
; pathp
->node
; pathp
--) {
740 pathp
->node
->slots
[pathp
->offset
] = NULL
;
741 pathp
->node
->count
--;
743 if (pathp
->node
->count
) {
744 if (pathp
->node
== root
->rnode
)
745 radix_tree_shrink(root
);
749 /* Node with zero slots in use so free it */
750 radix_tree_node_free(pathp
->node
);
758 #ifdef RADIX_TREE_TAGS
760 * radix_tree_tagged - test whether any items in the tree are tagged
761 * @root: radix tree root
764 int radix_tree_tagged(struct radix_tree_root
*root
, unsigned int tag
)
766 struct radix_tree_node
*rnode
;
770 return any_tag_set(rnode
, tag
);
774 static unsigned long __maxindex(unsigned int height
)
776 unsigned int width
= height
* RADIX_TREE_MAP_SHIFT
;
777 int shift
= RADIX_TREE_INDEX_BITS
- width
;
781 if (shift
>= BITS_PER_LONG
)
783 return ~0UL >> shift
;
786 static void radix_tree_init_maxindex(void)
790 for (i
= 0; i
< ARRAY_SIZE(height_to_maxindex
); i
++)
791 height_to_maxindex
[i
] = __maxindex(i
);
794 void radix_tree_init(void)
796 radix_tree_init_maxindex();