libsepol: Create function ebitmap_highest_set_bit()

Create the function ebitmap_highest_set_bit() which returns the position
of the highest bit set in the ebitmap.

The return value is valid only if the ebitmap is not empty. An empty
ebitmap will return 0.

Signed-off-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
James Carter 2021-02-05 09:07:59 -05:00 committed by Petr Lautrbach
parent 49ff851c98
commit 8f5409cf4c
2 changed files with 21 additions and 0 deletions

View File

@ -94,6 +94,7 @@ extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);
extern int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2);
extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit);
extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value);
extern unsigned int ebitmap_highest_set_bit(ebitmap_t * e);
extern void ebitmap_destroy(ebitmap_t * e);
extern int ebitmap_read(ebitmap_t * e, void *fp);

View File

@ -344,6 +344,26 @@ int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value)
return 0;
}
unsigned int ebitmap_highest_set_bit(ebitmap_t * e)
{
ebitmap_node_t *n;
MAPTYPE map;
unsigned int pos = 0;
n = e->node;
if (!n)
return 0;
while (n->next)
n = n->next;
map = n->map;
while (map >>= 1)
pos++;
return n->startbit + pos;
}
void ebitmap_destroy(ebitmap_t * e)
{
ebitmap_node_t *n, *temp;