1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2018, Google LLC.
6 * Copyright (C) 2018, Red Hat, Inc. (code style cleanup and fuzzing driver)
8 * This library provides functions to support a memory efficient bit array,
9 * with an index size of 2^64. A sparsebit array is allocated through
10 * the use sparsebit_alloc() and free'd via sparsebit_free(),
11 * such as in the following:
13 * struct sparsebit *s;
14 * s = sparsebit_alloc();
17 * The struct sparsebit type resolves down to a struct sparsebit.
18 * Note that, sparsebit_free() takes a pointer to the sparsebit
19 * structure. This is so that sparsebit_free() is able to poison
20 * the pointer (e.g. set it to NULL) to the struct sparsebit before
21 * returning to the caller.
23 * Between the return of sparsebit_alloc() and the call of
24 * sparsebit_free(), there are multiple query and modifying operations
25 * that can be performed on the allocated sparsebit array. All of
26 * these operations take as a parameter the value returned from
27 * sparsebit_alloc() and most also take a bit index. Frequently
28 * used routines include:
30 * ---- Query Operations
31 * sparsebit_is_set(s, idx)
32 * sparsebit_is_clear(s, idx)
33 * sparsebit_any_set(s)
34 * sparsebit_first_set(s)
35 * sparsebit_next_set(s, prev_idx)
37 * ---- Modifying Operations
38 * sparsebit_set(s, idx)
39 * sparsebit_clear(s, idx)
40 * sparsebit_set_num(s, idx, num);
41 * sparsebit_clear_num(s, idx, num);
43 * A common operation, is to itterate over all the bits set in a test
44 * sparsebit array. This can be done via code with the following structure:
46 * sparsebit_idx_t idx;
47 * if (sparsebit_any_set(s)) {
48 * idx = sparsebit_first_set(s);
51 * idx = sparsebit_next_set(s, idx);
55 * The index of the first bit set needs to be obtained via
56 * sparsebit_first_set(), because sparsebit_next_set(), needs
57 * the index of the previously set. The sparsebit_idx_t type is
58 * unsigned, so there is no previous index before 0 that is available.
59 * Also, the call to sparsebit_first_set() is not made unless there
60 * is at least 1 bit in the array set. This is because sparsebit_first_set()
61 * aborts if sparsebit_first_set() is called with no bits set.
62 * It is the callers responsibility to assure that the
63 * sparsebit array has at least a single bit set before calling
64 * sparsebit_first_set().
66 * ==== Implementation Overview ====
67 * For the most part the internal implementation of sparsebit is
68 * opaque to the caller. One important implementation detail that the
69 * caller may need to be aware of is the spatial complexity of the
70 * implementation. This implementation of a sparsebit array is not
71 * only sparse, in that it uses memory proportional to the number of bits
72 * set. It is also efficient in memory usage when most of the bits are
75 * At a high-level the state of the bit settings are maintained through
76 * the use of a binary-search tree, where each node contains at least
77 * the following members:
79 * typedef uint64_t sparsebit_idx_t;
80 * typedef uint64_t sparsebit_num_t;
82 * sparsebit_idx_t idx;
84 * sparsebit_num_t num_after;
86 * The idx member contains the bit index of the first bit described by this
87 * node, while the mask member stores the setting of the first 32-bits.
88 * The setting of the bit at idx + n, where 0 <= n < 32, is located in the
89 * mask member at 1 << n.
91 * Nodes are sorted by idx and the bits described by two nodes will never
92 * overlap. The idx member is always aligned to the mask size, i.e. a
95 * Beyond a typical implementation, the nodes in this implementation also
96 * contains a member named num_after. The num_after member holds the
97 * number of bits immediately after the mask bits that are contiguously set.
98 * The use of the num_after member allows this implementation to efficiently
99 * represent cases where most bits are set. For example, the case of all
100 * but the last two bits set, is represented by the following two nodes:
102 * node 0 - idx: 0x0 mask: 0xffffffff num_after: 0xffffffffffffffc0
103 * node 1 - idx: 0xffffffffffffffe0 mask: 0x3fffffff num_after: 0
105 * ==== Invariants ====
106 * This implementation usses the following invariants:
108 * + Node are only used to represent bits that are set.
109 * Nodes with a mask of 0 and num_after of 0 are not allowed.
111 * + Sum of bits set in all the nodes is equal to the value of
112 * the struct sparsebit_pvt num_set member.
114 * + The setting of at least one bit is always described in a nodes
117 * + A node with all mask bits set only occurs when the last bit
118 * described by the previous node is not equal to this nodes
119 * starting index - 1. All such occurences of this condition are
120 * avoided by moving the setting of the nodes mask bits into
121 * the previous nodes num_after setting.
123 * + Node starting index is evenly divisible by the number of bits
124 * within a nodes mask member.
126 * + Nodes never represent a range of bits that wrap around the
127 * highest supported index.
129 * (idx + MASK_BITS + num_after - 1) <= ((sparsebit_idx_t) 0) - 1)
131 * As a consequence of the above, the num_after member of a node
134 * maximum_index - nodes_starting_index - number_of_mask_bits
136 * + Nodes within the binary search tree are sorted based on each
137 * nodes starting index.
139 * + The range of bits described by any two nodes do not overlap. The
140 * range of bits described by a single node is:
143 * end (inclusive): node->idx + MASK_BITS + node->num_after - 1;
145 * Note, at times these invariants are temporarily violated for a
146 * specific portion of the code. For example, when setting a mask
147 * bit, there is a small delay between when the mask bit is set and the
148 * value in the struct sparsebit_pvt num_set member is updated. Other
149 * temporary violations occur when node_split() is called with a specified
150 * index and assures that a node where its mask represents the bit
151 * at the specified index exists. At times to do this node_split()
152 * must split an existing node into two nodes or create a node that
153 * has no bits set. Such temporary violations must be corrected before
154 * returning to the caller. These corrections are typically performed
155 * by the local function node_reduce().
158 #include "test_util.h"
159 #include "sparsebit.h"
163 #define DUMP_LINE_MAX 100 /* Does not include indent amount */
165 typedef uint32_t mask_t
;
166 #define MASK_BITS (sizeof(mask_t) * CHAR_BIT)
172 sparsebit_idx_t idx
; /* index of least-significant bit in mask */
173 sparsebit_num_t num_after
; /* num contiguously set after mask */
179 * Points to root node of the binary search
180 * tree. Equal to NULL when no bits are set in
181 * the entire sparsebit array.
186 * A redundant count of the total number of bits set. Used for
187 * diagnostic purposes and to change the time complexity of
188 * sparsebit_num_set() from O(n) to O(1).
189 * Note: Due to overflow, a value of 0 means none or all set.
191 sparsebit_num_t num_set
;
194 /* Returns the number of set bits described by the settings
195 * of the node pointed to by nodep.
197 static sparsebit_num_t
node_num_set(struct node
*nodep
)
199 return nodep
->num_after
+ __builtin_popcount(nodep
->mask
);
202 /* Returns a pointer to the node that describes the
205 static struct node
*node_first(const struct sparsebit
*s
)
209 for (nodep
= s
->root
; nodep
&& nodep
->left
; nodep
= nodep
->left
)
215 /* Returns a pointer to the node that describes the
216 * lowest bit index > the index of the node pointed to by np.
217 * Returns NULL if no node with a higher index exists.
219 static struct node
*node_next(const struct sparsebit
*s
, struct node
*np
)
221 struct node
*nodep
= np
;
224 * If current node has a right child, next node is the left-most
225 * of the right child.
228 for (nodep
= nodep
->right
; nodep
->left
; nodep
= nodep
->left
)
234 * No right child. Go up until node is left child of a parent.
235 * That parent is then the next node.
237 while (nodep
->parent
&& nodep
== nodep
->parent
->right
)
238 nodep
= nodep
->parent
;
240 return nodep
->parent
;
243 /* Searches for and returns a pointer to the node that describes the
244 * highest index < the index of the node pointed to by np.
245 * Returns NULL if no node with a lower index exists.
247 static struct node
*node_prev(const struct sparsebit
*s
, struct node
*np
)
249 struct node
*nodep
= np
;
252 * If current node has a left child, next node is the right-most
256 for (nodep
= nodep
->left
; nodep
->right
; nodep
= nodep
->right
)
258 return (struct node
*) nodep
;
262 * No left child. Go up until node is right child of a parent.
263 * That parent is then the next node.
265 while (nodep
->parent
&& nodep
== nodep
->parent
->left
)
266 nodep
= nodep
->parent
;
268 return (struct node
*) nodep
->parent
;
272 /* Allocates space to hold a copy of the node sub-tree pointed to by
273 * subtree and duplicates the bit settings to the newly allocated nodes.
274 * Returns the newly allocated copy of subtree.
276 static struct node
*node_copy_subtree(const struct node
*subtree
)
280 /* Duplicate the node at the root of the subtree */
281 root
= calloc(1, sizeof(*root
));
287 root
->idx
= subtree
->idx
;
288 root
->mask
= subtree
->mask
;
289 root
->num_after
= subtree
->num_after
;
291 /* As needed, recursively duplicate the left and right subtrees */
293 root
->left
= node_copy_subtree(subtree
->left
);
294 root
->left
->parent
= root
;
297 if (subtree
->right
) {
298 root
->right
= node_copy_subtree(subtree
->right
);
299 root
->right
->parent
= root
;
305 /* Searches for and returns a pointer to the node that describes the setting
306 * of the bit given by idx. A node describes the setting of a bit if its
307 * index is within the bits described by the mask bits or the number of
308 * contiguous bits set after the mask. Returns NULL if there is no such node.
310 static struct node
*node_find(const struct sparsebit
*s
, sparsebit_idx_t idx
)
314 /* Find the node that describes the setting of the bit at idx */
315 for (nodep
= s
->root
; nodep
;
316 nodep
= nodep
->idx
> idx
? nodep
->left
: nodep
->right
) {
317 if (idx
>= nodep
->idx
&&
318 idx
<= nodep
->idx
+ MASK_BITS
+ nodep
->num_after
- 1)
325 /* Entry Requirements:
326 * + A node that describes the setting of idx is not already present.
328 * Adds a new node to describe the setting of the bit at the index given
329 * by idx. Returns a pointer to the newly added node.
331 * TODO(lhuemill): Degenerate cases causes the tree to get unbalanced.
333 static struct node
*node_add(struct sparsebit
*s
, sparsebit_idx_t idx
)
335 struct node
*nodep
, *parentp
, *prev
;
337 /* Allocate and initialize the new node. */
338 nodep
= calloc(1, sizeof(*nodep
));
344 nodep
->idx
= idx
& -MASK_BITS
;
346 /* If no nodes, set it up as the root node. */
353 * Find the parent where the new node should be attached
354 * and add the node there.
358 if (idx
< parentp
->idx
) {
359 if (!parentp
->left
) {
360 parentp
->left
= nodep
;
361 nodep
->parent
= parentp
;
364 parentp
= parentp
->left
;
366 assert(idx
> parentp
->idx
+ MASK_BITS
+ parentp
->num_after
- 1);
367 if (!parentp
->right
) {
368 parentp
->right
= nodep
;
369 nodep
->parent
= parentp
;
372 parentp
= parentp
->right
;
377 * Does num_after bits of previous node overlap with the mask
378 * of the new node? If so set the bits in the new nodes mask
379 * and reduce the previous nodes num_after.
381 prev
= node_prev(s
, nodep
);
382 while (prev
&& prev
->idx
+ MASK_BITS
+ prev
->num_after
- 1 >= nodep
->idx
) {
383 unsigned int n1
= (prev
->idx
+ MASK_BITS
+ prev
->num_after
- 1)
385 assert(prev
->num_after
> 0);
386 assert(n1
< MASK_BITS
);
387 assert(!(nodep
->mask
& (1 << n1
)));
388 nodep
->mask
|= (1 << n1
);
395 /* Returns whether all the bits in the sparsebit array are set. */
396 bool sparsebit_all_set(const struct sparsebit
*s
)
399 * If any nodes there must be at least one bit set. Only case
400 * where a bit is set and total num set is 0, is when all bits
403 return s
->root
&& s
->num_set
== 0;
406 /* Clears all bits described by the node pointed to by nodep, then
409 static void node_rm(struct sparsebit
*s
, struct node
*nodep
)
412 sparsebit_num_t num_set
;
414 num_set
= node_num_set(nodep
);
415 assert(s
->num_set
>= num_set
|| sparsebit_all_set(s
));
416 s
->num_set
-= node_num_set(nodep
);
418 /* Have both left and right child */
419 if (nodep
->left
&& nodep
->right
) {
421 * Move left children to the leftmost leaf node
422 * of the right child.
424 for (tmp
= nodep
->right
; tmp
->left
; tmp
= tmp
->left
)
426 tmp
->left
= nodep
->left
;
428 tmp
->left
->parent
= tmp
;
431 /* Left only child */
433 if (!nodep
->parent
) {
434 s
->root
= nodep
->left
;
435 nodep
->left
->parent
= NULL
;
437 nodep
->left
->parent
= nodep
->parent
;
438 if (nodep
== nodep
->parent
->left
)
439 nodep
->parent
->left
= nodep
->left
;
441 assert(nodep
== nodep
->parent
->right
);
442 nodep
->parent
->right
= nodep
->left
;
446 nodep
->parent
= nodep
->left
= nodep
->right
= NULL
;
453 /* Right only child */
455 if (!nodep
->parent
) {
456 s
->root
= nodep
->right
;
457 nodep
->right
->parent
= NULL
;
459 nodep
->right
->parent
= nodep
->parent
;
460 if (nodep
== nodep
->parent
->left
)
461 nodep
->parent
->left
= nodep
->right
;
463 assert(nodep
== nodep
->parent
->right
);
464 nodep
->parent
->right
= nodep
->right
;
468 nodep
->parent
= nodep
->left
= nodep
->right
= NULL
;
475 if (!nodep
->parent
) {
478 if (nodep
->parent
->left
== nodep
)
479 nodep
->parent
->left
= NULL
;
481 assert(nodep
== nodep
->parent
->right
);
482 nodep
->parent
->right
= NULL
;
486 nodep
->parent
= nodep
->left
= nodep
->right
= NULL
;
492 /* Splits the node containing the bit at idx so that there is a node
493 * that starts at the specified index. If no such node exists, a new
494 * node at the specified index is created. Returns the new node.
496 * idx must start of a mask boundary.
498 static struct node
*node_split(struct sparsebit
*s
, sparsebit_idx_t idx
)
500 struct node
*nodep1
, *nodep2
;
501 sparsebit_idx_t offset
;
502 sparsebit_num_t orig_num_after
;
504 assert(!(idx
% MASK_BITS
));
507 * Is there a node that describes the setting of idx?
510 nodep1
= node_find(s
, idx
);
512 return node_add(s
, idx
);
515 * All done if the starting index of the node is where the
516 * split should occur.
518 if (nodep1
->idx
== idx
)
522 * Split point not at start of mask, so it must be part of
523 * bits described by num_after.
527 * Calculate offset within num_after for where the split is
530 offset
= idx
- (nodep1
->idx
+ MASK_BITS
);
531 orig_num_after
= nodep1
->num_after
;
534 * Add a new node to describe the bits starting at
537 nodep1
->num_after
= offset
;
538 nodep2
= node_add(s
, idx
);
540 /* Move bits after the split point into the new node */
541 nodep2
->num_after
= orig_num_after
- offset
;
542 if (nodep2
->num_after
>= MASK_BITS
) {
543 nodep2
->mask
= ~(mask_t
) 0;
544 nodep2
->num_after
-= MASK_BITS
;
546 nodep2
->mask
= (1 << nodep2
->num_after
) - 1;
547 nodep2
->num_after
= 0;
553 /* Iteratively reduces the node pointed to by nodep and its adjacent
554 * nodes into a more compact form. For example, a node with a mask with
555 * all bits set adjacent to a previous node, will get combined into a
556 * single node with an increased num_after setting.
558 * After each reduction, a further check is made to see if additional
559 * reductions are possible with the new previous and next nodes. Note,
560 * a search for a reduction is only done across the nodes nearest nodep
561 * and those that became part of a reduction. Reductions beyond nodep
562 * and the adjacent nodes that are reduced are not discovered. It is the
563 * responsibility of the caller to pass a nodep that is within one node
564 * of each possible reduction.
566 * This function does not fix the temporary violation of all invariants.
567 * For example it does not fix the case where the bit settings described
568 * by two or more nodes overlap. Such a violation introduces the potential
569 * complication of a bit setting for a specific index having different settings
570 * in different nodes. This would then introduce the further complication
571 * of which node has the correct setting of the bit and thus such conditions
574 * This function is designed to fix invariant violations that are introduced
575 * by node_split() and by changes to the nodes mask or num_after members.
576 * For example, when setting a bit within a nodes mask, the function that
577 * sets the bit doesn't have to worry about whether the setting of that
578 * bit caused the mask to have leading only or trailing only bits set.
579 * Instead, the function can call node_reduce(), with nodep equal to the
580 * node address that it set a mask bit in, and node_reduce() will notice
581 * the cases of leading or trailing only bits and that there is an
582 * adjacent node that the bit settings could be merged into.
584 * This implementation specifically detects and corrects violation of the
585 * following invariants:
587 * + Node are only used to represent bits that are set.
588 * Nodes with a mask of 0 and num_after of 0 are not allowed.
590 * + The setting of at least one bit is always described in a nodes
593 * + A node with all mask bits set only occurs when the last bit
594 * described by the previous node is not equal to this nodes
595 * starting index - 1. All such occurences of this condition are
596 * avoided by moving the setting of the nodes mask bits into
597 * the previous nodes num_after setting.
599 static void node_reduce(struct sparsebit
*s
, struct node
*nodep
)
601 bool reduction_performed
;
604 reduction_performed
= false;
605 struct node
*prev
, *next
, *tmp
;
607 /* 1) Potential reductions within the current node. */
609 /* Nodes with all bits cleared may be removed. */
610 if (nodep
->mask
== 0 && nodep
->num_after
== 0) {
612 * About to remove the node pointed to by
613 * nodep, which normally would cause a problem
614 * for the next pass through the reduction loop,
615 * because the node at the starting point no longer
616 * exists. This potential problem is handled
617 * by first remembering the location of the next
618 * or previous nodes. Doesn't matter which, because
619 * once the node at nodep is removed, there will be
620 * no other nodes between prev and next.
622 * Note, the checks performed on nodep against both
623 * both prev and next both check for an adjacent
624 * node that can be reduced into a single node. As
625 * such, after removing the node at nodep, doesn't
626 * matter whether the nodep for the next pass
627 * through the loop is equal to the previous pass
628 * prev or next node. Either way, on the next pass
629 * the one not selected will become either the
632 tmp
= node_next(s
, nodep
);
634 tmp
= node_prev(s
, nodep
);
639 reduction_performed
= true;
644 * When the mask is 0, can reduce the amount of num_after
645 * bits by moving the initial num_after bits into the mask.
647 if (nodep
->mask
== 0) {
648 assert(nodep
->num_after
!= 0);
649 assert(nodep
->idx
+ MASK_BITS
> nodep
->idx
);
651 nodep
->idx
+= MASK_BITS
;
653 if (nodep
->num_after
>= MASK_BITS
) {
655 nodep
->num_after
-= MASK_BITS
;
657 nodep
->mask
= (1u << nodep
->num_after
) - 1;
658 nodep
->num_after
= 0;
661 reduction_performed
= true;
666 * 2) Potential reductions between the current and
669 prev
= node_prev(s
, nodep
);
671 sparsebit_idx_t prev_highest_bit
;
673 /* Nodes with no bits set can be removed. */
674 if (prev
->mask
== 0 && prev
->num_after
== 0) {
677 reduction_performed
= true;
682 * All mask bits set and previous node has
685 if (nodep
->mask
+ 1 == 0 &&
686 prev
->idx
+ MASK_BITS
== nodep
->idx
) {
687 prev
->num_after
+= MASK_BITS
+ nodep
->num_after
;
689 nodep
->num_after
= 0;
691 reduction_performed
= true;
696 * Is node adjacent to previous node and the node
697 * contains a single contiguous range of bits
698 * starting from the beginning of the mask?
700 prev_highest_bit
= prev
->idx
+ MASK_BITS
- 1 + prev
->num_after
;
701 if (prev_highest_bit
+ 1 == nodep
->idx
&&
702 (nodep
->mask
| (nodep
->mask
>> 1)) == nodep
->mask
) {
704 * How many contiguous bits are there?
705 * Is equal to the total number of set
706 * bits, due to an earlier check that
707 * there is a single contiguous range of
710 unsigned int num_contiguous
711 = __builtin_popcount(nodep
->mask
);
712 assert((num_contiguous
> 0) &&
713 ((1ULL << num_contiguous
) - 1) == nodep
->mask
);
715 prev
->num_after
+= num_contiguous
;
719 * For predictable performance, handle special
720 * case where all mask bits are set and there
721 * is a non-zero num_after setting. This code
722 * is functionally correct without the following
723 * conditionalized statements, but without them
724 * the value of num_after is only reduced by
725 * the number of mask bits per pass. There are
726 * cases where num_after can be close to 2^64.
727 * Without this code it could take nearly
728 * (2^64) / 32 passes to perform the full
731 if (num_contiguous
== MASK_BITS
) {
732 prev
->num_after
+= nodep
->num_after
;
733 nodep
->num_after
= 0;
736 reduction_performed
= true;
742 * 3) Potential reductions between the current and
745 next
= node_next(s
, nodep
);
747 /* Nodes with no bits set can be removed. */
748 if (next
->mask
== 0 && next
->num_after
== 0) {
750 reduction_performed
= true;
755 * Is next node index adjacent to current node
756 * and has a mask with all bits set?
758 if (next
->idx
== nodep
->idx
+ MASK_BITS
+ nodep
->num_after
&&
759 next
->mask
== ~(mask_t
) 0) {
760 nodep
->num_after
+= MASK_BITS
;
762 nodep
->num_after
+= next
->num_after
;
768 reduction_performed
= true;
772 } while (nodep
&& reduction_performed
);
775 /* Returns whether the bit at the index given by idx, within the
776 * sparsebit array is set or not.
778 bool sparsebit_is_set(const struct sparsebit
*s
, sparsebit_idx_t idx
)
782 /* Find the node that describes the setting of the bit at idx */
783 for (nodep
= s
->root
; nodep
;
784 nodep
= nodep
->idx
> idx
? nodep
->left
: nodep
->right
)
785 if (idx
>= nodep
->idx
&&
786 idx
<= nodep
->idx
+ MASK_BITS
+ nodep
->num_after
- 1)
792 /* Bit is set if it is any of the bits described by num_after */
793 if (nodep
->num_after
&& idx
>= nodep
->idx
+ MASK_BITS
)
796 /* Is the corresponding mask bit set */
797 assert(idx
>= nodep
->idx
&& idx
- nodep
->idx
< MASK_BITS
);
798 return !!(nodep
->mask
& (1 << (idx
- nodep
->idx
)));
801 /* Within the sparsebit array pointed to by s, sets the bit
802 * at the index given by idx.
804 static void bit_set(struct sparsebit
*s
, sparsebit_idx_t idx
)
808 /* Skip bits that are already set */
809 if (sparsebit_is_set(s
, idx
))
813 * Get a node where the bit at idx is described by the mask.
814 * The node_split will also create a node, if there isn't
815 * already a node that describes the setting of bit.
817 nodep
= node_split(s
, idx
& -MASK_BITS
);
819 /* Set the bit within the nodes mask */
820 assert(idx
>= nodep
->idx
&& idx
<= nodep
->idx
+ MASK_BITS
- 1);
821 assert(!(nodep
->mask
& (1 << (idx
- nodep
->idx
))));
822 nodep
->mask
|= 1 << (idx
- nodep
->idx
);
825 node_reduce(s
, nodep
);
828 /* Within the sparsebit array pointed to by s, clears the bit
829 * at the index given by idx.
831 static void bit_clear(struct sparsebit
*s
, sparsebit_idx_t idx
)
835 /* Skip bits that are already cleared */
836 if (!sparsebit_is_set(s
, idx
))
839 /* Is there a node that describes the setting of this bit? */
840 nodep
= node_find(s
, idx
);
845 * If a num_after bit, split the node, so that the bit is
846 * part of a node mask.
848 if (idx
>= nodep
->idx
+ MASK_BITS
)
849 nodep
= node_split(s
, idx
& -MASK_BITS
);
852 * After node_split above, bit at idx should be within the mask.
855 assert(idx
>= nodep
->idx
&& idx
<= nodep
->idx
+ MASK_BITS
- 1);
856 assert(nodep
->mask
& (1 << (idx
- nodep
->idx
)));
857 nodep
->mask
&= ~(1 << (idx
- nodep
->idx
));
858 assert(s
->num_set
> 0 || sparsebit_all_set(s
));
861 node_reduce(s
, nodep
);
864 /* Recursively dumps to the FILE stream given by stream the contents
865 * of the sub-tree of nodes pointed to by nodep. Each line of output
866 * is prefixed by the number of spaces given by indent. On each
867 * recursion, the indent amount is increased by 2. This causes nodes
868 * at each level deeper into the binary search tree to be displayed
869 * with a greater indent.
871 static void dump_nodes(FILE *stream
, struct node
*nodep
,
876 /* Dump contents of node */
879 else if (nodep
== nodep
->parent
->left
)
882 assert(nodep
== nodep
->parent
->right
);
885 fprintf(stream
, "%*s---- %s nodep: %p\n", indent
, "", node_type
, nodep
);
886 fprintf(stream
, "%*s parent: %p left: %p right: %p\n", indent
, "",
887 nodep
->parent
, nodep
->left
, nodep
->right
);
888 fprintf(stream
, "%*s idx: 0x%lx mask: 0x%x num_after: 0x%lx\n",
889 indent
, "", nodep
->idx
, nodep
->mask
, nodep
->num_after
);
891 /* If present, dump contents of left child nodes */
893 dump_nodes(stream
, nodep
->left
, indent
+ 2);
895 /* If present, dump contents of right child nodes */
897 dump_nodes(stream
, nodep
->right
, indent
+ 2);
900 static inline sparsebit_idx_t
node_first_set(struct node
*nodep
, int start
)
902 mask_t leading
= (mask_t
)1 << start
;
903 int n1
= __builtin_ctz(nodep
->mask
& -leading
);
905 return nodep
->idx
+ n1
;
908 static inline sparsebit_idx_t
node_first_clear(struct node
*nodep
, int start
)
910 mask_t leading
= (mask_t
)1 << start
;
911 int n1
= __builtin_ctz(~nodep
->mask
& -leading
);
913 return nodep
->idx
+ n1
;
916 /* Dumps to the FILE stream specified by stream, the implementation dependent
917 * internal state of s. Each line of output is prefixed with the number
918 * of spaces given by indent. The output is completely implementation
919 * dependent and subject to change. Output from this function should only
920 * be used for diagnostic purposes. For example, this function can be
921 * used by test cases after they detect an unexpected condition, as a means
922 * to capture diagnostic information.
924 static void sparsebit_dump_internal(FILE *stream
, const struct sparsebit
*s
,
927 /* Dump the contents of s */
928 fprintf(stream
, "%*sroot: %p\n", indent
, "", s
->root
);
929 fprintf(stream
, "%*snum_set: 0x%lx\n", indent
, "", s
->num_set
);
932 dump_nodes(stream
, s
->root
, indent
);
935 /* Allocates and returns a new sparsebit array. The initial state
936 * of the newly allocated sparsebit array has all bits cleared.
938 struct sparsebit
*sparsebit_alloc(void)
942 /* Allocate top level structure. */
943 s
= calloc(1, sizeof(*s
));
952 /* Frees the implementation dependent data for the sparsebit array
953 * pointed to by s and poisons the pointer to that data.
955 void sparsebit_free(struct sparsebit
**sbitp
)
957 struct sparsebit
*s
= *sbitp
;
962 sparsebit_clear_all(s
);
967 /* Makes a copy of the sparsebit array given by s, to the sparsebit
968 * array given by d. Note, d must have already been allocated via
969 * sparsebit_alloc(). It can though already have bits set, which
970 * if different from src will be cleared.
972 void sparsebit_copy(struct sparsebit
*d
, const struct sparsebit
*s
)
974 /* First clear any bits already set in the destination */
975 sparsebit_clear_all(d
);
978 d
->root
= node_copy_subtree(s
->root
);
979 d
->num_set
= s
->num_set
;
983 /* Returns whether num consecutive bits starting at idx are all set. */
984 bool sparsebit_is_set_num(const struct sparsebit
*s
,
985 sparsebit_idx_t idx
, sparsebit_num_t num
)
987 sparsebit_idx_t next_cleared
;
990 assert(idx
+ num
- 1 >= idx
);
992 /* With num > 0, the first bit must be set. */
993 if (!sparsebit_is_set(s
, idx
))
996 /* Find the next cleared bit */
997 next_cleared
= sparsebit_next_clear(s
, idx
);
1000 * If no cleared bits beyond idx, then there are at least num
1001 * set bits. idx + num doesn't wrap. Otherwise check if
1002 * there are enough set bits between idx and the next cleared bit.
1004 return next_cleared
== 0 || next_cleared
- idx
>= num
;
1007 /* Returns whether the bit at the index given by idx. */
1008 bool sparsebit_is_clear(const struct sparsebit
*s
,
1009 sparsebit_idx_t idx
)
1011 return !sparsebit_is_set(s
, idx
);
1014 /* Returns whether num consecutive bits starting at idx are all cleared. */
1015 bool sparsebit_is_clear_num(const struct sparsebit
*s
,
1016 sparsebit_idx_t idx
, sparsebit_num_t num
)
1018 sparsebit_idx_t next_set
;
1021 assert(idx
+ num
- 1 >= idx
);
1023 /* With num > 0, the first bit must be cleared. */
1024 if (!sparsebit_is_clear(s
, idx
))
1027 /* Find the next set bit */
1028 next_set
= sparsebit_next_set(s
, idx
);
1031 * If no set bits beyond idx, then there are at least num
1032 * cleared bits. idx + num doesn't wrap. Otherwise check if
1033 * there are enough cleared bits between idx and the next set bit.
1035 return next_set
== 0 || next_set
- idx
>= num
;
1038 /* Returns the total number of bits set. Note: 0 is also returned for
1039 * the case of all bits set. This is because with all bits set, there
1040 * is 1 additional bit set beyond what can be represented in the return
1041 * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0,
1042 * to determine if the sparsebit array has any bits set.
1044 sparsebit_num_t
sparsebit_num_set(const struct sparsebit
*s
)
1049 /* Returns whether any bit is set in the sparsebit array. */
1050 bool sparsebit_any_set(const struct sparsebit
*s
)
1053 * Nodes only describe set bits. If any nodes then there
1054 * is at least 1 bit set.
1060 * Every node should have a non-zero mask. For now will
1061 * just assure that the root node has a non-zero mask,
1062 * which is a quick check that at least 1 bit is set.
1064 assert(s
->root
->mask
!= 0);
1065 assert(s
->num_set
> 0 ||
1066 (s
->root
->num_after
== ((sparsebit_num_t
) 0) - MASK_BITS
&&
1067 s
->root
->mask
== ~(mask_t
) 0));
1072 /* Returns whether all the bits in the sparsebit array are cleared. */
1073 bool sparsebit_all_clear(const struct sparsebit
*s
)
1075 return !sparsebit_any_set(s
);
1078 /* Returns whether all the bits in the sparsebit array are set. */
1079 bool sparsebit_any_clear(const struct sparsebit
*s
)
1081 return !sparsebit_all_set(s
);
1084 /* Returns the index of the first set bit. Abort if no bits are set.
1086 sparsebit_idx_t
sparsebit_first_set(const struct sparsebit
*s
)
1090 /* Validate at least 1 bit is set */
1091 assert(sparsebit_any_set(s
));
1093 nodep
= node_first(s
);
1094 return node_first_set(nodep
, 0);
1097 /* Returns the index of the first cleared bit. Abort if
1098 * no bits are cleared.
1100 sparsebit_idx_t
sparsebit_first_clear(const struct sparsebit
*s
)
1102 struct node
*nodep1
, *nodep2
;
1104 /* Validate at least 1 bit is cleared. */
1105 assert(sparsebit_any_clear(s
));
1107 /* If no nodes or first node index > 0 then lowest cleared is 0 */
1108 nodep1
= node_first(s
);
1109 if (!nodep1
|| nodep1
->idx
> 0)
1112 /* Does the mask in the first node contain any cleared bits. */
1113 if (nodep1
->mask
!= ~(mask_t
) 0)
1114 return node_first_clear(nodep1
, 0);
1117 * All mask bits set in first node. If there isn't a second node
1118 * then the first cleared bit is the first bit after the bits
1119 * described by the first node.
1121 nodep2
= node_next(s
, nodep1
);
1124 * No second node. First cleared bit is first bit beyond
1125 * bits described by first node.
1127 assert(nodep1
->mask
== ~(mask_t
) 0);
1128 assert(nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
!= (sparsebit_idx_t
) 0);
1129 return nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
;
1133 * There is a second node.
1134 * If it is not adjacent to the first node, then there is a gap
1135 * of cleared bits between the nodes, and the first cleared bit
1136 * is the first bit within the gap.
1138 if (nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
!= nodep2
->idx
)
1139 return nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
;
1142 * Second node is adjacent to the first node.
1143 * Because it is adjacent, its mask should be non-zero. If all
1144 * its mask bits are set, then with it being adjacent, it should
1145 * have had the mask bits moved into the num_after setting of the
1148 return node_first_clear(nodep2
, 0);
1151 /* Returns index of next bit set within s after the index given by prev.
1152 * Returns 0 if there are no bits after prev that are set.
1154 sparsebit_idx_t
sparsebit_next_set(const struct sparsebit
*s
,
1155 sparsebit_idx_t prev
)
1157 sparsebit_idx_t lowest_possible
= prev
+ 1;
1158 sparsebit_idx_t start
;
1161 /* A bit after the highest index can't be set. */
1162 if (lowest_possible
== 0)
1166 * Find the leftmost 'candidate' overlapping or to the right
1167 * of lowest_possible.
1169 struct node
*candidate
= NULL
;
1171 /* True iff lowest_possible is within candidate */
1172 bool contains
= false;
1175 * Find node that describes setting of bit at lowest_possible.
1176 * If such a node doesn't exist, find the node with the lowest
1177 * starting index that is > lowest_possible.
1179 for (nodep
= s
->root
; nodep
;) {
1180 if ((nodep
->idx
+ MASK_BITS
+ nodep
->num_after
- 1)
1181 >= lowest_possible
) {
1183 if (candidate
->idx
<= lowest_possible
) {
1187 nodep
= nodep
->left
;
1189 nodep
= nodep
->right
;
1195 assert(candidate
->mask
!= 0);
1197 /* Does the candidate node describe the setting of lowest_possible? */
1200 * Candidate doesn't describe setting of bit at lowest_possible.
1201 * Candidate points to the first node with a starting index
1202 * > lowest_possible.
1204 assert(candidate
->idx
> lowest_possible
);
1206 return node_first_set(candidate
, 0);
1210 * Candidate describes setting of bit at lowest_possible.
1211 * Note: although the node describes the setting of the bit
1212 * at lowest_possible, its possible that its setting and the
1213 * setting of all latter bits described by this node are 0.
1214 * For now, just handle the cases where this node describes
1215 * a bit at or after an index of lowest_possible that is set.
1217 start
= lowest_possible
- candidate
->idx
;
1219 if (start
< MASK_BITS
&& candidate
->mask
>= (1 << start
))
1220 return node_first_set(candidate
, start
);
1222 if (candidate
->num_after
) {
1223 sparsebit_idx_t first_num_after_idx
= candidate
->idx
+ MASK_BITS
;
1225 return lowest_possible
< first_num_after_idx
1226 ? first_num_after_idx
: lowest_possible
;
1230 * Although candidate node describes setting of bit at
1231 * the index of lowest_possible, all bits at that index and
1232 * latter that are described by candidate are cleared. With
1233 * this, the next bit is the first bit in the next node, if
1234 * such a node exists. If a next node doesn't exist, then
1235 * there is no next set bit.
1237 candidate
= node_next(s
, candidate
);
1241 return node_first_set(candidate
, 0);
1244 /* Returns index of next bit cleared within s after the index given by prev.
1245 * Returns 0 if there are no bits after prev that are cleared.
1247 sparsebit_idx_t
sparsebit_next_clear(const struct sparsebit
*s
,
1248 sparsebit_idx_t prev
)
1250 sparsebit_idx_t lowest_possible
= prev
+ 1;
1251 sparsebit_idx_t idx
;
1252 struct node
*nodep1
, *nodep2
;
1254 /* A bit after the highest index can't be set. */
1255 if (lowest_possible
== 0)
1259 * Does a node describing the setting of lowest_possible exist?
1260 * If not, the bit at lowest_possible is cleared.
1262 nodep1
= node_find(s
, lowest_possible
);
1264 return lowest_possible
;
1266 /* Does a mask bit in node 1 describe the next cleared bit. */
1267 for (idx
= lowest_possible
- nodep1
->idx
; idx
< MASK_BITS
; idx
++)
1268 if (!(nodep1
->mask
& (1 << idx
)))
1269 return nodep1
->idx
+ idx
;
1272 * Next cleared bit is not described by node 1. If there
1273 * isn't a next node, then next cleared bit is described
1274 * by bit after the bits described by the first node.
1276 nodep2
= node_next(s
, nodep1
);
1278 return nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
;
1281 * There is a second node.
1282 * If it is not adjacent to the first node, then there is a gap
1283 * of cleared bits between the nodes, and the next cleared bit
1284 * is the first bit within the gap.
1286 if (nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
!= nodep2
->idx
)
1287 return nodep1
->idx
+ MASK_BITS
+ nodep1
->num_after
;
1290 * Second node is adjacent to the first node.
1291 * Because it is adjacent, its mask should be non-zero. If all
1292 * its mask bits are set, then with it being adjacent, it should
1293 * have had the mask bits moved into the num_after setting of the
1296 return node_first_clear(nodep2
, 0);
1299 /* Starting with the index 1 greater than the index given by start, finds
1300 * and returns the index of the first sequence of num consecutively set
1301 * bits. Returns a value of 0 of no such sequence exists.
1303 sparsebit_idx_t
sparsebit_next_set_num(const struct sparsebit
*s
,
1304 sparsebit_idx_t start
, sparsebit_num_t num
)
1306 sparsebit_idx_t idx
;
1310 for (idx
= sparsebit_next_set(s
, start
);
1311 idx
!= 0 && idx
+ num
- 1 >= idx
;
1312 idx
= sparsebit_next_set(s
, idx
)) {
1313 assert(sparsebit_is_set(s
, idx
));
1316 * Does the sequence of bits starting at idx consist of
1319 if (sparsebit_is_set_num(s
, idx
, num
))
1323 * Sequence of set bits at idx isn't large enough.
1324 * Skip this entire sequence of set bits.
1326 idx
= sparsebit_next_clear(s
, idx
);
1334 /* Starting with the index 1 greater than the index given by start, finds
1335 * and returns the index of the first sequence of num consecutively cleared
1336 * bits. Returns a value of 0 of no such sequence exists.
1338 sparsebit_idx_t
sparsebit_next_clear_num(const struct sparsebit
*s
,
1339 sparsebit_idx_t start
, sparsebit_num_t num
)
1341 sparsebit_idx_t idx
;
1345 for (idx
= sparsebit_next_clear(s
, start
);
1346 idx
!= 0 && idx
+ num
- 1 >= idx
;
1347 idx
= sparsebit_next_clear(s
, idx
)) {
1348 assert(sparsebit_is_clear(s
, idx
));
1351 * Does the sequence of bits starting at idx consist of
1354 if (sparsebit_is_clear_num(s
, idx
, num
))
1358 * Sequence of cleared bits at idx isn't large enough.
1359 * Skip this entire sequence of cleared bits.
1361 idx
= sparsebit_next_set(s
, idx
);
1369 /* Sets the bits * in the inclusive range idx through idx + num - 1. */
1370 void sparsebit_set_num(struct sparsebit
*s
,
1371 sparsebit_idx_t start
, sparsebit_num_t num
)
1373 struct node
*nodep
, *next
;
1375 sparsebit_idx_t idx
;
1377 sparsebit_idx_t middle_start
, middle_end
;
1380 assert(start
+ num
- 1 >= start
);
1383 * Leading - bits before first mask boundary.
1385 * TODO(lhuemill): With some effort it may be possible to
1386 * replace the following loop with a sequential sequence
1387 * of statements. High level sequence would be:
1389 * 1. Use node_split() to force node that describes setting
1390 * of idx to be within the mask portion of a node.
1391 * 2. Form mask of bits to be set.
1392 * 3. Determine number of mask bits already set in the node
1393 * and store in a local variable named num_already_set.
1394 * 4. Set the appropriate mask bits within the node.
1395 * 5. Increment struct sparsebit_pvt num_set member
1396 * by the number of bits that were actually set.
1397 * Exclude from the counts bits that were already set.
1398 * 6. Before returning to the caller, use node_reduce() to
1399 * handle the multiple corner cases that this method
1402 for (idx
= start
, n
= num
; n
> 0 && idx
% MASK_BITS
!= 0; idx
++, n
--)
1405 /* Middle - bits spanning one or more entire mask */
1407 middle_end
= middle_start
+ (n
& -MASK_BITS
) - 1;
1408 if (n
>= MASK_BITS
) {
1409 nodep
= node_split(s
, middle_start
);
1412 * As needed, split just after end of middle bits.
1413 * No split needed if end of middle bits is at highest
1414 * supported bit index.
1416 if (middle_end
+ 1 > middle_end
)
1417 (void) node_split(s
, middle_end
+ 1);
1419 /* Delete nodes that only describe bits within the middle. */
1420 for (next
= node_next(s
, nodep
);
1421 next
&& (next
->idx
< middle_end
);
1422 next
= node_next(s
, nodep
)) {
1423 assert(next
->idx
+ MASK_BITS
+ next
->num_after
- 1 <= middle_end
);
1428 /* As needed set each of the mask bits */
1429 for (n1
= 0; n1
< MASK_BITS
; n1
++) {
1430 if (!(nodep
->mask
& (1 << n1
))) {
1431 nodep
->mask
|= 1 << n1
;
1436 s
->num_set
-= nodep
->num_after
;
1437 nodep
->num_after
= middle_end
- middle_start
+ 1 - MASK_BITS
;
1438 s
->num_set
+= nodep
->num_after
;
1440 node_reduce(s
, nodep
);
1442 idx
= middle_end
+ 1;
1443 n
-= middle_end
- middle_start
+ 1;
1445 /* Trailing - bits at and beyond last mask boundary */
1446 assert(n
< MASK_BITS
);
1447 for (; n
> 0; idx
++, n
--)
1451 /* Clears the bits * in the inclusive range idx through idx + num - 1. */
1452 void sparsebit_clear_num(struct sparsebit
*s
,
1453 sparsebit_idx_t start
, sparsebit_num_t num
)
1455 struct node
*nodep
, *next
;
1457 sparsebit_idx_t idx
;
1459 sparsebit_idx_t middle_start
, middle_end
;
1462 assert(start
+ num
- 1 >= start
);
1464 /* Leading - bits before first mask boundary */
1465 for (idx
= start
, n
= num
; n
> 0 && idx
% MASK_BITS
!= 0; idx
++, n
--)
1468 /* Middle - bits spanning one or more entire mask */
1470 middle_end
= middle_start
+ (n
& -MASK_BITS
) - 1;
1471 if (n
>= MASK_BITS
) {
1472 nodep
= node_split(s
, middle_start
);
1475 * As needed, split just after end of middle bits.
1476 * No split needed if end of middle bits is at highest
1477 * supported bit index.
1479 if (middle_end
+ 1 > middle_end
)
1480 (void) node_split(s
, middle_end
+ 1);
1482 /* Delete nodes that only describe bits within the middle. */
1483 for (next
= node_next(s
, nodep
);
1484 next
&& (next
->idx
< middle_end
);
1485 next
= node_next(s
, nodep
)) {
1486 assert(next
->idx
+ MASK_BITS
+ next
->num_after
- 1 <= middle_end
);
1491 /* As needed clear each of the mask bits */
1492 for (n1
= 0; n1
< MASK_BITS
; n1
++) {
1493 if (nodep
->mask
& (1 << n1
)) {
1494 nodep
->mask
&= ~(1 << n1
);
1499 /* Clear any bits described by num_after */
1500 s
->num_set
-= nodep
->num_after
;
1501 nodep
->num_after
= 0;
1504 * Delete the node that describes the beginning of
1505 * the middle bits and perform any allowed reductions
1506 * with the nodes prev or next of nodep.
1508 node_reduce(s
, nodep
);
1511 idx
= middle_end
+ 1;
1512 n
-= middle_end
- middle_start
+ 1;
1514 /* Trailing - bits at and beyond last mask boundary */
1515 assert(n
< MASK_BITS
);
1516 for (; n
> 0; idx
++, n
--)
1520 /* Sets the bit at the index given by idx. */
1521 void sparsebit_set(struct sparsebit
*s
, sparsebit_idx_t idx
)
1523 sparsebit_set_num(s
, idx
, 1);
1526 /* Clears the bit at the index given by idx. */
1527 void sparsebit_clear(struct sparsebit
*s
, sparsebit_idx_t idx
)
1529 sparsebit_clear_num(s
, idx
, 1);
1532 /* Sets the bits in the entire addressable range of the sparsebit array. */
1533 void sparsebit_set_all(struct sparsebit
*s
)
1535 sparsebit_set(s
, 0);
1536 sparsebit_set_num(s
, 1, ~(sparsebit_idx_t
) 0);
1537 assert(sparsebit_all_set(s
));
1540 /* Clears the bits in the entire addressable range of the sparsebit array. */
1541 void sparsebit_clear_all(struct sparsebit
*s
)
1543 sparsebit_clear(s
, 0);
1544 sparsebit_clear_num(s
, 1, ~(sparsebit_idx_t
) 0);
1545 assert(!sparsebit_any_set(s
));
1548 static size_t display_range(FILE *stream
, sparsebit_idx_t low
,
1549 sparsebit_idx_t high
, bool prepend_comma_space
)
1554 /* Determine the printf format string */
1556 fmt_str
= prepend_comma_space
? ", 0x%lx" : "0x%lx";
1558 fmt_str
= prepend_comma_space
? ", 0x%lx:0x%lx" : "0x%lx:0x%lx";
1561 * When stream is NULL, just determine the size of what would
1562 * have been printed, else print the range.
1565 sz
= snprintf(NULL
, 0, fmt_str
, low
, high
);
1567 sz
= fprintf(stream
, fmt_str
, low
, high
);
1573 /* Dumps to the FILE stream given by stream, the bit settings
1574 * of s. Each line of output is prefixed with the number of
1575 * spaces given by indent. The length of each line is implementation
1576 * dependent and does not depend on the indent amount. The following
1577 * is an example output of a sparsebit array that has bits:
1579 * 0x5, 0x8, 0xa:0xe, 0x12
1581 * This corresponds to a sparsebit whose bits 5, 8, 10, 11, 12, 13, 14, 18
1582 * are set. Note that a ':', instead of a '-' is used to specify a range of
1583 * contiguous bits. This is done because '-' is used to specify command-line
1584 * options, and sometimes ranges are specified as command-line arguments.
1586 void sparsebit_dump(FILE *stream
, const struct sparsebit
*s
,
1587 unsigned int indent
)
1589 size_t current_line_len
= 0;
1593 if (!sparsebit_any_set(s
))
1596 /* Display initial indent */
1597 fprintf(stream
, "%*s", indent
, "");
1600 for (nodep
= node_first(s
); nodep
; nodep
= node_next(s
, nodep
)) {
1602 sparsebit_idx_t low
, high
;
1604 /* For each group of bits in the mask */
1605 for (n1
= 0; n1
< MASK_BITS
; n1
++) {
1606 if (nodep
->mask
& (1 << n1
)) {
1607 low
= high
= nodep
->idx
+ n1
;
1609 for (; n1
< MASK_BITS
; n1
++) {
1610 if (nodep
->mask
& (1 << n1
))
1611 high
= nodep
->idx
+ n1
;
1616 if ((n1
== MASK_BITS
) && nodep
->num_after
)
1617 high
+= nodep
->num_after
;
1620 * How much room will it take to display
1623 sz
= display_range(NULL
, low
, high
,
1624 current_line_len
!= 0);
1627 * If there is not enough room, display
1628 * a newline plus the indent of the next
1631 if (current_line_len
+ sz
> DUMP_LINE_MAX
) {
1632 fputs("\n", stream
);
1633 fprintf(stream
, "%*s", indent
, "");
1634 current_line_len
= 0;
1637 /* Display the range */
1638 sz
= display_range(stream
, low
, high
,
1639 current_line_len
!= 0);
1640 current_line_len
+= sz
;
1645 * If num_after and most significant-bit of mask is not
1646 * set, then still need to display a range for the bits
1647 * described by num_after.
1649 if (!(nodep
->mask
& (1 << (MASK_BITS
- 1))) && nodep
->num_after
) {
1650 low
= nodep
->idx
+ MASK_BITS
;
1651 high
= nodep
->idx
+ MASK_BITS
+ nodep
->num_after
- 1;
1654 * How much room will it take to display
1657 sz
= display_range(NULL
, low
, high
,
1658 current_line_len
!= 0);
1661 * If there is not enough room, display
1662 * a newline plus the indent of the next
1665 if (current_line_len
+ sz
> DUMP_LINE_MAX
) {
1666 fputs("\n", stream
);
1667 fprintf(stream
, "%*s", indent
, "");
1668 current_line_len
= 0;
1671 /* Display the range */
1672 sz
= display_range(stream
, low
, high
,
1673 current_line_len
!= 0);
1674 current_line_len
+= sz
;
1677 fputs("\n", stream
);
1680 /* Validates the internal state of the sparsebit array given by
1681 * s. On error, diagnostic information is printed to stderr and
1684 void sparsebit_validate_internal(const struct sparsebit
*s
)
1686 bool error_detected
= false;
1687 struct node
*nodep
, *prev
= NULL
;
1688 sparsebit_num_t total_bits_set
= 0;
1692 for (nodep
= node_first(s
); nodep
;
1693 prev
= nodep
, nodep
= node_next(s
, nodep
)) {
1696 * Increase total bits set by the number of bits set
1699 for (n1
= 0; n1
< MASK_BITS
; n1
++)
1700 if (nodep
->mask
& (1 << n1
))
1703 total_bits_set
+= nodep
->num_after
;
1706 * Arbitrary choice as to whether a mask of 0 is allowed
1707 * or not. For diagnostic purposes it is beneficial to
1708 * have only one valid means to represent a set of bits.
1709 * To support this an arbitrary choice has been made
1710 * to not allow a mask of zero.
1712 if (nodep
->mask
== 0) {
1713 fprintf(stderr
, "Node mask of zero, "
1714 "nodep: %p nodep->mask: 0x%x",
1715 nodep
, nodep
->mask
);
1716 error_detected
= true;
1721 * Validate num_after is not greater than the max index
1722 * - the number of mask bits. The num_after member
1723 * uses 0-based indexing and thus has no value that
1724 * represents all bits set. This limitation is handled
1725 * by requiring a non-zero mask. With a non-zero mask,
1726 * MASK_BITS worth of bits are described by the mask,
1727 * which makes the largest needed num_after equal to:
1729 * (~(sparsebit_num_t) 0) - MASK_BITS + 1
1731 if (nodep
->num_after
1732 > (~(sparsebit_num_t
) 0) - MASK_BITS
+ 1) {
1733 fprintf(stderr
, "num_after too large, "
1734 "nodep: %p nodep->num_after: 0x%lx",
1735 nodep
, nodep
->num_after
);
1736 error_detected
= true;
1740 /* Validate node index is divisible by the mask size */
1741 if (nodep
->idx
% MASK_BITS
) {
1742 fprintf(stderr
, "Node index not divisible by "
1744 " nodep: %p nodep->idx: 0x%lx "
1746 nodep
, nodep
->idx
, MASK_BITS
);
1747 error_detected
= true;
1752 * Validate bits described by node don't wrap beyond the
1753 * highest supported index.
1755 if ((nodep
->idx
+ MASK_BITS
+ nodep
->num_after
- 1) < nodep
->idx
) {
1756 fprintf(stderr
, "Bits described by node wrap "
1757 "beyond highest supported index,\n"
1758 " nodep: %p nodep->idx: 0x%lx\n"
1759 " MASK_BITS: %lu nodep->num_after: 0x%lx",
1760 nodep
, nodep
->idx
, MASK_BITS
, nodep
->num_after
);
1761 error_detected
= true;
1765 /* Check parent pointers. */
1767 if (nodep
->left
->parent
!= nodep
) {
1768 fprintf(stderr
, "Left child parent pointer "
1769 "doesn't point to this node,\n"
1770 " nodep: %p nodep->left: %p "
1771 "nodep->left->parent: %p",
1773 nodep
->left
->parent
);
1774 error_detected
= true;
1780 if (nodep
->right
->parent
!= nodep
) {
1781 fprintf(stderr
, "Right child parent pointer "
1782 "doesn't point to this node,\n"
1783 " nodep: %p nodep->right: %p "
1784 "nodep->right->parent: %p",
1785 nodep
, nodep
->right
,
1786 nodep
->right
->parent
);
1787 error_detected
= true;
1792 if (!nodep
->parent
) {
1793 if (s
->root
!= nodep
) {
1794 fprintf(stderr
, "Unexpected root node, "
1795 "s->root: %p nodep: %p",
1797 error_detected
= true;
1804 * Is index of previous node before index of
1807 if (prev
->idx
>= nodep
->idx
) {
1808 fprintf(stderr
, "Previous node index "
1809 ">= current node index,\n"
1810 " prev: %p prev->idx: 0x%lx\n"
1811 " nodep: %p nodep->idx: 0x%lx",
1812 prev
, prev
->idx
, nodep
, nodep
->idx
);
1813 error_detected
= true;
1818 * Nodes occur in asscending order, based on each
1819 * nodes starting index.
1821 if ((prev
->idx
+ MASK_BITS
+ prev
->num_after
- 1)
1823 fprintf(stderr
, "Previous node bit range "
1824 "overlap with current node bit range,\n"
1825 " prev: %p prev->idx: 0x%lx "
1826 "prev->num_after: 0x%lx\n"
1827 " nodep: %p nodep->idx: 0x%lx "
1828 "nodep->num_after: 0x%lx\n"
1830 prev
, prev
->idx
, prev
->num_after
,
1831 nodep
, nodep
->idx
, nodep
->num_after
,
1833 error_detected
= true;
1838 * When the node has all mask bits set, it shouldn't
1839 * be adjacent to the last bit described by the
1842 if (nodep
->mask
== ~(mask_t
) 0 &&
1843 prev
->idx
+ MASK_BITS
+ prev
->num_after
== nodep
->idx
) {
1844 fprintf(stderr
, "Current node has mask with "
1845 "all bits set and is adjacent to the "
1847 " prev: %p prev->idx: 0x%lx "
1848 "prev->num_after: 0x%lx\n"
1849 " nodep: %p nodep->idx: 0x%lx "
1850 "nodep->num_after: 0x%lx\n"
1852 prev
, prev
->idx
, prev
->num_after
,
1853 nodep
, nodep
->idx
, nodep
->num_after
,
1856 error_detected
= true;
1862 if (!error_detected
) {
1864 * Is sum of bits set in each node equal to the count
1865 * of total bits set.
1867 if (s
->num_set
!= total_bits_set
) {
1868 fprintf(stderr
, "Number of bits set mismatch,\n"
1869 " s->num_set: 0x%lx total_bits_set: 0x%lx",
1870 s
->num_set
, total_bits_set
);
1872 error_detected
= true;
1876 if (error_detected
) {
1877 fputs(" dump_internal:\n", stderr
);
1878 sparsebit_dump_internal(stderr
, s
, 4);
1885 /* A simple but effective fuzzing driver. Look for bugs with the help
1886 * of some invariants and of a trivial representation of sparsebit.
1887 * Just use 512 bytes of /dev/zero and /dev/urandom as inputs, and let
1888 * afl-fuzz do the magic. :)
1894 sparsebit_idx_t first
, last
;
1898 struct sparsebit
*s
;
1899 struct range ranges
[1000];
1902 static bool get_value(sparsebit_idx_t idx
)
1906 for (i
= num_ranges
; --i
>= 0; )
1907 if (ranges
[i
].first
<= idx
&& idx
<= ranges
[i
].last
)
1908 return ranges
[i
].set
;
1913 static void operate(int code
, sparsebit_idx_t first
, sparsebit_idx_t last
)
1915 sparsebit_num_t num
;
1916 sparsebit_idx_t next
;
1919 num
= last
- first
+ 1;
1921 num
= first
- last
+ 1;
1923 last
= first
+ num
- 1;
1928 sparsebit_set(s
, first
);
1929 assert(sparsebit_is_set(s
, first
));
1930 assert(!sparsebit_is_clear(s
, first
));
1931 assert(sparsebit_any_set(s
));
1932 assert(!sparsebit_all_clear(s
));
1933 if (get_value(first
))
1935 if (num_ranges
== 1000)
1937 ranges
[num_ranges
++] = (struct range
)
1938 { .first
= first
, .last
= first
, .set
= true };
1941 sparsebit_clear(s
, first
);
1942 assert(!sparsebit_is_set(s
, first
));
1943 assert(sparsebit_is_clear(s
, first
));
1944 assert(sparsebit_any_clear(s
));
1945 assert(!sparsebit_all_set(s
));
1946 if (!get_value(first
))
1948 if (num_ranges
== 1000)
1950 ranges
[num_ranges
++] = (struct range
)
1951 { .first
= first
, .last
= first
, .set
= false };
1954 assert(sparsebit_is_set(s
, first
) == get_value(first
));
1955 assert(sparsebit_is_clear(s
, first
) == !get_value(first
));
1958 if (sparsebit_any_set(s
))
1959 assert(get_value(sparsebit_first_set(s
)));
1960 if (sparsebit_any_clear(s
))
1961 assert(!get_value(sparsebit_first_clear(s
)));
1962 sparsebit_set_all(s
);
1963 assert(!sparsebit_any_clear(s
));
1964 assert(sparsebit_all_set(s
));
1966 ranges
[num_ranges
++] = (struct range
)
1967 { .first
= 0, .last
= ~(sparsebit_idx_t
)0, .set
= true };
1970 if (sparsebit_any_set(s
))
1971 assert(get_value(sparsebit_first_set(s
)));
1972 if (sparsebit_any_clear(s
))
1973 assert(!get_value(sparsebit_first_clear(s
)));
1974 sparsebit_clear_all(s
);
1975 assert(!sparsebit_any_set(s
));
1976 assert(sparsebit_all_clear(s
));
1980 next
= sparsebit_next_set(s
, first
);
1981 assert(next
== 0 || next
> first
);
1982 assert(next
== 0 || get_value(next
));
1985 next
= sparsebit_next_clear(s
, first
);
1986 assert(next
== 0 || next
> first
);
1987 assert(next
== 0 || !get_value(next
));
1990 next
= sparsebit_next_clear(s
, first
);
1991 if (sparsebit_is_set_num(s
, first
, num
)) {
1992 assert(next
== 0 || next
> last
);
1994 next
= sparsebit_next_set(s
, first
- 1);
1995 else if (sparsebit_any_set(s
))
1996 next
= sparsebit_first_set(s
);
1999 assert(next
== first
);
2001 assert(sparsebit_is_clear(s
, first
) || next
<= last
);
2005 next
= sparsebit_next_set(s
, first
);
2006 if (sparsebit_is_clear_num(s
, first
, num
)) {
2007 assert(next
== 0 || next
> last
);
2009 next
= sparsebit_next_clear(s
, first
- 1);
2010 else if (sparsebit_any_clear(s
))
2011 next
= sparsebit_first_clear(s
);
2014 assert(next
== first
);
2016 assert(sparsebit_is_set(s
, first
) || next
<= last
);
2020 sparsebit_set_num(s
, first
, num
);
2021 assert(sparsebit_is_set_num(s
, first
, num
));
2022 assert(!sparsebit_is_clear_num(s
, first
, num
));
2023 assert(sparsebit_any_set(s
));
2024 assert(!sparsebit_all_clear(s
));
2025 if (num_ranges
== 1000)
2027 ranges
[num_ranges
++] = (struct range
)
2028 { .first
= first
, .last
= last
, .set
= true };
2031 sparsebit_clear_num(s
, first
, num
);
2032 assert(!sparsebit_is_set_num(s
, first
, num
));
2033 assert(sparsebit_is_clear_num(s
, first
, num
));
2034 assert(sparsebit_any_clear(s
));
2035 assert(!sparsebit_all_set(s
));
2036 if (num_ranges
== 1000)
2038 ranges
[num_ranges
++] = (struct range
)
2039 { .first
= first
, .last
= last
, .set
= false };
2042 sparsebit_validate_internal(s
);
2049 unsigned char get8(void)
2059 uint64_t get64(void)
2064 x
= (x
<< 8) | get8();
2065 x
= (x
<< 8) | get8();
2066 x
= (x
<< 8) | get8();
2067 x
= (x
<< 8) | get8();
2068 x
= (x
<< 8) | get8();
2069 x
= (x
<< 8) | get8();
2070 return (x
<< 8) | get8();
2075 s
= sparsebit_alloc();
2077 uint8_t op
= get8() & 0xf;
2078 uint64_t first
= get64();
2079 uint64_t last
= get64();
2081 operate(op
, first
, last
);