11b49b8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
11b49b8
From: Javier Martinez Canillas <javierm@redhat.com>
11b49b8
Date: Mon, 21 Jan 2019 17:33:13 +0100
11b49b8
Subject: [PATCH] blscfg: store the BLS entries in a sorted linked list
11b49b8
11b49b8
The parsed BLS entries are stored in an array, that it's sorted
11b49b8
using the quick sort algorithm before populating the boot menu.
11b49b8
11b49b8
This works on the assumption that all BLS entries are parsed at
11b49b8
the same time and that are displayed just after being retrieved.
11b49b8
11b49b8
But the support could be made more flexible, and have different
11b49b8
commands to load and display the entries. Keeping the BLS in a
11b49b8
sorted link simplify the code considerably, while not making it
11b49b8
much less efficient.
11b49b8
11b49b8
While being there, mark entries that have been used to populate
11b49b8
the boot menu so multiple calls to the blscfg command don't add
11b49b8
duplicated entries.
11b49b8
11b49b8
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
11b49b8
---
11b49b8
 grub-core/Makefile.core.def    |   1 -
11b49b8
 grub-core/commands/blscfg.c    | 177 ++++++++++++----------------
11b49b8
 grub-core/commands/bls_qsort.h | 255 -----------------------------------------
11b49b8
 3 files changed, 71 insertions(+), 362 deletions(-)
11b49b8
 delete mode 100644 grub-core/commands/bls_qsort.h
