]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: generalize sorting code
authorLennart Poettering <lennart@poettering.net>
Mon, 20 Sep 2021 12:07:42 +0000 (14:07 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 23 Sep 2021 15:23:45 +0000 (17:23 +0200)
Let's make this generic, so that we can reuse it elsewhere later.

src/boot/efi/boot.c
src/boot/efi/util.c
src/boot/efi/util.h

index 074a85659829d48eb473cf2692ffaf21d97a0c37..734ae56fb1912745e6282395713ec418e2c9db19 100644 (file)
@@ -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) {
index 68053c2c39b22f8856b00684fc86f8c4be59260a..e500069d512309794664782898c3c7aabd63267a 100644 (file)
@@ -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,
index 45e6b940cbe3fc3b49e75daa4effa20eba3a4d19..ea32a76166ac6f166be144e0826ea71c3eda32da 100644 (file)
@@ -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);