1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdlib.h>
5 
6 #include "macro.h"
7 
8 /* This is the same as glibc's internal __compar_d_fn_t type. glibc exports a public comparison_fn_t, for the
9  * external type __compar_fn_t, but doesn't do anything similar for __compar_d_fn_t. Let's hence do that
10  * ourselves, picking a name that is obvious, but likely enough to not clash with glibc's choice of naming if
11  * they should ever add one. */
12 typedef int (*comparison_userdata_fn_t)(const void *, const void *, void *);
13 
14 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
15                  comparison_userdata_fn_t compar, void *arg);
16 
17 #define typesafe_bsearch_r(k, b, n, func, userdata)                     \
18         ({                                                              \
19                 const typeof((b)[0]) *_k = k;                           \
20                 int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*, typeof(userdata)) = func; \
21                 xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_userdata_fn_t) _func_, userdata); \
22         })
23 
24 /**
25  * Normal bsearch requires base to be nonnull. Here were require
26  * that only if nmemb > 0.
27  */
bsearch_safe(const void * key,const void * base,size_t nmemb,size_t size,comparison_fn_t compar)28 static inline void* bsearch_safe(const void *key, const void *base,
29                                  size_t nmemb, size_t size, comparison_fn_t compar) {
30         if (nmemb <= 0)
31                 return NULL;
32 
33         assert(base);
34         return bsearch(key, base, nmemb, size, compar);
35 }
36 
37 #define typesafe_bsearch(k, b, n, func)                                 \
38         ({                                                              \
39                 const typeof((b)[0]) *_k = k;                           \
40                 int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*) = func; \
41                 bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_fn_t) _func_); \
42         })
43 
44 /**
45  * Normal qsort requires base to be nonnull. Here were require
46  * that only if nmemb > 0.
47  */
_qsort_safe(void * base,size_t nmemb,size_t size,comparison_fn_t compar)48 static inline void _qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
49         if (nmemb <= 1)
50                 return;
51 
52         assert(base);
53         qsort(base, nmemb, size, compar);
54 }
55 
56 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
57  * is the prototype for the comparison function */
58 #define typesafe_qsort(p, n, func)                                      \
59         ({                                                              \
60                 int (*_func_)(const typeof((p)[0])*, const typeof((p)[0])*) = func; \
61                 _qsort_safe((p), (n), sizeof((p)[0]), (comparison_fn_t) _func_); \
62         })
63 
qsort_r_safe(void * base,size_t nmemb,size_t size,comparison_userdata_fn_t compar,void * userdata)64 static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, comparison_userdata_fn_t compar, void *userdata) {
65         if (nmemb <= 1)
66                 return;
67 
68         assert(base);
69         qsort_r(base, nmemb, size, compar, userdata);
70 }
71 
72 #define typesafe_qsort_r(p, n, func, userdata)                          \
73         ({                                                              \
74                 int (*_func_)(const typeof((p)[0])*, const typeof((p)[0])*, typeof(userdata)) = func; \
75                 qsort_r_safe((p), (n), sizeof((p)[0]), (comparison_userdata_fn_t) _func_, userdata); \
76         })
77 
78 int cmp_int(const int *a, const int *b);
79