11b49b8
11b49b8
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
11b49b8
index 017080bd599..8a00c6177e1 100644
11b49b8
--- a/grub-core/Makefile.core.def
11b49b8
+++ b/grub-core/Makefile.core.def
11b49b8
@@ -796,7 +796,6 @@ module = {
11b49b8
 module = {
11b49b8
   name = blscfg;
11b49b8
   common = commands/blscfg.c;
11b49b8
-  common = commands/bls_qsort.h;
11b49b8
   common = commands/loadenv.h;
11b49b8
   enable = powerpc_ieee1275;
11b49b8
   enable = efi;
11b49b8
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
11b49b8
index c432c6ba27a..304d73908ae 100644
11b49b8
--- a/grub-core/commands/blscfg.c
11b49b8
+++ b/grub-core/commands/blscfg.c
11b49b8
@@ -19,6 +19,7 @@
11b49b8
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
11b49b8
  */
11b49b8
 
11b49b8
+#include <grub/list.h>
11b49b8
 #include <grub/types.h>
11b49b8
 #include <grub/misc.h>
11b49b8
 #include <grub/mm.h>
11b49b8
@@ -36,7 +37,6 @@
11b49b8
 
11b49b8
 GRUB_MOD_LICENSE ("GPLv3+");
11b49b8
 
11b49b8
-#include "bls_qsort.h"
11b49b8
 #include "loadenv.h"
11b49b8
 
11b49b8
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
11b49b8
@@ -54,45 +54,17 @@ struct keyval
11b49b8
 
11b49b8
 struct bls_entry
11b49b8
 {
11b49b8
+  struct bls_entry *next;
11b49b8
+  struct bls_entry **prev;
11b49b8
   struct keyval **keyvals;
11b49b8
   int nkeyvals;
11b49b8
   char *filename;
11b49b8
+  bool visible;
11b49b8
 };
11b49b8
 
11b49b8
-static struct bls_entry **entries;
11b49b8
-static int nentries;
11b49b8
+static struct bls_entry *entries = NULL;
11b49b8
 
11b49b8
-static struct bls_entry *bls_new_entry(void)
11b49b8
-{
11b49b8
-  struct bls_entry **new_entries;
11b49b8
-  struct bls_entry *entry;
11b49b8
-  int new_n = nentries + 1;
11b49b8
-
11b49b8
-  new_entries = grub_realloc (entries,  new_n * sizeof (struct bls_entry *));
11b49b8
-  if (!new_entries)
11b49b8
-    {
11b49b8
-      grub_error (GRUB_ERR_OUT_OF_MEMORY,
11b49b8
-		  "couldn't find space for BLS entry list");
11b49b8
-      return NULL;
11b49b8
-    }
11b49b8
-
11b49b8
-  entries = new_entries;
11b49b8
-
11b49b8
-  entry = grub_malloc (sizeof (*entry));
11b49b8
-  if (!entry)
11b49b8
-    {
11b49b8
-      grub_error (GRUB_ERR_OUT_OF_MEMORY,
11b49b8
-		  "couldn't find space for BLS entry list");
11b49b8
-      return NULL;
11b49b8
-    }
11b49b8
-
11b49b8
-  grub_memset (entry, 0, sizeof (*entry));
11b49b8
-  entries[nentries] = entry;
11b49b8
-
11b49b8
-  nentries = new_n;
11b49b8
-
11b49b8
-  return entry;
11b49b8
-}
11b49b8
+#define FOR_BLS_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
11b49b8
 
11b49b8
 static int bls_add_keyval(struct bls_entry *entry, char *key, char *val)
11b49b8
 {
11b49b8
@@ -138,24 +110,6 @@ static int bls_add_keyval(struct bls_entry *entry, char *key, char *val)
11b49b8
   return 0;
11b49b8
 }
11b49b8
 
11b49b8
-static void bls_free_entry(struct bls_entry *entry)
11b49b8
-{
11b49b8
-  int i;
11b49b8
-
11b49b8
-  for (i = 0; i < entry->nkeyvals; i++)
11b49b8
-    {
11b49b8
-      struct keyval *kv = entry->keyvals[i];
11b49b8
-      grub_free ((void *)kv->key);
11b49b8
-      grub_free (kv->val);
11b49b8
-      grub_free (kv);
11b49b8
-    }
11b49b8
-
11b49b8
-  grub_free (entry->keyvals);
11b49b8
-  grub_free (entry->filename);
11b49b8
-  grub_memset (entry, 0, sizeof (*entry));
11b49b8
-  grub_free (entry);
11b49b8
-}
11b49b8
-
11b49b8
 /* Find they value of the key named by keyname.  If there are allowed to be
11b49b8
  * more than one, pass a pointer to an int set to -1 the first time, and pass
11b49b8
  * the same pointer through each time after, and it'll return them in sorted
11b49b8
@@ -387,43 +341,17 @@ split_cmp(char *nvr0, char *nvr1, int has_name)
11b49b8
   return ret;
11b49b8
 }
11b49b8
 
11b49b8
-/* return 1: p0 is newer than p1 */
11b49b8
-/*        0: p0 and p1 are the same version */
11b49b8
-/*       -1: p1 is newer than p0 */
11b49b8
-static int bls_cmp(const void *p0, const void *p1, void *state)
11b49b8
+/* return 1: e0 is newer than e1 */
11b49b8
+/*        0: e0 and e1 are the same version */
11b49b8
+/*       -1: e1 is newer than e0 */
11b49b8
+static int bls_cmp(const struct bls_entry *e0, const struct bls_entry *e1)
11b49b8
 {
11b49b8
-  struct bls_entry * e0 = *(struct bls_entry **)p0;
11b49b8
-  struct bls_entry * e1 = *(struct bls_entry **)p1;
11b49b8
-  bool use_version = *(bool *)state;
11b49b8
-  char *v0, *v1;
11b49b8
   char *id0, *id1;
11b49b8
-  int l, r;
11b49b8
-
11b49b8
-  if (use_version)
11b49b8
-    {
11b49b8
-      v0 = grub_strdup(bls_get_val(e0, "version", NULL));
11b49b8
-      v1 = grub_strdup(bls_get_val(e1, "version", NULL));
11b49b8
-
11b49b8
-      r = split_cmp(v0, v1, 0);
11b49b8
-
11b49b8
-      grub_free(v0);
11b49b8
-      grub_free(v1);
11b49b8
-
11b49b8
-      if (r != 0)
11b49b8
-	return r;
11b49b8
-    }
11b49b8
+  int r;
11b49b8
 
11b49b8
   id0 = grub_strdup(e0->filename);
11b49b8
   id1 = grub_strdup(e1->filename);
11b49b8
 
11b49b8
-  l = grub_strlen(id0);
11b49b8
-  if (l > 5 && grub_strcmp(id0 + l - 5, ".conf"))
11b49b8
-    id0[l-5] = '\0';
11b49b8
-
11b49b8
-  l = grub_strlen(id1);
11b49b8
-  if (l > 5 && grub_strcmp(id1 + l - 5, ".conf"))
11b49b8
-    id1[l-5] = '\0';
11b49b8
-
11b49b8
   r = split_cmp(id0, id1, 1);
11b49b8
 
11b49b8
   grub_free(id0);
11b49b8
@@ -432,6 +360,53 @@ static int bls_cmp(const void *p0, const void *p1, void *state)
11b49b8
   return r;
11b49b8
 }
11b49b8
 
11b49b8
+static void list_add_tail(grub_list_t head, grub_list_t item)
11b49b8
+{
11b49b8
+  item->next = head;
11b49b8
+  if (head->prev)
11b49b8
+    (*head->prev)->next = item;
11b49b8
+  item->prev = head->prev;
11b49b8
+  head->prev = &ite;;
11b49b8
+}
11b49b8
+
11b49b8
+static int bls_add_entry(struct bls_entry *entry)
11b49b8
+{
11b49b8
+  struct bls_entry *e, *last = NULL;
11b49b8
+  int rc;
11b49b8
+
11b49b8
+  if (!entries) {
11b49b8
+    grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
11b49b8
+    entries = entry;
11b49b8
+    return 0;
11b49b8
+  }
11b49b8
+
11b49b8
+  FOR_BLS_ENTRIES(e) {
11b49b8
+    rc = bls_cmp(entry, e);
11b49b8
+
11b49b8
+    if (!rc)
11b49b8
+      return GRUB_ERR_BAD_ARGUMENT;
11b49b8
+
11b49b8
+    if (rc == 1) {
11b49b8
+      grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
11b49b8
+      list_add_tail (GRUB_AS_LIST (e), GRUB_AS_LIST (entry));
11b49b8
+      if (e == entries) {
11b49b8
+	entries = entry;
11b49b8
+	entry->prev = NULL;
11b49b8
+      }
11b49b8
+      return 0;
11b49b8
+    }
11b49b8
+    last = e;
11b49b8
+  }
11b49b8
+
11b49b8
+  if (last) {
11b49b8
+    grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
11b49b8
+    last->next = entry;
11b49b8
+    entry->prev = &las;;
11b49b8
+  }
11b49b8
+
11b49b8
+  return 0;
11b49b8
+}
11b49b8
+
11b49b8
 struct read_entry_info {
11b49b8
   const char *devid;
11b49b8
   const char *dirname;
11b49b8
@@ -442,6 +417,7 @@ static int read_entry (
11b49b8
     const struct grub_dirhook_info *dirhook_info UNUSED,
11b49b8
     void *data)
11b49b8
 {
11b49b8
+  int rc = 0;
11b49b8
   grub_size_t n;
11b49b8
   char *p;
11b49b8
   grub_file_t f = NULL;
11b49b8
@@ -471,7 +447,7 @@ static int read_entry (
11b49b8
   if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
11b49b8
     goto finish;
11b49b8
 
11b49b8
-  entry = bls_new_entry();
11b49b8
+  entry = grub_zalloc (sizeof (*entry));
11b49b8
   if (!entry)
11b49b8
     goto finish;
11b49b8
 
11b49b8
@@ -485,7 +461,6 @@ static int read_entry (
11b49b8
     {
11b49b8
       char *buf;
11b49b8
       char *separator;
11b49b8
-      int rc;
11b49b8
 
11b49b8
       buf = grub_file_getline (f);
11b49b8
       if (!buf)
11b49b8
@@ -519,6 +494,9 @@ static int read_entry (
11b49b8
 	break;
11b49b8
     }
11b49b8
 
11b49b8
+    if (!rc)
11b49b8
+      bls_add_entry(entry);
11b49b8
+
11b49b8
 finish:
11b49b8
   grub_free (p);
11b49b8
 
11b49b8
@@ -779,10 +757,10 @@ struct find_entry_info {
11b49b8
 static int find_entry (struct find_entry_info *info)
11b49b8
 {
11b49b8
   struct read_entry_info read_entry_info;
11b49b8
+  struct bls_entry *entry = NULL;
11b49b8
   grub_fs_t blsdir_fs = NULL;
11b49b8
   grub_device_t blsdir_dev = NULL;
11b49b8
   const char *blsdir = NULL;
11b49b8
-  bool use_version = true;
11b49b8
   int fallback = 0;
11b49b8
   int r = 0;
11b49b8
 
11b49b8
@@ -810,7 +788,7 @@ read_fallback:
11b49b8
 	} while (e);
11b49b8
   }
11b49b8
 
11b49b8
-  if (!nentries && !fallback) {
11b49b8
+  if (!entries && !fallback) {
11b49b8
     read_entry_info.dirname = "/boot" GRUB_BLS_CONFIG_PATH;
11b49b8
     grub_dprintf ("blscfg", "Entries weren't found in %s, fallback to %s\n",
11b49b8
 		  blsdir, read_entry_info.dirname);
11b49b8
@@ -818,27 +796,14 @@ read_fallback:
11b49b8
     goto read_fallback;
11b49b8
   }
11b49b8
 
11b49b8
-  grub_dprintf ("blscfg", "Sorting %d entries\n", nentries);
11b49b8
+  grub_dprintf ("blscfg", "%s Creating entries from bls\n", __func__);
11b49b8
+  FOR_BLS_ENTRIES(entry) {
11b49b8
+    if (entry->visible)
11b49b8
+      continue;
11b49b8
 
11b49b8
-  for (r = 0; r < nentries && use_version; r++) {
11b49b8
-    if (!bls_get_val(entries[r], "version", NULL))
11b49b8
-      use_version = false;
11b49b8
+    create_entry(entry);
11b49b8
+    entry->visible = true;
11b49b8
   }
11b49b8
-
11b49b8
-  bls_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, &use_version);
11b49b8
-
11b49b8
-  grub_dprintf ("blscfg", "%s Creating %d entries from bls\n", __func__, nentries);
11b49b8
-  for (r = nentries - 1; r >= 0; r--)
11b49b8
-      create_entry(entries[r]);
11b49b8
-
11b49b8
-  for (r = 0; r < nentries; r++)
11b49b8
-      bls_free_entry (entries[r]);
11b49b8
-
11b49b8
-  nentries = 0;
11b49b8
-
11b49b8
-  grub_free (entries);
11b49b8
-  entries = NULL;
11b49b8
-
11b49b8
   return 0;
11b49b8
 }
11b49b8
 
11b49b8
diff --git a/grub-core/commands/bls_qsort.h b/grub-core/commands/bls_qsort.h
11b49b8
deleted file mode 100644
11b49b8
index 572765fa3f2..00000000000
11b49b8
--- a/grub-core/commands/bls_qsort.h
11b49b8
+++ /dev/null
11b49b8
@@ -1,255 +0,0 @@
11b49b8
-/* quicksort
11b49b8
- * This file from the GNU C Library.
11b49b8
- * Copyright (C) 1991-2016 Free Software Foundation, Inc.
11b49b8
- * Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
11b49b8
- *
11b49b8
- *  GRUB  --  GRand Unified Bootloader
11b49b8
- *
11b49b8
- *  GRUB is free software: you can redistribute it and/or modify
11b49b8
- *  it under the terms of the GNU General Public License as published by
11b49b8
- *  the Free Software Foundation, either version 3 of the License, or
11b49b8
- *  (at your option) any later version.
11b49b8
- *
11b49b8
- *  GRUB is distributed in the hope that it will be useful,
11b49b8
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11b49b8
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11b49b8
- *  GNU General Public License for more details.
11b49b8
- *
11b49b8
- *  You should have received a copy of the GNU General Public License
11b49b8
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
11b49b8
- */
11b49b8
-
11b49b8
-/* If you consider tuning this algorithm, you should consult first:
11b49b8
-   Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
11b49b8
-   Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993.  */
11b49b8
-
11b49b8
-#include <grub/types.h>
11b49b8
-#include <grub/misc.h>
11b49b8
-#include <grub/mm.h>
11b49b8
-
11b49b8
-#define CHAR_BIT 8
11b49b8
-
11b49b8
-/* Byte-wise swap two items of size SIZE. */
11b49b8
-#define SWAP(a, b, size)						      \
11b49b8
-  do									      \
11b49b8
-    {									      \
11b49b8
-      grub_size_t __size = (size);						      \
11b49b8
-      char *__a = (a), *__b = (b);					      \
11b49b8
-      do								      \
11b49b8
-	{								      \
11b49b8
-	  char __tmp = *__a;						      \
11b49b8
-	  *__a++ = *__b;						      \
11b49b8
-	  *__b++ = __tmp;						      \
11b49b8
-	} while (--__size > 0);						      \
11b49b8
-    } while (0)
11b49b8
-
11b49b8
-/* Discontinue quicksort algorithm when partition gets below this size.
11b49b8
-   This particular magic number was chosen to work best on a Sun 4/260. */
11b49b8
-#define MAX_THRESH 4
11b49b8
-
11b49b8
-/* Stack node declarations used to store unfulfilled partition obligations. */
11b49b8
-typedef struct
11b49b8
-  {
11b49b8
-    char *lo;
11b49b8
-    char *hi;
11b49b8
-  } stack_node;
11b49b8
-
11b49b8
-/* The next 4 #defines implement a very fast in-line stack abstraction. */
11b49b8
-/* The stack needs log (total_elements) entries (we could even subtract
11b49b8
-   log(MAX_THRESH)).  Since total_elements has type grub_size_t, we get as
11b49b8
-   upper bound for log (total_elements):
11b49b8
-   bits per byte (CHAR_BIT) * sizeof(grub_size_t).  */
11b49b8
-#define STACK_SIZE	(CHAR_BIT * sizeof(grub_size_t))
11b49b8
-#define PUSH(low, high)	((void) ((top->lo = (low)), (top->hi = (high)), ++top))
11b49b8
-#define	POP(low, high)	((void) (--top, (low = top->lo), (high = top->hi)))
11b49b8
-#define	STACK_NOT_EMPTY	(stack < top)
11b49b8
-
11b49b8
-typedef int (*grub_compar_d_fn_t) (const void *p0, const void *p1, void *state);
11b49b8
-
11b49b8
-/* Order size using quicksort.  This implementation incorporates
11b49b8
-   four optimizations discussed in Sedgewick:
11b49b8
-
11b49b8
-   1. Non-recursive, using an explicit stack of pointer that store the
11b49b8
-      next array partition to sort.  To save time, this maximum amount
11b49b8
-      of space required to store an array of SIZE_MAX is allocated on the
11b49b8
-      stack.  Assuming a 32-bit (64 bit) integer for grub_size_t, this needs
11b49b8
-      only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
11b49b8
-      Pretty cheap, actually.
11b49b8
-
11b49b8
-   2. Chose the pivot element using a median-of-three decision tree.
11b49b8
-      This reduces the probability of selecting a bad pivot value and
11b49b8
-      eliminates certain extraneous comparisons.
11b49b8
-
11b49b8
-   3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
11b49b8
-      insertion sort to order the MAX_THRESH items within each partition.
11b49b8
-      This is a big win, since insertion sort is faster for small, mostly
11b49b8
-      sorted array segments.
11b49b8
-
11b49b8
-   4. The larger of the two sub-partitions is always pushed onto the
11b49b8
-      stack first, with the algorithm then concentrating on the
11b49b8
-      smaller partition.  This *guarantees* no more than log (total_elems)
11b49b8
-      stack size is needed (actually O(1) in this case)!  */
11b49b8
-
11b49b8
-static inline void UNUSED
11b49b8
-bls_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
11b49b8
-	    grub_compar_d_fn_t cmp, void *arg)
11b49b8
-{
11b49b8
-  char *base_ptr = (char *) pbase;
11b49b8
-
11b49b8
-  const grub_size_t max_thresh = MAX_THRESH * size;
11b49b8
-
11b49b8
-  if (total_elems == 0)
11b49b8
-    /* Avoid lossage with unsigned arithmetic below.  */
11b49b8
-    return;
11b49b8
-
11b49b8
-  if (total_elems > MAX_THRESH)
11b49b8
-    {
11b49b8
-      char *lo = base_ptr;
11b49b8
-      char *hi = &lo[size * (total_elems - 1)];
11b49b8
-      stack_node stack[STACK_SIZE];
11b49b8
-      stack_node *top = stack;
11b49b8
-
11b49b8
-      PUSH (NULL, NULL);
11b49b8
-
11b49b8
-      while (STACK_NOT_EMPTY)
11b49b8
-        {
11b49b8
-          char *left_ptr;
11b49b8
-          char *right_ptr;
11b49b8
-
11b49b8
-	  /* Select median value from among LO, MID, and HI. Rearrange
11b49b8
-	     LO and HI so the three values are sorted. This lowers the
11b49b8
-	     probability of picking a pathological pivot value and
11b49b8
-	     skips a comparison for both the LEFT_PTR and RIGHT_PTR in
11b49b8
-	     the while loops. */
11b49b8
-
11b49b8
-	  char *mid = lo + size * ((hi - lo) / size >> 1);
11b49b8
-
11b49b8
-	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
11b49b8
-	    SWAP (mid, lo, size);
11b49b8
-	  if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
11b49b8
-	    SWAP (mid, hi, size);
11b49b8
-	  else
11b49b8
-	    goto jump_over;
11b49b8
-	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
11b49b8
-	    SWAP (mid, lo, size);
11b49b8
-	jump_over:;
11b49b8
-
11b49b8
-	  left_ptr  = lo + size;
11b49b8
-	  right_ptr = hi - size;
11b49b8
-
11b49b8
-	  /* Here's the famous ``collapse the walls'' section of quicksort.
11b49b8
-	     Gotta like those tight inner loops!  They are the main reason
11b49b8
-	     that this algorithm runs much faster than others. */
11b49b8
-	  do
11b49b8
-	    {
11b49b8
-	      while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
11b49b8
-		left_ptr += size;
11b49b8
-
11b49b8
-	      while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
11b49b8
-		right_ptr -= size;
11b49b8
-
11b49b8
-	      if (left_ptr < right_ptr)
11b49b8
-		{
11b49b8
-		  SWAP (left_ptr, right_ptr, size);
11b49b8
-		  if (mid == left_ptr)
11b49b8
-		    mid = right_ptr;
11b49b8
-		  else if (mid == right_ptr)
11b49b8
-		    mid = left_ptr;
11b49b8
-		  left_ptr += size;
11b49b8
-		  right_ptr -= size;
11b49b8
-		}
11b49b8
-	      else if (left_ptr == right_ptr)
11b49b8
-		{
11b49b8
-		  left_ptr += size;
11b49b8
-		  right_ptr -= size;
11b49b8
-		  break;
11b49b8
-		}
11b49b8
-	    }
11b49b8
-	  while (left_ptr <= right_ptr);
11b49b8
-
11b49b8
-          /* Set up pointers for next iteration.  First determine whether
11b49b8
-             left and right partitions are below the threshold size.  If so,
11b49b8
-             ignore one or both.  Otherwise, push the larger partition's
11b49b8
-             bounds on the stack and continue sorting the smaller one. */
11b49b8
-
11b49b8
-          if ((grub_size_t) (right_ptr - lo) <= max_thresh)
11b49b8
-            {
11b49b8
-              if ((grub_size_t) (hi - left_ptr) <= max_thresh)
11b49b8
-		/* Ignore both small partitions. */
11b49b8
-                POP (lo, hi);
11b49b8
-              else
11b49b8
-		/* Ignore small left partition. */
11b49b8
-                lo = left_ptr;
11b49b8
-            }
11b49b8
-          else if ((grub_size_t) (hi - left_ptr) <= max_thresh)
11b49b8
-	    /* Ignore small right partition. */
11b49b8
-            hi = right_ptr;
11b49b8
-          else if ((right_ptr - lo) > (hi - left_ptr))
11b49b8
-            {
11b49b8
-	      /* Push larger left partition indices. */
11b49b8
-              PUSH (lo, right_ptr);
11b49b8
-              lo = left_ptr;
11b49b8
-            }
11b49b8
-          else
11b49b8
-            {
11b49b8
-	      /* Push larger right partition indices. */
11b49b8
-              PUSH (left_ptr, hi);
11b49b8
-              hi = right_ptr;
11b49b8
-            }
11b49b8
-        }
11b49b8
-    }
11b49b8
-
11b49b8
-  /* Once the BASE_PTR array is partially sorted by quicksort the rest
11b49b8
-     is completely sorted using insertion sort, since this is efficient
11b49b8
-     for partitions below MAX_THRESH size. BASE_PTR points to the beginning
11b49b8
-     of the array to sort, and END_PTR points at the very last element in
11b49b8
-     the array (*not* one beyond it!). */
11b49b8
-
11b49b8
-#define min(x, y) ((x) < (y) ? (x) : (y))
11b49b8
-
11b49b8
-  {
11b49b8
-    char *const end_ptr = &base_ptr[size * (total_elems - 1)];
11b49b8
-    char *tmp_ptr = base_ptr;
11b49b8
-    char *thresh = min(end_ptr, base_ptr + max_thresh);
11b49b8
-    char *run_ptr;
11b49b8
-
11b49b8
-    /* Find smallest element in first threshold and place it at the
11b49b8
-       array's beginning.  This is the smallest array element,
11b49b8
-       and the operation speeds up insertion sort's inner loop. */
11b49b8
-
11b49b8
-    for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
11b49b8
-      if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
11b49b8
-        tmp_ptr = run_ptr;
11b49b8
-
11b49b8
-    if (tmp_ptr != base_ptr)
11b49b8
-      SWAP (tmp_ptr, base_ptr, size);
11b49b8
-
11b49b8
-    /* Insertion sort, running from left-hand-side up to right-hand-side.  */
11b49b8
-
11b49b8
-    run_ptr = base_ptr + size;
11b49b8
-    while ((run_ptr += size) <= end_ptr)
11b49b8
-      {
11b49b8
-	tmp_ptr = run_ptr - size;
11b49b8
-	while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
11b49b8
-	  tmp_ptr -= size;
11b49b8
-
11b49b8
-	tmp_ptr += size;
11b49b8
-        if (tmp_ptr != run_ptr)
11b49b8
-          {
11b49b8
-            char *trav;
11b49b8
-
11b49b8
-	    trav = run_ptr + size;
11b49b8
-	    while (--trav >= run_ptr)
11b49b8
-              {
11b49b8
-                char c = *trav;
11b49b8
-                char *hi, *lo;
11b49b8
-
11b49b8
-                for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
11b49b8
-                  *hi = *lo;
11b49b8
-                *hi = c;
11b49b8
-              }
11b49b8
-          }
11b49b8
-      }
11b49b8
-  }
11b49b8
-}
11b49b8
-