- rename to USE_OWN_QSORT, add bsd qsort_r helper
authorMichael Schroeder <mls@suse.de>
Tue, 15 Feb 2011 11:20:34 +0000 (12:20 +0100)
committerMichael Schroeder <mls@suse.de>
Tue, 15 Feb 2011 11:20:34 +0000 (12:20 +0100)
package/libsatsolver.spec.in
src/qsort_r.c
src/util.c

index 7b81049..627943b 100644 (file)
@@ -111,7 +111,7 @@ CMAKE_FLAGS="-DFEDORA=1"
 %endif
 # Where does RHEL provide qsort_r ?
 %if 0%{?rhel_version} > 0 || 0%{?centos_version} > 0
-export CFLAGS="$RPM_OPT_FLAGS -DHAVE_OWN_QSORT=1"
+export CFLAGS="$RPM_OPT_FLAGS -DUSE_OWN_QSORT=1"
 %endif
 
 cmake   $CMAKE_FLAGS \
index ef46d7f..7911ec5 100644 (file)
@@ -95,8 +95,8 @@ med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
               :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
 }
 
-static void
-qsort_r(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk)
+void
+sat_sort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk)
 {
        char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
        size_t d, r;
@@ -168,7 +168,7 @@ loop:       SWAPINIT(a, es);
        r = min(pd - pc, pn - pd - es);
        vecswap(pb, pn - r, r);
        if ((r = pb - pa) > es)
-               qsort_r(a, r / es, es, cmp, thunk);
+               sat_sort(a, r / es, es, cmp, thunk);
        if ((r = pd - pc) > es) {
                /* Iterate rather than recurse to save stack space */
                a = pn - r;
index bfa18e3..f728e92 100644 (file)
@@ -98,30 +98,55 @@ sat_timems(unsigned int subtract)
   return r - subtract;
 }
 
-#ifdef HAVE_OWN_QSORT
+#ifdef USE_OWN_QSORT
 #include "qsort_r.c"
-#endif
+#else
 
 /* bsd's qsort_r has different arguments, so we define our
    own version in case we need to do some clever mapping
  
    see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
  */
+#if defined(__GLIBC__)
+
 void
 sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
 {
-#if defined(__GLIBC__)
-# if __GLIBC_PREREQ(2, 8) || defined(HAVE_OWN_QSORT)
+# if __GLIBC_PREREQ(2, 8)
   qsort_r(base, nmemb, size, compar, compard);
 # else
   /* backported for SLE10-SP2 */
   __qsort_r(base, nmemb, size, compar, compard);
 # endif
+}
+
 #else
-# error please add correct qsort_r call here, note different ordering on BSD
-#endif
+
+struct sat_sort_data {
+  int (*compar)(const void *, const void *, void *);
+  void *compard;
+};
+
+static int
+sat_sort_helper(void *compard, const void *a, const void *b)
+{
+  struct sat_sort_data *d = compard;
+  return (*d->compar)(a, b, d->compard);
 }
 
+void
+sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
+{
+  struct sat_sort_data d;
+  d.compar = compar;
+  d.compard = compard;
+  qsort_r(base, nmemb, size, &d, sat_sort_helper);
+}
+
+#endif
+
+#endif /* USE_OWN_QSORT */
+
 char *
 sat_dupjoin(const char *str1, const char *str2, const char *str3)
 {