diff --git a/src/util/u_range_remap.c b/src/util/u_range_remap.c index 7447c28e9e0..03b35706e50 100644 --- a/src/util/u_range_remap.c +++ b/src/util/u_range_remap.c @@ -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) diff --git a/src/util/u_range_remap.h b/src/util/u_range_remap.h index 0da8544f63a..9b97a5b4610 100644 --- a/src/util/u_range_remap.h +++ b/src/util/u_range_remap.h @@ -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