diff options
author | Lennart Poettering <lennart@poettering.net> | 2019-03-13 12:14:47 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2019-03-13 12:16:43 +0100 |
commit | 760877e90cfdf1efd12e12ad29ce47d0ed1503dc (patch) | |
tree | 1cfb3ff933c36333079168b4aeb3275416ee1e3d /src/basic | |
parent | 0a9707187b7d7b02751b068336fa27757797d44c (diff) | |
download | systemd-760877e90cfdf1efd12e12ad29ce47d0ed1503dc.tar.gz |
util: split out sorting related calls to new sort-util.[ch]
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/conf-files.c | 2 | ||||
-rw-r--r-- | src/basic/meson.build | 2 | ||||
-rw-r--r-- | src/basic/sort-util.c | 27 | ||||
-rw-r--r-- | src/basic/sort-util.h | 70 | ||||
-rw-r--r-- | src/basic/strbuf.c | 2 | ||||
-rw-r--r-- | src/basic/strv.c | 2 | ||||
-rw-r--r-- | src/basic/util.c | 25 | ||||
-rw-r--r-- | src/basic/util.h | 64 |
8 files changed, 102 insertions, 92 deletions
diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c index b70c6e50a8..d010fbb266 100644 --- a/src/basic/conf-files.c +++ b/src/basic/conf-files.c @@ -17,11 +17,11 @@ #include "missing.h" #include "path-util.h" #include "set.h" +#include "sort-util.h" #include "stat-util.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" -#include "util.h" static int files_add( Hashmap *h, diff --git a/src/basic/meson.build b/src/basic/meson.build index 3d382b3940..30466ce946 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -174,6 +174,8 @@ basic_sources = files(''' socket-label.c socket-util.c socket-util.h + sort-util.c + sort-util.h sparse-endian.h special.h stat-util.c diff --git a/src/basic/sort-util.c b/src/basic/sort-util.c new file mode 100644 index 0000000000..5cf0d1d49b --- /dev/null +++ b/src/basic/sort-util.c @@ -0,0 +1,27 @@ +#include "sort-util.h" +#include "alloc-util.h" + +/* hey glibc, APIs with callbacks without a user pointer are so useless */ +void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, + __compar_d_fn_t compar, void *arg) { + size_t l, u, idx; + const void *p; + int comparison; + + assert(!size_multiply_overflow(nmemb, size)); + + l = 0; + u = nmemb; + while (l < u) { + idx = (l + u) / 2; + p = (const uint8_t*) base + idx * size; + comparison = compar(key, p, arg); + if (comparison < 0) + u = idx; + else if (comparison > 0) + l = idx + 1; + else + return (void *)p; + } + return NULL; +} diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h new file mode 100644 index 0000000000..e029f8646e --- /dev/null +++ b/src/basic/sort-util.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include <stdlib.h> + +#include "macro.h" + +void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, + __compar_d_fn_t compar, void *arg); + +#define typesafe_bsearch_r(k, b, n, func, userdata) \ + ({ \ + const typeof(b[0]) *_k = k; \ + int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \ + xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \ + }) + +/** + * Normal bsearch requires base to be nonnull. Here were require + * that only if nmemb > 0. + */ +static inline void* bsearch_safe(const void *key, const void *base, + size_t nmemb, size_t size, __compar_fn_t compar) { + if (nmemb <= 0) + return NULL; + + assert(base); + return bsearch(key, base, nmemb, size, compar); +} + +#define typesafe_bsearch(k, b, n, func) \ + ({ \ + const typeof(b[0]) *_k = k; \ + int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \ + bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \ + }) + +/** + * 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, __compar_fn_t compar) { + if (nmemb <= 1) + return; + + assert(base); + 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_); \ + }) + +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; + + assert(base); + 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); \ + }) diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c index 81f4f21ade..769b22aba0 100644 --- a/src/basic/strbuf.c +++ b/src/basic/strbuf.c @@ -5,8 +5,8 @@ #include <string.h> #include "alloc-util.h" +#include "sort-util.h" #include "strbuf.h" -#include "util.h" /* * Strbuf stores given strings in a single continuous allocated memory diff --git a/src/basic/strv.c b/src/basic/strv.c index 3a62f25ded..3700ce5b30 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -11,9 +11,9 @@ #include "escape.h" #include "extract-word.h" #include "fileio.h" +#include "sort-util.h" #include "string-util.h" #include "strv.h" -#include "util.h" char *strv_find(char **l, const char *name) { char **i; diff --git a/src/basic/util.c b/src/basic/util.c index fd0277b5df..ce3e321925 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -126,31 +126,6 @@ void in_initrd_force(bool value) { saved_in_initrd = value; } -/* hey glibc, APIs with callbacks without a user pointer are so useless */ -void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, - __compar_d_fn_t compar, void *arg) { - size_t l, u, idx; - const void *p; - int comparison; - - assert(!size_multiply_overflow(nmemb, size)); - - l = 0; - u = nmemb; - while (l < u) { - idx = (l + u) / 2; - p = (const uint8_t*) base + idx * size; - comparison = compar(key, p, arg); - if (comparison < 0) - u = idx; - else if (comparison > 0) - l = idx + 1; - else - return (void *)p; - } - return NULL; -} - int on_ac_power(void) { bool found_offline = false, found_online = false; _cleanup_closedir_ DIR *d = NULL; diff --git a/src/basic/util.h b/src/basic/util.h index 3c29586749..02fc31e69e 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -63,70 +63,6 @@ int prot_from_flags(int flags) _const_; bool in_initrd(void); void in_initrd_force(bool value); -void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, - __compar_d_fn_t compar, void *arg); - -#define typesafe_bsearch_r(k, b, n, func, userdata) \ - ({ \ - const typeof(b[0]) *_k = k; \ - int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \ - xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \ - }) - -/** - * Normal bsearch requires base to be nonnull. Here were require - * that only if nmemb > 0. - */ -static inline void* bsearch_safe(const void *key, const void *base, - size_t nmemb, size_t size, __compar_fn_t compar) { - if (nmemb <= 0) - return NULL; - - assert(base); - return bsearch(key, base, nmemb, size, compar); -} - -#define typesafe_bsearch(k, b, n, func) \ - ({ \ - const typeof(b[0]) *_k = k; \ - int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \ - bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \ - }) - -/** - * 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, __compar_fn_t compar) { - if (nmemb <= 1) - return; - - assert(base); - 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_); \ - }) - -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; - - assert(base); - 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); \ - }) - int on_ac_power(void); static inline void _reset_errno_(int *saved_errno) { |