From 561a9f94a29c5ab3582255a9de7de818f32a3396 Mon Sep 17 00:00:00 2001 From: Dragos Tatulea Date: Fri, 18 Mar 2016 12:58:50 +0100 Subject: [PATCH] idmap: add api for finding a certain id in map --- src/idmap.c | 12 ++++++++++++ src/idmap.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/idmap.c b/src/idmap.c index c097eb4b..da32d960 100644 --- a/src/idmap.c +++ b/src/idmap.c @@ -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 diff --git a/src/idmap.h b/src/idmap.h index ebda1772..97a6f04e 100644 --- a/src/idmap.h +++ b/src/idmap.h @@ -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);