diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-03-27 14:56:29 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-04-11 22:30:04 +0200 |
commit | 1ce360813decc3ddc6dfff8c1f55927e83021404 (patch) | |
tree | a5f57876ab2ab53c8975592fda45e712bca85c31 /src/basic/util.h | |
parent | Merge pull request #8700 from keszybz/hibernation (diff) | |
download | systemd-1ce360813decc3ddc6dfff8c1f55927e83021404.tar.xz systemd-1ce360813decc3ddc6dfff8c1f55927e83021404.zip |
util: introduce typesafe_qsort(), a typesafe version of qsort()/qsort_safe()
It does two things:
1. It derives the element size from the array argument type
2. It derives the right type for the function from the array argument
type
Using this macro call should make the invocations of qsort() quite a bit
safer.
Diffstat (limited to '')
-rw-r--r-- | src/basic/util.h | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/basic/util.h b/src/basic/util.h index 7a98890836..0db3627e24 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -103,6 +103,14 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_ qsort(base, nmemb, size, compar); } +/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so + * is the prototype for the comparison function */ +#define typesafe_qsort(p, n, func) \ + ({ \ + int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \ + qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \ + }) + /** * Normal memcpy requires src to be nonnull. We do nothing if n is 0. */ |