util/idalloc: add util_idalloc_alloc_range

v2: fixed infinite loop (Pierre-Eric)

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com> (v1)
Reviewed-by: Marek Olšák <marek.olsak@amd.com> (v2)
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11493>
This commit is contained in:
Marek Olšák 2021-05-17 16:37:15 -04:00 committed by Marge Bot
parent f29823df66
commit b48998926c
2 changed files with 59 additions and 0 deletions

View File

@ -85,6 +85,62 @@ util_idalloc_alloc(struct util_idalloc *buf)
return num_elements * 32;
}
static unsigned
find_free_block(struct util_idalloc *buf, unsigned start)
{
for (unsigned i = start; i < buf->num_elements; i++) {
if (!buf->data[i])
return i;
}
return buf->num_elements;
}
/* Allocate a range of consecutive IDs. Return the first ID. */
unsigned
util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num)
{
if (num == 1)
return util_idalloc_alloc(buf);
unsigned num_alloc = DIV_ROUND_UP(num, 32);
unsigned num_elements = buf->num_elements;
unsigned base = find_free_block(buf, buf->lowest_free_idx);
while (1) {
unsigned i;
for (i = base;
i < num_elements && i - base < num_alloc && !buf->data[i]; i++);
if (i - base == num_alloc)
goto ret; /* found */
if (i == num_elements)
break; /* not found */
/* continue searching */
base = !buf->data[i] ? i : i + 1;
}
/* No slots available, allocate more. */
util_idalloc_resize(buf, num_elements * 2 + num_alloc);
ret:
/* Mark the bits as used. */
for (unsigned i = base; i < base + num_alloc - (num % 32 != 0); i++)
buf->data[i] = 0xffffffff;
if (num % 32 != 0)
buf->data[base + num_alloc - 1] |= BITFIELD_MASK(num % 32);
if (buf->lowest_free_idx == base)
buf->lowest_free_idx = base + num / 32;
/* Validate this algorithm. */
for (unsigned i = 0; i < num; i++)
assert(util_idalloc_exists(buf, base * 32 + i));
return base * 32;
}
void
util_idalloc_free(struct util_idalloc *buf, unsigned id)
{

View File

@ -58,6 +58,9 @@ util_idalloc_fini(struct util_idalloc *buf);
unsigned
util_idalloc_alloc(struct util_idalloc *buf);
unsigned
util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num);
void
util_idalloc_free(struct util_idalloc *buf, unsigned id);