{
unsigned num_elements = buf->num_elements;
- for (unsigned i = 0; i < num_elements / 32; i++) {
+ for (unsigned i = buf->lowest_free_idx; i < num_elements / 32; i++) {
if (buf->data[i] == 0xffffffff)
continue;
unsigned bit = ffs(~buf->data[i]) - 1;
buf->data[i] |= 1u << bit;
+ buf->lowest_free_idx = i;
return i * 32 + bit;
}
/* No slots available, resize and return the first free. */
util_idalloc_resize(buf, num_elements * 2);
+ buf->lowest_free_idx = num_elements / 32;
+
buf->data[num_elements / 32] |= 1 << (num_elements % 32);
return num_elements;
util_idalloc_free(struct util_idalloc *buf, unsigned id)
{
assert(id < buf->num_elements);
- buf->data[id / 32] &= ~(1 << (id % 32));
+ unsigned idx = id / 32;
+ buf->lowest_free_idx = MIN2(idx, buf->lowest_free_idx);
+ buf->data[idx] &= ~(1 << (id % 32));
}
void