From 80b2f4d92ce4d7a52cbfb9b5efdcff96366fc37a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 20 Sep 2021 14:07:42 +0200 Subject: boot: generalize sorting code Let's make this generic, so that we can reuse it elsewhere later. --- src/boot/efi/boot.c | 21 ++------------------- src/boot/efi/util.c | 30 ++++++++++++++++++++++++++++++ src/boot/efi/util.h | 3 +++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index 074a856598..734ae56fb1 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -1529,7 +1529,7 @@ static VOID config_load_entries( } } -static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) { +static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) { INTN r; assert(a); @@ -1567,24 +1567,7 @@ static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) { static VOID config_sort_entries(Config *config) { assert(config); - for (UINTN i = 1; i < config->entry_count; i++) { - BOOLEAN more; - - more = FALSE; - for (UINTN k = 0; k < config->entry_count - i; k++) { - ConfigEntry *entry; - - if (config_entry_compare(config->entries[k], config->entries[k+1]) <= 0) - continue; - - entry = config->entries[k]; - config->entries[k] = config->entries[k+1]; - config->entries[k+1] = entry; - more = TRUE; - } - if (!more) - break; - } + sort_pointer_array((void**) config->entries, config->entry_count, (compare_pointer_func_t) config_entry_compare); } static INTN config_entry_find(Config *config, CHAR16 *id) { diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 68053c2c39..e500069d51 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -531,6 +531,36 @@ VOID clear_screen(UINTN attr) { uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); } +void sort_pointer_array( + VOID **array, + UINTN n_members, + compare_pointer_func_t compare) { + + assert(array || n_members == 0); + assert(compare); + + if (n_members <= 1) + return; + + for (UINTN i = 1; i < n_members; i++) { + BOOLEAN more = FALSE; + + for (UINTN k = 0; k < n_members - i; k++) { + void *entry; + + if (compare(array[k], array[k+1]) <= 0) + continue; + + entry = array[k]; + array[k] = array[k+1]; + array[k+1] = entry; + more = TRUE; + } + if (!more) + break; + } +} + EFI_STATUS get_file_info_harder( EFI_FILE_HANDLE handle, EFI_FILE_INFO **ret, diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index 45e6b940cb..ea32a76166 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -98,6 +98,9 @@ static inline VOID *mempmem_safe(const VOID *haystack, UINTN haystack_len, const VOID print_at(UINTN x, UINTN y, UINTN attr, const CHAR16 *str); VOID clear_screen(UINTN attr); +typedef INTN (*compare_pointer_func_t)(const VOID *a, const VOID *b); +void sort_pointer_array(VOID **array, UINTN n_members, compare_pointer_func_t compare); + EFI_STATUS get_file_info_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **ret, UINTN *ret_size); EFI_STATUS readdir_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **buffer, UINTN *buffer_size); -- cgit v1.2.3