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 HAVE_QSORT_R
60 # if DETECT_OS_APPLE || DETECT_OS_BSD
61 /* BSD/macOS qsort_r takes "arg" before the comparison function and it
62 * pass the "arg" before the elements.
63 */
64 struct util_qsort_adapter_data data = {
65 compar,
66 arg
67 };
68 qsort_r(base, nmemb, size, &data, util_qsort_adapter);
69 # else
70 /* GNU extension added in glibc 2.8 */
71 qsort_r(base, nmemb, size, compar, arg);
72 # endif
73 #elif HAVE_QSORT_S
74 # ifdef _WIN32
75 /* MSVC/MinGW qsort_s takes "arg" after the comparison function and it
76 * pass the "arg" before the elements.
77 */
78 struct util_qsort_adapter_data data = {
79 compar,
80 arg
81 };
82 qsort_s(base, nmemb, size, util_qsort_adapter, &data);
83 # else
84 /* C11 added qsort_s */
85 qsort_s(base, nmemb, size, compar, arg);
86 # endif
87 #else
88 /* Fall-back to using thread local storage */
89 util_tls_qsort_r(base, nmemb, size, compar, arg);
90 #endif
91 }
92
93 #ifdef __cplusplus
94 } /* extern "C" */
95 #endif
96
97 #endif /* U_QSORT_H */
98