lib: move sort code
authorJan Beulich <jbeulich@suse.com>
Fri, 18 Dec 2020 12:25:40 +0000 (13:25 +0100)
committerJan Beulich <jbeulich@suse.com>
Fri, 18 Dec 2020 12:25:40 +0000 (13:25 +0100)
Build this code into an archive, partly paralleling bsearch().

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Julien Grall <jgrall@amazon.com>
Acked-by: Wei Liu <wl@xen.org>
Reviewed-by: Bertrand Marquis <bertrand.marquis@arm.com>
xen/common/Makefile
xen/common/sort.c [deleted file]
xen/lib/Makefile
xen/lib/sort.c [new file with mode: 0644]

index e8ce23acea6785a35690f6e9002c09323491b053..7a4e652b575e34af9ec6013a0c39de8e7348f673 100644 (file)
@@ -36,7 +36,6 @@ obj-y += rcupdate.o
 obj-y += rwlock.o
 obj-y += shutdown.o
 obj-y += softirq.o
-obj-y += sort.o
 obj-y += smp.o
 obj-y += spinlock.o
 obj-y += stop_machine.o
diff --git a/xen/common/sort.c b/xen/common/sort.c
deleted file mode 100644 (file)
index 7b7544b..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
- *
- * Jan 23 2005  Matt Mackall <mpm@selenic.com>
- */
-
-#include <xen/types.h>
-
-static void u32_swap(void *a, void *b, int size)
-{
-    u32 t = *(u32 *)a;
-    *(u32 *)a = *(u32 *)b;
-    *(u32 *)b = t;
-}
-
-static void generic_swap(void *a, void *b, int size)
-{
-    char t;
-
-    do {
-        t = *(char *)a;
-        *(char *)a++ = *(char *)b;
-        *(char *)b++ = t;
-    } while ( --size > 0 );
-}
-
-/*
- * sort - sort an array of elements
- * @base: pointer to data to sort
- * @num: number of elements
- * @size: size of each element
- * @cmp: pointer to comparison function
- * @swap: pointer to swap function or NULL
- *
- * This function does a heapsort on the given array. You may provide a
- * swap function optimized to your element type.
- *
- * Sorting time is O(n log n) both on average and worst-case. While
- * qsort is about 20% faster on average, it suffers from exploitable
- * O(n*n) worst-case behavior and extra memory requirements that make
- * it less suitable for kernel use.
- */
-
-void sort(void *base, size_t num, size_t size,
-          int (*cmp)(const void *, const void *),
-          void (*swap)(void *, void *, int size))
-{
-    /* pre-scale counters for performance */
-    int i = (num/2 - 1) * size, n = num * size, c, r;
-
-    if (!swap)
-        swap = (size == 4 ? u32_swap : generic_swap);
-
-    /* heapify */
-    for ( ; i >= 0; i -= size )
-    {
-        for ( r = i; r * 2 + size < n; r  = c )
-        {
-            c = r * 2 + size;
-            if ( (c < n - size) && (cmp(base + c, base + c + size) < 0) )
-                c += size;
-            if ( cmp(base + r, base + c) >= 0 )
-                break;
-            swap(base + r, base + c, size);
-        }
-    }
-
-    /* sort */
-    for ( i = n - size; i >= 0; i -= size )
-    {
-        swap(base, base + i, size);
-        for ( r = 0; r * 2 + size < i; r = c )
-        {
-            c = r * 2 + size;
-            if ( (c < i - size) && (cmp(base + c, base + c + size) < 0) )
-                c += size;
-            if ( cmp(base + r, base + c) >= 0 )
-                break;
-            swap(base + r, base + c, size);
-        }
-    }
-}
index f12dab7a737af3d2e76b7ee8c70de7f1eeede36c..42cf7a1164efaeacd939abd4b20849a3e91b8c08 100644 (file)
@@ -6,3 +6,4 @@ lib-y += ctype.o
 lib-y += list-sort.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-y += sort.o
diff --git a/xen/lib/sort.c b/xen/lib/sort.c
new file mode 100644 (file)
index 0000000..ee983d0
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
+ *
+ * Jan 23 2005  Matt Mackall <mpm@selenic.com>
+ */
+
+#include <xen/types.h>
+
+static void u32_swap(void *a, void *b, int size)
+{
+    u32 t = *(u32 *)a;
+    *(u32 *)a = *(u32 *)b;
+    *(u32 *)b = t;
+}
+
+static void generic_swap(void *a, void *b, int size)
+{
+    char t;
+
+    do {
+        t = *(char *)a;
+        *(char *)a++ = *(char *)b;
+        *(char *)b++ = t;
+    } while ( --size > 0 );
+}
+
+/*
+ * sort - sort an array of elements
+ * @base: pointer to data to sort
+ * @num: number of elements
+ * @size: size of each element
+ * @cmp: pointer to comparison function
+ * @swap: pointer to swap function or NULL
+ *
+ * This function does a heapsort on the given array. You may provide a
+ * swap function optimized to your element type.
+ *
+ * Sorting time is O(n log n) both on average and worst-case. While
+ * qsort is about 20% faster on average, it suffers from exploitable
+ * O(n*n) worst-case behavior and extra memory requirements that make
+ * it less suitable for kernel use.
+ */
+
+void sort(void *base, size_t num, size_t size,
+          int (*cmp)(const void *, const void *),
+          void (*swap)(void *, void *, int size))
+{
+    /* pre-scale counters for performance */
+    int i = (num / 2 - 1) * size, n = num * size, c, r;
+
+    if ( !swap )
+        swap = (size == 4 ? u32_swap : generic_swap);
+
+    /* heapify */
+    for ( ; i >= 0; i -= size )
+    {
+        for ( r = i; r * 2 + size < n; r  = c )
+        {
+            c = r * 2 + size;
+            if ( (c < n - size) && (cmp(base + c, base + c + size) < 0) )
+                c += size;
+            if ( cmp(base + r, base + c) >= 0 )
+                break;
+            swap(base + r, base + c, size);
+        }
+    }
+
+    /* sort */
+    for ( i = n - size; i >= 0; i -= size )
+    {
+        swap(base, base + i, size);
+        for ( r = 0; r * 2 + size < i; r = c )
+        {
+            c = r * 2 + size;
+            if ( (c < i - size) && (cmp(base + c, base + c + size) < 0) )
+                c += size;
+            if ( cmp(base + r, base + c) >= 0 )
+                break;
+            swap(base + r, base + c, size);
+        }
+    }
+}