[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
On Sun, 08 Dec 2013, David Holland wrote:
My irritation with not being able to pass a data pointer through
qsort() boiled over just now. Apparently Linux and/or GNU
has a qsort_r() that supports this; so, following is a patch
that gives us a compatible qsort_r() plus mergesort_r(), and
Apparently FreeBSD  and GNU  have incompatible versions
of qsort_r, passing the extra 'thunk' or 'data' argument in a
: FreeBSD qsort_r <http://www.manpagez.com/man/3/qsort_r/>
: Linux qsort_r <http://man7.org/linux/man-pages/man3/qsort.3.html>
If we have to pick one, let's pick the FreeBSD version.
I have done it by having the original, non-_r functions
provide a thunk for the comparison function, as this is least
invasive. If we think this is too expensive, an alternative is
generating a union of function pointers and making tests at the
call sites; another option is to duplicate the code (hopefully
with cpp rather than C&P) but that seems like a bad plan.
I'd probably duplicate the code via CPP, to trade time for space,
but your way is fine.
Note that the thunks use an extra struct to hold the function
pointer; this is to satisfy C standards pedantry about void
pointers vs. function pointers, and if we decide not to care it
could be simplified.
That adds more run-time overhead. Could you make it conditional
on whether it's really necessary? All existing NetBSD platforms
can convert back and forth between void * and function pointers
without any trouble.
--apb (Alan Barrett)
Main Index |
Thread Index |