X Tutup
Skip to content

Commit 760877e

Browse files
committed
util: split out sorting related calls to new sort-util.[ch]
1 parent 0a97071 commit 760877e

39 files changed

+136
-108
lines changed

src/analyze/analyze.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636
#if HAVE_SECCOMP
3737
# include "seccomp-util.h"
3838
#endif
39+
#include "sort-util.h"
3940
#include "special.h"
4041
#include "strv.h"
4142
#include "strxcpyx.h"
42-
#include "time-util.h"
4343
#include "terminal-util.h"
44+
#include "time-util.h"
4445
#include "unit-name.h"
4546
#include "util.h"
4647
#include "verbs.h"

src/basic/conf-files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#include "missing.h"
1818
#include "path-util.h"
1919
#include "set.h"
20+
#include "sort-util.h"
2021
#include "stat-util.h"
2122
#include "string-util.h"
2223
#include "strv.h"
2324
#include "terminal-util.h"
24-
#include "util.h"
2525

2626
static int files_add(
2727
Hashmap *h,

src/basic/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ basic_sources = files('''
174174
socket-label.c
175175
socket-util.c
176176
socket-util.h
177+
sort-util.c
178+
sort-util.h
177179
sparse-endian.h
178180
special.h
179181
stat-util.c

src/basic/sort-util.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "sort-util.h"
2+
#include "alloc-util.h"
3+
4+
/* hey glibc, APIs with callbacks without a user pointer are so useless */
5+
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
6+
__compar_d_fn_t compar, void *arg) {
7+
size_t l, u, idx;
8+
const void *p;
9+
int comparison;
10+
11+
assert(!size_multiply_overflow(nmemb, size));
12+
13+
l = 0;
14+
u = nmemb;
15+
while (l < u) {
16+
idx = (l + u) / 2;
17+
p = (const uint8_t*) base + idx * size;
18+
comparison = compar(key, p, arg);
19+
if (comparison < 0)
20+
u = idx;
21+
else if (comparison > 0)
22+
l = idx + 1;
23+
else
24+
return (void *)p;
25+
}
26+
return NULL;
27+
}

src/basic/sort-util.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
#include <stdlib.h>
5+
6+
#include "macro.h"
7+
8+
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
9+
__compar_d_fn_t compar, void *arg);
10+
11+
#define typesafe_bsearch_r(k, b, n, func, userdata) \
12+
({ \
13+
const typeof(b[0]) *_k = k; \
14+
int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
15+
xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
16+
})
17+
18+
/**
19+
* Normal bsearch requires base to be nonnull. Here were require
20+
* that only if nmemb > 0.
21+
*/
22+
static inline void* bsearch_safe(const void *key, const void *base,
23+
size_t nmemb, size_t size, __compar_fn_t compar) {
24+
if (nmemb <= 0)
25+
return NULL;
26+
27+
assert(base);
28+
return bsearch(key, base, nmemb, size, compar);
29+
}
30+
31+
#define typesafe_bsearch(k, b, n, func) \
32+
({ \
33+
const typeof(b[0]) *_k = k; \
34+
int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
35+
bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
36+
})
37+
38+
/**
39+
* Normal qsort requires base to be nonnull. Here were require
40+
* that only if nmemb > 0.
41+
*/
42+
static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) {
43+
if (nmemb <= 1)
44+
return;
45+
46+
assert(base);
47+
qsort(base, nmemb, size, compar);
48+
}
49+
50+
/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
51+
* is the prototype for the comparison function */
52+
#define typesafe_qsort(p, n, func) \
53+
({ \
54+
int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
55+
qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
56+
})
57+
58+
static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
59+
if (nmemb <= 1)
60+
return;
61+
62+
assert(base);
63+
qsort_r(base, nmemb, size, compar, userdata);
64+
}
65+
66+
#define typesafe_qsort_r(p, n, func, userdata) \
67+
({ \
68+
int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
69+
qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
70+
})

src/basic/strbuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <string.h>
66

77
#include "alloc-util.h"
8+
#include "sort-util.h"
89
#include "strbuf.h"
9-
#include "util.h"
1010

1111
/*
1212
* Strbuf stores given strings in a single continuous allocated memory

src/basic/strv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "escape.h"
1212
#include "extract-word.h"
1313
#include "fileio.h"
14+
#include "sort-util.h"
1415
#include "string-util.h"
1516
#include "strv.h"
16-
#include "util.h"
1717

1818
char *strv_find(char **l, const char *name) {
1919
char **i;

src/basic/util.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,6 @@ void in_initrd_force(bool value) {
126126
saved_in_initrd = value;
127127
}
128128

129-
/* hey glibc, APIs with callbacks without a user pointer are so useless */
130-
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
131-
__compar_d_fn_t compar, void *arg) {
132-
size_t l, u, idx;
133-
const void *p;
134-
int comparison;
135-
136-
assert(!size_multiply_overflow(nmemb, size));
137-
138-
l = 0;
139-
u = nmemb;
140-
while (l < u) {
141-
idx = (l + u) / 2;
142-
p = (const uint8_t*) base + idx * size;
143-
comparison = compar(key, p, arg);
144-
if (comparison < 0)
145-
u = idx;
146-
else if (comparison > 0)
147-
l = idx + 1;
148-
else
149-
return (void *)p;
150-
}
151-
return NULL;
152-
}
153-
154129
int on_ac_power(void) {
155130
bool found_offline = false, found_online = false;
156131
_cleanup_closedir_ DIR *d = NULL;

src/basic/util.h

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -63,70 +63,6 @@ int prot_from_flags(int flags) _const_;
6363
bool in_initrd(void);
6464
void in_initrd_force(bool value);
6565

66-
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
67-
__compar_d_fn_t compar, void *arg);
68-
69-
#define typesafe_bsearch_r(k, b, n, func, userdata) \
70-
({ \
71-
const typeof(b[0]) *_k = k; \
72-
int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \
73-
xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \
74-
})
75-
76-
/**
77-
* Normal bsearch requires base to be nonnull. Here were require
78-
* that only if nmemb > 0.
79-
*/
80-
static inline void* bsearch_safe(const void *key, const void *base,
81-
size_t nmemb, size_t size, __compar_fn_t compar) {
82-
if (nmemb <= 0)
83-
return NULL;
84-
85-
assert(base);
86-
return bsearch(key, base, nmemb, size, compar);
87-
}
88-
89-
#define typesafe_bsearch(k, b, n, func) \
90-
({ \
91-
const typeof(b[0]) *_k = k; \
92-
int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \
93-
bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \
94-
})
95-
96-
/**
97-
* Normal qsort requires base to be nonnull. Here were require
98-
* that only if nmemb > 0.
99-
*/
100-
static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) {
101-
if (nmemb <= 1)
102-
return;
103-
104-
assert(base);
105-
qsort(base, nmemb, size, compar);
106-
}
107-
108-
/* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
109-
* is the prototype for the comparison function */
110-
#define typesafe_qsort(p, n, func) \
111-
({ \
112-
int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
113-
qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
114-
})
115-
116-
static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
117-
if (nmemb <= 1)
118-
return;
119-
120-
assert(base);
121-
qsort_r(base, nmemb, size, compar, userdata);
122-
}
123-
124-
#define typesafe_qsort_r(p, n, func, userdata) \
125-
({ \
126-
int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
127-
qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
128-
})
129-
13066
int on_ac_power(void);
13167

13268
static inline void _reset_errno_(int *saved_errno) {

src/busctl/busctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#include "path-util.h"
2626
#include "pretty-print.h"
2727
#include "set.h"
28+
#include "sort-util.h"
2829
#include "strv.h"
2930
#include "terminal-util.h"
3031
#include "user-util.h"
31-
#include "util.h"
3232
#include "verbs.h"
3333

3434
static enum {

0 commit comments

Comments
 (0)
X Tutup