Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ if with_tests
'tests/u_debug_stack_test.cpp',
'tests/u_debug_test.cpp',
'tests/u_dl_test.cpp',
'tests/u_idalloc_test.cpp',
'tests/u_memstream_test.cpp',
'tests/u_printf_test.cpp',
'tests/u_qsort_test.cpp',
Expand Down
63 changes: 63 additions & 0 deletions src/util/tests/u_idalloc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* SPDX-License-Identifier: GPL-2.0-only */

#include <gtest/gtest.h>

#include "util/u_idalloc.h"

class idalloc_sparse : public testing::TestWithParam<bool> {
protected:
struct util_idalloc_sparse allocator;

void SetUp() override { util_idalloc_sparse_init(&allocator); }
void TearDown() override { util_idalloc_sparse_fini(&allocator); }

unsigned alloc()
{
return GetParam() ? util_idalloc_sparse_alloc_range(&allocator, 1)
: util_idalloc_sparse_alloc(&allocator);
}

void check_rollover(bool reserve_tail)
{
const unsigned limit = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(&allocator);
const unsigned max_elements =
UTIL_IDALLOC_MAX_ELEMS_PER_SEGMENT(&allocator);

for (unsigned i = 0; i < limit - reserve_tail; i++)
ASSERT_EQ(alloc(), i);
if (reserve_tail)
util_idalloc_sparse_reserve(&allocator, limit - 1);

ASSERT_EQ(allocator.segment[0].num_elements, max_elements);

/* A stale search hint must not grow a full segment's bitmap. */
const unsigned first = alloc();
const unsigned second = alloc();
EXPECT_EQ(first, limit);
EXPECT_EQ(second, limit + 1);
EXPECT_EQ(allocator.segment[0].num_elements, max_elements);
EXPECT_EQ(allocator.segment[0].num_set_elements, max_elements);

util_idalloc_sparse_free(&allocator, first);
util_idalloc_sparse_free(&allocator, second);
EXPECT_EQ(allocator.segment[1].num_set_elements, 0u);

/* Freeing the old segment's tail makes it eligible for reuse. */
util_idalloc_sparse_free(&allocator, limit - 1);
EXPECT_EQ(alloc(), limit - 1);
EXPECT_EQ(alloc(), limit);
EXPECT_EQ(allocator.segment[0].num_elements, max_elements);
}
};

TEST_P(idalloc_sparse, allocated_tail)
{
check_rollover(false);
}

TEST_P(idalloc_sparse, reserved_tail)
{
check_rollover(true);
}

INSTANTIATE_TEST_SUITE_P(single_id, idalloc_sparse, testing::Bool());
57 changes: 36 additions & 21 deletions src/util/u_idalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,39 @@ util_idalloc_fini(struct util_idalloc *buf)
free(buf->data);
}

unsigned
util_idalloc_alloc(struct util_idalloc *buf)
static bool
util_idalloc_try_alloc(struct util_idalloc *buf, unsigned max_elements,
unsigned *id)
{
unsigned num_elements = buf->num_elements;
unsigned num_elements = MIN2(buf->num_elements, max_elements);

for (unsigned i = buf->lowest_free_idx; i < num_elements; i++) {
if (buf->data[i] == 0xffffffff)
continue;
unsigned i = buf->lowest_free_idx;
while (i < num_elements && buf->data[i] == UINT32_MAX)
i++;

unsigned bit = ffs(~buf->data[i]) - 1;
buf->data[i] |= 1u << bit;
buf->lowest_free_idx = i;
buf->num_set_elements = MAX2(buf->num_set_elements, i + 1);
return i * 32 + bit;
}
buf->lowest_free_idx = i;
if (i >= max_elements)
return false;

/* No slots available, resize and return the first free. */
util_idalloc_resize(buf, MAX2(num_elements, 1) * 2);
if (i == num_elements)
util_idalloc_resize(buf, MIN2(MAX2(num_elements, 1) * 2, max_elements));

buf->lowest_free_idx = num_elements;
buf->data[num_elements] |= 1;
buf->num_set_elements = MAX2(buf->num_set_elements, num_elements + 1);
return num_elements * 32;
unsigned bit = ffs(~buf->data[i]) - 1;
buf->data[i] |= 1u << bit;
buf->num_set_elements = MAX2(buf->num_set_elements, i + 1);
*id = i * 32 + bit;
return true;
}

unsigned
util_idalloc_alloc(struct util_idalloc *buf)
{
unsigned id;
if (!util_idalloc_try_alloc(buf, UINT32_MAX / 32 + 1, &id)) {
fprintf(stderr, "mesa: util_idalloc_alloc: all 2^32 IDs are used\n");
abort();
}
return id;
}

static unsigned
Expand Down Expand Up @@ -257,11 +267,13 @@ unsigned
util_idalloc_sparse_alloc(struct util_idalloc_sparse *buf)
{
unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
unsigned max_elements = UTIL_IDALLOC_MAX_ELEMS_PER_SEGMENT(buf);

/* The last word may have filled since the free index was recorded. */
for (unsigned i = 0; i < ARRAY_SIZE(buf->segment); i++) {
if (buf->segment[i].lowest_free_idx <
UTIL_IDALLOC_MAX_ELEMS_PER_SEGMENT(buf))
return max_ids * i + util_idalloc_alloc(&buf->segment[i]);
unsigned id;
if (util_idalloc_try_alloc(&buf->segment[i], max_elements, &id))
return max_ids * i + id;
}

fprintf(stderr, "mesa: util_idalloc_sparse_alloc: "
Expand All @@ -273,6 +285,9 @@ util_idalloc_sparse_alloc(struct util_idalloc_sparse *buf)
unsigned
util_idalloc_sparse_alloc_range(struct util_idalloc_sparse *buf, unsigned num)
{
if (num == 1)
return util_idalloc_sparse_alloc(buf);

unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
unsigned num_elems = DIV_ROUND_UP(num, 32);

Expand Down