Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 08 Aug 2021 17:35:51 -0300
From: Érico Nogueira <ericonr@...root.org>
To: <musl@...ts.openwall.com>
Cc: "Olivier Galibert" <galibert@...ox.com>
Subject: Re: [PATCH] stdlib: implement qsort_r

On Sun Aug 8, 2021 at 9:42 AM -03, Jon Chesterfield wrote:
> On Sun, Aug 8, 2021 at 12:29 PM Olivier Galibert <galibert@...ox.com>
> wrote:
>
> > The extension qsort_r allows calling qsort on a list of indices
> > without having a global variable to hold the data to sort.
> >
>
> qsort_r is a strong improvement on qsort. I think it's available outside
> of
> glibc.
>
> I remember doing something similar locally. Just looked it up and I
> renamed & mutated qsort to pass the context along. Therefore typed into
> email, I think something like this would provide an implementation of
> qsort in terms of qsort_r.
>
> // declare qsort_r
> typedef int (*cmp_r_t)(const void *, const void *, void * context);
>
>
> void qsort_r(void *base, size_t nel, size_t width, cmp_r_t cmp, void*
> context);
>
>
> // pass it a function that extracts the comparator for qsort from the
> // context
> typedef int (*cmp_t)(const void *, const void *);
>
>
> static int compare_adapter(const void *l, const void *r, void * context)
> {
> cmpt_t c = (cmpt_t) context;
>
>
> return c(l,r);
>
>
> }
>
> // tail call
> void qsort(void *base, size_t nel, size_t width, cmp_t c)
> {
> return qsort_r(base, nel, width, compare_adapter, (cmp_t_t)c);
>
>
> }
>
> Given optimism about inlining or an always inline annotation
> it should turn into the same machine code as the macro
> instantiation approach. Alternatively it's a tail call into qsort_r, so
> a couple of indirections in exchange for minimal code size growth.
>
> I haven't compiled or tested that (or looked up the coding conventions
> for musl) so this is a drive by suggestion, written in pseudocode
> instead of prose for clarity.

This is the favored approach, from what I understood of the discussions
surrounding it. It's implemented with musl's namespacing rules and such
in [1].

It is badly optimized for some archs, unfortunately, as explained in the
thread from [2]. I believe that's what's holding it up.

[1] https://inbox.vuxu.org/musl/20210309210213.29539-1-ericonr@disroot.org/
[2] https://inbox.vuxu.org/musl/20210309150320.GU32655@brightrain.aerifal.cx/

>
> Thanks all!
>
> Jon

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.