util: split out sorting related calls to new sort-util.[ch]

This commit is contained in:
Lennart Poettering 2019-03-13 12:14:47 +01:00
parent 0a9707187b
commit 760877e90c
39 changed files with 136 additions and 108 deletions

View File

@ -36,11 +36,12 @@
#if HAVE_SECCOMP
# include "seccomp-util.h"
#endif
#include "sort-util.h"
#include "special.h"
#include "strv.h"
#include "strxcpyx.h"
#include "time-util.h"
#include "terminal-util.h"
#include "time-util.h"
#include "unit-name.h"
#include "util.h"
#include "verbs.h"

View File

@ -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,

View File

@ -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

27
src/basic/sort-util.c Normal file
View File

@ -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;
}

70
src/basic/sort-util.h Normal file
View File

@ -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); \
})

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -25,10 +25,10 @@
#include "path-util.h"
#include "pretty-print.h"
#include "set.h"
#include "sort-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
#include "util.h"
#include "verbs.h"
static enum {

View File

@ -25,11 +25,11 @@
#include "pretty-print.h"
#include "process-util.h"
#include "procfs-util.h"
#include "sort-util.h"
#include "stdio-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "util.h"
#include "virt.h"
typedef struct Group {

View File

@ -17,6 +17,7 @@
#include "parse-util.h"
#include "serialize.h"
#include "set.h"
#include "sort-util.h"
#include "special.h"
#include "stdio-util.h"
#include "string-table.h"

View File

@ -26,13 +26,13 @@
#include "path-util.h"
#include "selinux-util.h"
#include "socket-util.h"
#include "sort-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "umask-util.h"
#include "user-util.h"
#include "util.h"
#define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC)

View File

@ -21,6 +21,7 @@
#include "mkdir.h"
#include "path-util.h"
#include "siphash24.h"
#include "sort-util.h"
#include "sparse-endian.h"
#include "strbuf.h"
#include "string-util.h"

View File

@ -27,6 +27,7 @@
#include "path-util.h"
#include "random-util.h"
#include "set.h"
#include "sort-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"

View File

@ -14,9 +14,9 @@
#include "journal-file.h"
#include "journal-vacuum.h"
#include "parse-util.h"
#include "sort-util.h"
#include "string-util.h"
#include "time-util.h"
#include "util.h"
#include "xattr-util.h"
struct vacuum_info {

View File

@ -15,6 +15,7 @@
#include "lldp-network.h"
#include "memory-util.h"
#include "socket-util.h"
#include "sort-util.h"
#include "string-table.h"
#define LLDP_DEFAULT_NEIGHBORS_MAX 128U

View File

@ -10,6 +10,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "hexdecoct.h"
#include "sort-util.h"
#include "string-util.h"
#include "strv.h"

View File

@ -8,9 +8,9 @@
#include "dirent-util.h"
#include "fd-util.h"
#include "set.h"
#include "sort-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
#define DEVICE_ENUMERATE_MAX_DEPTH 256

View File

@ -13,6 +13,7 @@
#include "label.h"
#include "mkdir.h"
#include "path-util.h"
#include "sort-util.h"
#include "strbuf.h"
#include "string-util.h"
#include "strv.h"

View File

@ -6,6 +6,7 @@
#include "local-addresses.h"
#include "macro.h"
#include "netlink-util.h"
#include "sort-util.h"
static int address_compare(const struct local_address *a, const struct local_address *b) {
int r;

View File

@ -44,13 +44,13 @@
#include "rlimit-util.h"
#include "sigbus.h"
#include "signal-util.h"
#include "sort-util.h"
#include "spawn-polkit-agent.h"
#include "stdio-util.h"
#include "string-table.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "util.h"
#include "verbs.h"
#include "web-util.h"

View File

@ -22,13 +22,14 @@
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "sort-util.h"
#include "spawn-polkit-agent.h"
#include "stat-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-def.h"
#include "unit-name.h"
#include "user-util.h"
#include "terminal-util.h"
enum {
ACTION_DEFAULT,

View File

@ -26,6 +26,7 @@
#include "parse-util.h"
#include "pretty-print.h"
#include "socket-util.h"
#include "sort-util.h"
#include "sparse-endian.h"
#include "stdio-util.h"
#include "string-table.h"
@ -33,7 +34,6 @@
#include "strv.h"
#include "strxcpyx.h"
#include "terminal-util.h"
#include "util.h"
#include "verbs.h"
static PagerFlags arg_pager_flags = 0;

View File

@ -16,12 +16,12 @@
#include "path-util.h"
#include "rm-rf.h"
#include "set.h"
#include "sort-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "tmpfile-util.h"
#include "user-util.h"
#include "util.h"
CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
CustomMount *c, *ret;

View File

@ -24,6 +24,7 @@
#include "set.h"
#include "signal-util.h"
#include "socket-util.h"
#include "sort-util.h"
#include "string-table.h"
#include "strv.h"
#include "tmpfile-util.h"

View File

@ -15,6 +15,7 @@
#include "memory-util.h"
#include "resolved-dns-dnssec.h"
#include "resolved-dns-packet.h"
#include "sort-util.h"
#include "string-table.h"
#define VERIFY_RRS_MAX 256

View File

@ -10,9 +10,10 @@
#include "fileio.h"
#include "hexdecoct.h"
#include "parse-util.h"
#include "resolved-dns-trust-anchor.h"
#include "resolved-dns-dnssec.h"
#include "resolved-dns-trust-anchor.h"
#include "set.h"
#include "sort-util.h"
#include "string-util.h"
#include "strv.h"

View File

@ -8,6 +8,7 @@
#include "fd-util.h"
#include "resolved-manager.h"
#include "resolved-mdns.h"
#include "sort-util.h"
#define CLEAR_CACHE_FLUSH(x) (~MDNS_RR_CACHE_FLUSH & (x))

View File

@ -21,6 +21,7 @@
#include "parse-util.h"
#include "path-util.h"
#include "pe-header.h"
#include "sort-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"

View File

@ -27,13 +27,13 @@
#include "rlimit-util.h"
#include "securebits-util.h"
#include "signal-util.h"
#include "sort-util.h"
#include "string-util.h"
#include "syslog-util.h"
#include "terminal-util.h"
#include "unit-def.h"
#include "user-util.h"
#include "utf8.h"
#include "util.h"
int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
assert(message);

View File

@ -18,6 +18,7 @@
#include "macro.h"
#include "parse-util.h"
#include "process-util.h"
#include "sort-util.h"
#include "string-util.h"
#include "time-util.h"

View File

@ -20,6 +20,7 @@
#include "output-mode.h"
#include "path-util.h"
#include "process-util.h"
#include "sort-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "unit-name.h"

View File

@ -21,11 +21,11 @@
#include "io-util.h"
#include "macro.h"
#include "parse-util.h"
#include "sort-util.h"
#include "stdio-util.h"
#include "strv.h"
#include "time-util.h"
#include "utf8.h"
#include "util.h"
#include "virt.h"
#if ENABLE_EFI

View File

@ -12,6 +12,7 @@
#include "pager.h"
#include "parse-util.h"
#include "pretty-print.h"
#include "sort-util.h"
#include "string-util.h"
#include "terminal-util.h"
#include "time-util.h"

View File

@ -6,9 +6,9 @@
#include "alloc-util.h"
#include "macro.h"
#include "sort-util.h"
#include "uid-range.h"
#include "user-util.h"
#include "util.h"
static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
assert(range);

View File

@ -66,6 +66,7 @@
#include "sigbus.h"
#include "signal-util.h"
#include "socket-util.h"
#include "sort-util.h"
#include "spawn-ask-password-agent.h"
#include "spawn-polkit-agent.h"
#include "special.h"

View File

@ -6,7 +6,7 @@
#include "prioq.h"
#include "set.h"
#include "siphash24.h"
#include "util.h"
#include "sort-util.h"
#define SET_SIZE 1024*4

View File

@ -53,6 +53,7 @@
#include "rm-rf.h"
#include "selinux-util.h"
#include "set.h"
#include "sort-util.h"
#include "specifier.h"
#include "stat-util.h"
#include "stdio-util.h"
@ -61,7 +62,6 @@
#include "strv.h"
#include "umask-util.h"
#include "user-util.h"
#include "util.h"
/* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
* them in the file system. This is intended to be used to create