diff --git a/src/util/u_idalloc.h b/src/util/u_idalloc.h index c6ba9f8d0fc..83f13789510 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 {