Implement the same PLT reduction technique used in GTK+:
[platform/upstream/glib.git] / glib / gqsort.c
index c75cca3..ac45e8b 100644 (file)
  *
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
- * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/.  */
+ * files for a list of changes.  These files are distributed with GLib
+ * at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#include "config.h"
 
 #include <string.h>
 
+#include "galias.h"
 #include "glib.h"
 
+
 /* Byte-wise swap two items of size SIZE. */
 #define SWAP(a, b, size)                                                     \
   do                                                                         \
@@ -92,10 +97,22 @@ stack_node;
  *    in this case)!
  */
 
+/**
+ * g_qsort_with_data:
+ * @pbase: start of array to sort
+ * @total_elems: elements in the array
+ * @size: size of each element
+ * @compare_func: function to compare elements
+ * @user_data: data to pass to @compare_func
+ *
+ * This is just like the standard C qsort() function, but
+ * the comparison routine accepts a user data argument.
+ * 
+ **/
 void
 g_qsort_with_data (gconstpointer    pbase,
                   gint             total_elems,
-                  size_t           size,
+                  gsize            size,
                   GCompareDataFunc compare_func,
                   gpointer         user_data)
 {
@@ -104,13 +121,16 @@ g_qsort_with_data (gconstpointer    pbase,
   /* Allocating SIZE bytes for a pivot buffer facilitates a better
    * algorithm below since we can do comparisons directly on the pivot.
    */
-  char *pivot_buffer = (char *) alloca (size);
+  char *pivot_buffer = (char *) g_alloca (size);
   const size_t max_thresh = MAX_THRESH * size;
 
-  g_return_if_fail (total_elems > 0);
-  g_return_if_fail (pbase != NULL);
+  g_return_if_fail (total_elems >= 0);
+  g_return_if_fail (pbase != NULL || total_elems == 0);
   g_return_if_fail (compare_func != NULL);
 
+  if (total_elems == 0)
+    return;
+
   if (total_elems > MAX_THRESH)
     {
       char *lo = base_ptr;