1 /******************************************************************************
2 * sort.c: sort back-end
3 ******************************************************************************
4 * Copyright © 2019 VLC authors and VideoLAN
5 * Copyright © 2018 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
26 #include <vlc_common.h>
29 static thread_local
struct
31 int (*compar
)(const void *, const void *, void *);
35 static int compar_wrapper(const void *a
, const void *b
)
37 return state
.compar(a
, b
, state
.arg
);
40 /* Follow the upcoming POSIX prototype, coming from GNU/libc.
41 * Note that this differs from the BSD prototype. */
43 VLC_WEAK
void vlc_qsort(void *base
, size_t nmemb
, size_t size
,
44 int (*compar
)(const void *, const void *, void *),
47 int (*saved_compar
)(const void *, const void *, void *) = state
.compar
;
48 void *saved_arg
= state
.arg
;
50 state
.compar
= compar
;
53 qsort(base
, nmemb
, size
, compar_wrapper
);
55 /* Restore state for nested reentrant calls */
56 state
.compar
= saved_compar
;
57 state
.arg
= saved_arg
;