tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: qsort_r

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 heapsort_r().

Apparently FreeBSD [1] and GNU [2] have incompatible versions of qsort_r, passing the extra 'thunk' or 'data' argument in a different position.

[1]: FreeBSD qsort_r <>
[2]: Linux qsort_r  <>

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)

Home | Main Index | Thread Index | Old Index