1 /*
2 * Copyright © 2020 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef U_QSORT_H
25 #define U_QSORT_H
26
27 #include <stdlib.h>
28
29 #include "detect_os.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 void util_tls_qsort_r(void *base, size_t nmemb, size_t size,
36 int (*compar)(const void *, const void *, void *),
37 void *arg);
38
39
40 struct util_qsort_adapter_data {
41 int (*compar)(const void*, const void*, void*);
42 void *args;
43 };
44
45 /**
46 * Converts comparison function arguments
47 * from [MSVC, BSD, macOS]
48 * (void *ctx, const void *elem1, const void *elem2)
49 * to [GNU, C11]
50 * (const void *elem1, const void *elem2, void *ctx);
51 */
52 int util_qsort_adapter(void *ctx, const void *elem1, const void *elem2);
53
54 static inline void
util_qsort_r(void * base,size_t nmemb,size_t size,int (* compar)(const void *,const void *,void *),void * arg)55 util_qsort_r(void *base, size_t nmemb, size_t size,
56 int (*compar)(const void *, const void *, void *),
57 void *arg)
58 {
59 #if defined(HAVE_GNU_QSORT_R)
60 /* GNU extension added in glibc 2.8 */
61 qsort_r(base, nmemb, size, compar, arg);
62 #elif defined(HAVE_BSD_QSORT_R)
63 /* BSD/macOS qsort_r takes "arg" before the comparison function and it
64 * pass the "arg" before the elements.
65 */
66 struct util_qsort_adapter_data data = {
67 compar,
68 arg
69 };
70 qsort_r(base, nmemb, size, &data, util_qsort_adapter);
71 #elif HAVE_QSORT_S
72 # ifdef _WIN32
73 /* MSVC/MinGW qsort_s takes "arg" after the comparison function and it
74 * pass the "arg" before the elements.
75 */
76 struct util_qsort_adapter_data data = {
77 compar,
78 arg
79 };
80 qsort_s(base, nmemb, size, util_qsort_adapter, &data);
81 # else
82 /* C11 added qsort_s */
83 qsort_s(base, nmemb, size, compar, arg);
84 # endif
85 #else
86 /* Fall-back to using thread local storage */
87 util_tls_qsort_r(base, nmemb, size, compar, arg);
88 #endif
89 }
90
91 #ifdef __cplusplus
92 } /* extern "C" */
93 #endif
94
95 #endif /* U_QSORT_H */
96