diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-09-18 08:58:21 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-09-19 08:04:40 +0900 |
commit | 5532395be7aecae768afd96d37a30834167711a0 (patch) | |
tree | ac8c7cfe41ec68a05c91dabf7d478eaa25844ed2 | |
parent | 93bab288956f43c70f2b28a88efdc9effd951bb5 (diff) | |
download | systemd-5532395be7aecae768afd96d37a30834167711a0.tar.gz systemd-5532395be7aecae768afd96d37a30834167711a0.tar.bz2 systemd-5532395be7aecae768afd96d37a30834167711a0.zip |
util: introduce typesafe_qsort_r()
-rw-r--r-- | src/basic/util.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/basic/util.h b/src/basic/util.h index 308933ac90..a6e77980a1 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -88,7 +88,7 @@ static inline void* bsearch_safe(const void *key, const void *base, * Normal qsort requires base to be nonnull. Here were require * that only if nmemb > 0. */ -static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) { +static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) { if (nmemb <= 1) return; @@ -104,7 +104,7 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_ qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \ }) -static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void *userdata) { +static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) { if (nmemb <= 1) return; @@ -112,6 +112,12 @@ static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*com qsort_r(base, nmemb, size, compar, userdata); } +#define typesafe_qsort_r(p, n, func, userdata) \ + ({ \ + int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \ + qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \ + }) + /* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */ static inline void memcpy_safe(void *dst, const void *src, size_t n) { if (n == 0) |