]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sort-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 void *xbsearch_r(const void *key
, const void *base
, size_t nmemb
, size_t size
,
9 __compar_d_fn_t compar
, void *arg
);
11 #define typesafe_bsearch_r(k, b, n, func, userdata) \
13 const typeof(b[0]) *_k = k; \
14 int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
15 xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
19 * Normal bsearch requires base to be nonnull. Here were require
20 * that only if nmemb > 0.
22 static inline void* bsearch_safe(const void *key
, const void *base
,
23 size_t nmemb
, size_t size
, __compar_fn_t compar
) {
28 return bsearch(key
, base
, nmemb
, size
, compar
);
31 #define typesafe_bsearch(k, b, n, func) \
33 const typeof(b[0]) *_k = k; \
34 int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
35 bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
39 * Normal qsort requires base to be nonnull. Here were require
40 * that only if nmemb > 0.
42 static inline void qsort_safe(void *base
, size_t nmemb
, size_t size
, __compar_fn_t compar
) {
47 qsort(base
, nmemb
, size
, compar
);
50 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
51 * is the prototype for the comparison function */
52 #define typesafe_qsort(p, n, func) \
54 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
55 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
58 static inline void qsort_r_safe(void *base
, size_t nmemb
, size_t size
, __compar_d_fn_t compar
, void *userdata
) {
63 qsort_r(base
, nmemb
, size
, compar
, userdata
);
66 #define typesafe_qsort_r(p, n, func, userdata) \
68 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
69 qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \