From f29823df665ace68cbbad5aae54ae66e73266038 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Mon, 17 May 2021 15:44:50 -0400 Subject: [PATCH] util/idalloc: add exists and foreach helpers Reviewed-by: Pierre-Eric Pelloux-Prayer Part-of: --- src/util/u_idalloc.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/util/u_idalloc.h b/src/util/u_idalloc.h index c6ba9f8..83f1378 100644 --- a/src/util/u_idalloc.h +++ b/src/util/u_idalloc.h @@ -25,6 +25,12 @@ * **************************************************************************/ +/* Allocator of IDs (e.g. OpenGL object IDs), or simply an allocator of + * numbers. + * + * The allocator uses a bit array to track allocated IDs. + */ + #ifndef U_IDALLOC_H #define U_IDALLOC_H @@ -58,6 +64,20 @@ util_idalloc_free(struct util_idalloc *buf, unsigned id); void util_idalloc_reserve(struct util_idalloc *buf, unsigned id); +static inline bool +util_idalloc_exists(struct util_idalloc *buf, unsigned id) +{ + return id / 32 < buf->num_elements && + buf->data[id / 32] & BITFIELD_BIT(id % 32); +} + +#define util_idalloc_foreach(buf, id) \ + for (uint32_t i = 0, mask = (buf)->num_elements ? (buf)->data[0] : 0, id, \ + count = (buf)->num_elements; \ + i < count; mask = ++i < count ? (buf)->data[i] : 0) \ + while (mask) \ + if ((id = i * 32 + u_bit_scan(&mask)), true) + /* Thread-safe variant. */ struct util_idalloc_mt { -- 2.7.4