util/range_remap: add util_range_switch_to_sorted_array() helper

This creates an array of the linked list elements to be used for
faster binary searches in the following patch, and frees the old
linked list.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37754>
This commit is contained in:
Timothy Arceri 2025-10-07 11:36:02 +11:00 committed by Marge Bot
parent f8326657f8
commit 2ea58908ae
2 changed files with 37 additions and 0 deletions

View file

@ -171,6 +171,36 @@ insert_end:
return &lre->entry;
}
void
util_range_switch_to_sorted_array(struct range_remap *r_remap)
{
r_remap->sorted_array_length = list_length(&r_remap->r_list);
if (r_remap->sorted_array) {
ralloc_free(r_remap->sorted_array);
r_remap->sorted_array = NULL;
}
if (r_remap->sorted_array_length == 0)
return;
r_remap->sorted_array = rzalloc_array(r_remap, struct range_entry,
r_remap->sorted_array_length);
unsigned i = 0;
list_for_each_entry(struct list_range_entry, e, &r_remap->r_list, node) {
r_remap->sorted_array[i].start = e->entry.start;
r_remap->sorted_array[i].end = e->entry.end;
r_remap->sorted_array[i].ptr = e->entry.ptr;
i++;
}
/* Free linked list and reset head */
list_inithead(&r_remap->r_list);
ralloc_free(r_remap->list_mem_ctx);
r_remap->list_mem_ctx = ralloc_context(r_remap);
}
/* Return the range entry that maps to n or NULL if no match found. */
struct range_entry *
util_range_remap(unsigned n, const struct range_remap *r_remap)

View file

@ -32,6 +32,10 @@ struct range_remap {
struct list_head r_list;
void *list_mem_ctx;
/* Sorted array built from the linked list for fast binary searches */
struct range_entry *sorted_array;
unsigned sorted_array_length;
};
struct range_entry {
@ -57,6 +61,9 @@ util_create_range_remap(void);
struct range_remap *
util_reset_range_remap(struct range_remap *r_remap);
void
util_range_switch_to_sorted_array(struct range_remap *r_remap);
#ifdef __cplusplus
}
#endif