idmap: add api for finding a certain id in map

This commit is contained in:
Dragos Tatulea 2016-03-18 12:58:50 +01:00 committed by Denis Kenzior
parent 9173d7e2e4
commit 561a9f94a2
2 changed files with 13 additions and 0 deletions

View File

@ -166,6 +166,18 @@ void idmap_take(struct idmap *idmap, unsigned int id)
idmap->bits[offset] |= 1UL << (bit % BITS_PER_LONG);
}
int idmap_find(struct idmap *idmap, unsigned int id)
{
unsigned int bit = id - idmap->min;
unsigned int offset;
if (bit >= idmap->size)
return 0;
offset = bit / BITS_PER_LONG;
return (idmap->bits[offset] & (1UL << (bit % BITS_PER_LONG))) != 0;
}
/*
* Allocate the next bit skipping the ids up to and including last. If there
* is no free ids until the max id is encountered, the counter is wrapped back

View File

@ -25,6 +25,7 @@ struct idmap *idmap_new(unsigned int size);
void idmap_free(struct idmap *idmap);
void idmap_put(struct idmap *idmap, unsigned int id);
void idmap_take(struct idmap *idmap, unsigned int id);
int idmap_find(struct idmap *idmap, unsigned int id);
unsigned int idmap_alloc(struct idmap *idmap);
unsigned int idmap_alloc_next(struct idmap *idmap, unsigned int last);
struct idmap *idmap_new_from_range(unsigned int min, unsigned int max);