d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Peter Jones <pjones@redhat.com>
d9d99f
Date: Fri, 9 Dec 2016 15:39:47 -0500
d9d99f
Subject: [PATCH] Add quicksort implementation
d9d99f
d9d99f
This will be used to sort the boot menu entries that are read from
d9d99f
the BootLoaderSpec config files.
d9d99f
---
d9d99f
 grub-core/kern/qsort.c | 279 +++++++++++++++++++++++++++++++++++++++++++++++++
d9d99f
 include/grub/misc.h    |  15 +++
d9d99f
 2 files changed, 294 insertions(+)
d9d99f
 create mode 100644 grub-core/kern/qsort.c
d9d99f
d9d99f
diff --git a/grub-core/kern/qsort.c b/grub-core/kern/qsort.c
d9d99f
new file mode 100644
b71686
index 000000000..7f3fc9ffd
d9d99f
--- /dev/null
d9d99f
+++ b/grub-core/kern/qsort.c
d9d99f
@@ -0,0 +1,279 @@
d9d99f
+/* quicksort
d9d99f
+ * This file from the GNU C Library.
d9d99f
+ * Copyright (C) 1991-2016 Free Software Foundation, Inc.
d9d99f
+ * Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
d9d99f
+ *
d9d99f
+ *  GRUB  --  GRand Unified Bootloader
d9d99f
+ *
d9d99f
+ *  GRUB is free software: you can redistribute it and/or modify
d9d99f
+ *  it under the terms of the GNU General Public License as published by
d9d99f
+ *  the Free Software Foundation, either version 3 of the License, or
d9d99f
+ *  (at your option) any later version.
d9d99f
+ *
d9d99f
+ *  GRUB is distributed in the hope that it will be useful,
d9d99f
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d9d99f
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d9d99f
+ *  GNU General Public License for more details.
d9d99f
+ *
d9d99f
+ *  You should have received a copy of the GNU General Public License
d9d99f
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d9d99f
+ */
d9d99f
+
d9d99f
+/* If you consider tuning this algorithm, you should consult first:
d9d99f
+   Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
d9d99f
+   Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993.  */
d9d99f
+
d9d99f
+#include <grub/types.h>
d9d99f
+#include <grub/misc.h>
d9d99f
+#include <grub/mm.h>
d9d99f
+
d9d99f
+#define CHAR_BIT 8
d9d99f
+
d9d99f
+/* Byte-wise swap two items of size SIZE. */
d9d99f
+#define SWAP(a, b, size)						      \
d9d99f
+  do									      \
d9d99f
+    {									      \
d9d99f
+      grub_size_t __size = (size);						      \
d9d99f
+      char *__a = (a), *__b = (b);					      \
d9d99f
+      do								      \
d9d99f
+	{								      \
d9d99f
+	  char __tmp = *__a;						      \
d9d99f
+	  *__a++ = *__b;						      \
d9d99f
+	  *__b++ = __tmp;						      \
d9d99f
+	} while (--__size > 0);						      \
d9d99f
+    } while (0)
d9d99f
+
d9d99f
+/* Discontinue quicksort algorithm when partition gets below this size.
d9d99f
+   This particular magic number was chosen to work best on a Sun 4/260. */
d9d99f
+#define MAX_THRESH 4
d9d99f
+
d9d99f
+/* Stack node declarations used to store unfulfilled partition obligations. */
d9d99f
+typedef struct
d9d99f
+  {
d9d99f
+    char *lo;
d9d99f
+    char *hi;
d9d99f
+  } stack_node;
d9d99f
+
d9d99f
+/* The next 4 #defines implement a very fast in-line stack abstraction. */
d9d99f
+/* The stack needs log (total_elements) entries (we could even subtract
d9d99f
+   log(MAX_THRESH)).  Since total_elements has type grub_size_t, we get as
d9d99f
+   upper bound for log (total_elements):
d9d99f
+   bits per byte (CHAR_BIT) * sizeof(grub_size_t).  */
d9d99f
+#define STACK_SIZE	(CHAR_BIT * sizeof(grub_size_t))
d9d99f
+#define PUSH(low, high)	((void) ((top->lo = (low)), (top->hi = (high)), ++top))
d9d99f
+#define	POP(low, high)	((void) (--top, (low = top->lo), (high = top->hi)))
d9d99f
+#define	STACK_NOT_EMPTY	(stack < top)
d9d99f
+
d9d99f
+
d9d99f
+/* Order size using quicksort.  This implementation incorporates
d9d99f
+   four optimizations discussed in Sedgewick:
d9d99f
+
d9d99f
+   1. Non-recursive, using an explicit stack of pointer that store the
d9d99f
+      next array partition to sort.  To save time, this maximum amount
d9d99f
+      of space required to store an array of SIZE_MAX is allocated on the
d9d99f
+      stack.  Assuming a 32-bit (64 bit) integer for grub_size_t, this needs
d9d99f
+      only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
d9d99f
+      Pretty cheap, actually.
d9d99f
+
d9d99f
+   2. Chose the pivot element using a median-of-three decision tree.
d9d99f
+      This reduces the probability of selecting a bad pivot value and
d9d99f
+      eliminates certain extraneous comparisons.
d9d99f
+
d9d99f
+   3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
d9d99f
+      insertion sort to order the MAX_THRESH items within each partition.
d9d99f
+      This is a big win, since insertion sort is faster for small, mostly
d9d99f
+      sorted array segments.
d9d99f
+
d9d99f
+   4. The larger of the two sub-partitions is always pushed onto the
d9d99f
+      stack first, with the algorithm then concentrating on the
d9d99f
+      smaller partition.  This *guarantees* no more than log (total_elems)
d9d99f
+      stack size is needed (actually O(1) in this case)!  */
d9d99f
+
d9d99f
+void
d9d99f
+grub_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
d9d99f
+	    grub_compar_d_fn_t cmp, void *arg)
d9d99f
+{
d9d99f
+  char *base_ptr = (char *) pbase;
d9d99f
+
d9d99f
+  const grub_size_t max_thresh = MAX_THRESH * size;
d9d99f
+
d9d99f
+  if (total_elems == 0)
d9d99f
+    /* Avoid lossage with unsigned arithmetic below.  */
d9d99f
+    return;
d9d99f
+
d9d99f
+  if (total_elems > MAX_THRESH)
d9d99f
+    {
d9d99f
+      char *lo = base_ptr;
d9d99f
+      char *hi = &lo[size * (total_elems - 1)];
d9d99f
+      stack_node stack[STACK_SIZE];
d9d99f
+      stack_node *top = stack;
d9d99f
+
d9d99f
+      PUSH (NULL, NULL);
d9d99f
+
d9d99f
+      while (STACK_NOT_EMPTY)
d9d99f
+        {
d9d99f
+          char *left_ptr;
d9d99f
+          char *right_ptr;
d9d99f
+
d9d99f
+	  /* Select median value from among LO, MID, and HI. Rearrange
d9d99f
+	     LO and HI so the three values are sorted. This lowers the
d9d99f
+	     probability of picking a pathological pivot value and
d9d99f
+	     skips a comparison for both the LEFT_PTR and RIGHT_PTR in
d9d99f
+	     the while loops. */
d9d99f
+
d9d99f
+	  char *mid = lo + size * ((hi - lo) / size >> 1);
d9d99f
+
d9d99f
+	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
d9d99f
+	    SWAP (mid, lo, size);
d9d99f
+	  if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
d9d99f
+	    SWAP (mid, hi, size);
d9d99f
+	  else
d9d99f
+	    goto jump_over;
d9d99f
+	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
d9d99f
+	    SWAP (mid, lo, size);
d9d99f
+	jump_over:;
d9d99f
+
d9d99f
+	  left_ptr  = lo + size;
d9d99f
+	  right_ptr = hi - size;
d9d99f
+
d9d99f
+	  /* Here's the famous ``collapse the walls'' section of quicksort.
d9d99f
+	     Gotta like those tight inner loops!  They are the main reason
d9d99f
+	     that this algorithm runs much faster than others. */
d9d99f
+	  do
d9d99f
+	    {
d9d99f
+	      while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
d9d99f
+		left_ptr += size;
d9d99f
+
d9d99f
+	      while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
d9d99f
+		right_ptr -= size;
d9d99f
+
d9d99f
+	      if (left_ptr < right_ptr)
d9d99f
+		{
d9d99f
+		  SWAP (left_ptr, right_ptr, size);
d9d99f
+		  if (mid == left_ptr)
d9d99f
+		    mid = right_ptr;
d9d99f
+		  else if (mid == right_ptr)
d9d99f
+		    mid = left_ptr;
d9d99f
+		  left_ptr += size;
d9d99f
+		  right_ptr -= size;
d9d99f
+		}
d9d99f
+	      else if (left_ptr == right_ptr)
d9d99f
+		{
d9d99f
+		  left_ptr += size;
d9d99f
+		  right_ptr -= size;
d9d99f
+		  break;
d9d99f
+		}
d9d99f
+	    }
d9d99f
+	  while (left_ptr <= right_ptr);
d9d99f
+
d9d99f
+          /* Set up pointers for next iteration.  First determine whether
d9d99f
+             left and right partitions are below the threshold size.  If so,
d9d99f
+             ignore one or both.  Otherwise, push the larger partition's
d9d99f
+             bounds on the stack and continue sorting the smaller one. */
d9d99f
+
d9d99f
+          if ((grub_size_t) (right_ptr - lo) <= max_thresh)
d9d99f
+            {
d9d99f
+              if ((grub_size_t) (hi - left_ptr) <= max_thresh)
d9d99f
+		/* Ignore both small partitions. */
d9d99f
+                POP (lo, hi);
d9d99f
+              else
d9d99f
+		/* Ignore small left partition. */
d9d99f
+                lo = left_ptr;
d9d99f
+            }
d9d99f
+          else if ((grub_size_t) (hi - left_ptr) <= max_thresh)
d9d99f
+	    /* Ignore small right partition. */
d9d99f
+            hi = right_ptr;
d9d99f
+          else if ((right_ptr - lo) > (hi - left_ptr))
d9d99f
+            {
d9d99f
+	      /* Push larger left partition indices. */
d9d99f
+              PUSH (lo, right_ptr);
d9d99f
+              lo = left_ptr;
d9d99f
+            }
d9d99f
+          else
d9d99f
+            {
d9d99f
+	      /* Push larger right partition indices. */
d9d99f
+              PUSH (left_ptr, hi);
d9d99f
+              hi = right_ptr;
d9d99f
+            }
d9d99f
+        }
d9d99f
+    }
d9d99f
+
d9d99f
+  /* Once the BASE_PTR array is partially sorted by quicksort the rest
d9d99f
+     is completely sorted using insertion sort, since this is efficient
d9d99f
+     for partitions below MAX_THRESH size. BASE_PTR points to the beginning
d9d99f
+     of the array to sort, and END_PTR points at the very last element in
d9d99f
+     the array (*not* one beyond it!). */
d9d99f
+
d9d99f
+#define min(x, y) ((x) < (y) ? (x) : (y))
d9d99f
+
d9d99f
+  {
d9d99f
+    char *const end_ptr = &base_ptr[size * (total_elems - 1)];
d9d99f
+    char *tmp_ptr = base_ptr;
d9d99f
+    char *thresh = min(end_ptr, base_ptr + max_thresh);
d9d99f
+    char *run_ptr;
d9d99f
+
d9d99f
+    /* Find smallest element in first threshold and place it at the
d9d99f
+       array's beginning.  This is the smallest array element,
d9d99f
+       and the operation speeds up insertion sort's inner loop. */
d9d99f
+
d9d99f
+    for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
d9d99f
+      if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
d9d99f
+        tmp_ptr = run_ptr;
d9d99f
+
d9d99f
+    if (tmp_ptr != base_ptr)
d9d99f
+      SWAP (tmp_ptr, base_ptr, size);
d9d99f
+
d9d99f
+    /* Insertion sort, running from left-hand-side up to right-hand-side.  */
d9d99f
+
d9d99f
+    run_ptr = base_ptr + size;
d9d99f
+    while ((run_ptr += size) <= end_ptr)
d9d99f
+      {
d9d99f
+	tmp_ptr = run_ptr - size;
d9d99f
+	while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
d9d99f
+	  tmp_ptr -= size;
d9d99f
+
d9d99f
+	tmp_ptr += size;
d9d99f
+        if (tmp_ptr != run_ptr)
d9d99f
+          {
d9d99f
+            char *trav;
d9d99f
+
d9d99f
+	    trav = run_ptr + size;
d9d99f
+	    while (--trav >= run_ptr)
d9d99f
+              {
d9d99f
+                char c = *trav;
d9d99f
+                char *hi, *lo;
d9d99f
+
d9d99f
+                for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
d9d99f
+                  *hi = *lo;
d9d99f
+                *hi = c;
d9d99f
+              }
d9d99f
+          }
d9d99f
+      }
d9d99f
+  }
d9d99f
+}
d9d99f
+
d9d99f
+void *
d9d99f
+grub_bsearch (const void *key, const void *base, grub_size_t nmemb, grub_size_t size,
d9d99f
+	 grub_compar_d_fn_t compar, void *state)
d9d99f
+{
d9d99f
+  grub_size_t l, u, idx;
d9d99f
+  const void *p;
d9d99f
+  int comparison;
d9d99f
+
d9d99f
+  l = 0;
d9d99f
+  u = nmemb;
d9d99f
+  while (l < u)
d9d99f
+    {
d9d99f
+      idx = (l + u) / 2;
d9d99f
+      p = (void *) (((const char *) base) + (idx * size));
d9d99f
+      comparison = (*compar) (key, p, state);
d9d99f
+      if (comparison < 0)
d9d99f
+	u = idx;
d9d99f
+      else if (comparison > 0)
d9d99f
+	l = idx + 1;
d9d99f
+      else
d9d99f
+	return (void *) p;
d9d99f
+    }
d9d99f
+
d9d99f
+  return NULL;
d9d99f
+}
d9d99f
diff --git a/include/grub/misc.h b/include/grub/misc.h
b71686
index fcaf1201e..cbfae75a1 100644
d9d99f
--- a/include/grub/misc.h
d9d99f
+++ b/include/grub/misc.h
d9d99f
@@ -507,4 +507,19 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
d9d99f
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
d9d99f
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
d9d99f
 
d9d99f
+typedef int (*grub_compar_d_fn_t) (const void *p0, const void *p1, void *state);
d9d99f
+
d9d99f
+void *EXPORT_FUNC(grub_bsearch) (const void *key,
d9d99f
+			    const void *base,
d9d99f
+			    grub_size_t nmemb,
d9d99f
+			    grub_size_t size,
d9d99f
+			    grub_compar_d_fn_t compar,
d9d99f
+			    void *state);
d9d99f
+
d9d99f
+void EXPORT_FUNC(grub_qsort) (void *const pbase,
d9d99f
+			 grub_size_t total_elems,
d9d99f
+			 grub_size_t size,
d9d99f
+			 grub_compar_d_fn_t cmp,
d9d99f
+			 void *state);
d9d99f
+
d9d99f
 #endif /* ! GRUB_MISC_HEADER */