blob: d856bcb583ee75c19318e1f4b91f3f6ffb89619c [file] [log] [blame]
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001From 66ece0b870b3a34fdabc48b88437e6cc354e9fce Mon Sep 17 00:00:00 2001
Brad Bishop19323692019-04-05 15:28:33 -04002From: Chen Qi <Qi.Chen@windriver.com>
3Date: Mon, 25 Feb 2019 13:41:41 +0800
Andrew Geisslerd1e89492021-02-12 15:35:20 -06004Subject: [PATCH 02/26] don't use glibc-specific qsort_r
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08005
6Upstream-Status: Inappropriate [musl specific]
7
8Signed-off-by: Khem Raj <raj.khem@gmail.com>
Brad Bishop19323692019-04-05 15:28:33 -04009[Rebased for v241]
10Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
Brad Bishopc342db32019-05-15 21:57:59 -040011[Rebased for v242]
12Signed-off-by: Andrej Valek <andrej.valek@siemens.com>
Andrew Geisslerd1e89492021-02-12 15:35:20 -060013[Rebased for v247]
14Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080015---
Andrew Geissler82c905d2020-04-13 13:39:40 -050016 src/basic/sort-util.h | 14 ------------
17 src/libsystemd/sd-hwdb/hwdb-util.c | 19 +++++++++++-----
18 src/shared/format-table.c | 36 ++++++++++++++++++++----------
Brad Bishop19323692019-04-05 15:28:33 -040019 3 files changed, 38 insertions(+), 31 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080020
Andrew Geisslerd1e89492021-02-12 15:35:20 -060021diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
22index 1d194a1f04..3394c9eb72 100644
23--- a/src/basic/sort-util.h
24+++ b/src/basic/sort-util.h
25@@ -54,17 +54,3 @@ static inline void _qsort_safe(void *base, size_t nmemb, size_t size, __compar_f
Brad Bishopc342db32019-05-15 21:57:59 -040026 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
Andrew Geissler635e0e42020-08-21 15:58:33 -050027 _qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
Brad Bishop19323692019-04-05 15:28:33 -040028 })
Brad Bishopc342db32019-05-15 21:57:59 -040029-
Brad Bishop19323692019-04-05 15:28:33 -040030-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
31- if (nmemb <= 1)
32- return;
33-
34- assert(base);
35- qsort_r(base, nmemb, size, compar, userdata);
36-}
37-
38-#define typesafe_qsort_r(p, n, func, userdata) \
39- ({ \
40- int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
41- qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
42- })
Andrew Geisslerd1e89492021-02-12 15:35:20 -060043diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
44index 4c94ba9c88..95495dba6d 100644
45--- a/src/libsystemd/sd-hwdb/hwdb-util.c
46+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
47@@ -126,9 +126,13 @@ static void trie_free(struct trie *trie) {
Brad Bishop19323692019-04-05 15:28:33 -040048
49 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
50
51-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
52- return strcmp(trie->strings->buf + a->key_off,
53- trie->strings->buf + b->key_off);
54+static struct trie *trie_node_add_value_trie;
55+static int trie_values_cmp(const void *v1, const void *v2) {
56+ const struct trie_value_entry *a = v1;
57+ const struct trie_value_entry *b = v2;
58+
59+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
60+ trie_node_add_value_trie->strings->buf + b->key_off);
61 }
62
63 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Andrew Geisslerd1e89492021-02-12 15:35:20 -060064@@ -156,7 +160,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040065 .value_off = v,
66 };
67
68- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
69+ trie_node_add_value_trie = trie;
70+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
71+ trie_node_add_value_trie = NULL;
72+
73 if (val) {
74 /* At this point we have 2 identical properties on the same match-string.
75 * Since we process files in order, we just replace the previous value. */
Andrew Geisslerd1e89492021-02-12 15:35:20 -060076@@ -182,7 +189,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040077 .line_number = line_number,
78 };
79 node->values_count++;
80- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
81+ trie_node_add_value_trie = trie;
82+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
83+ trie_node_add_value_trie = NULL;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080084 return 0;
85 }
86
Andrew Geisslerd1e89492021-02-12 15:35:20 -060087diff --git a/src/shared/format-table.c b/src/shared/format-table.c
88index a13a198b7a..bce10bc607 100644
89--- a/src/shared/format-table.c
90+++ b/src/shared/format-table.c
91@@ -1243,30 +1243,32 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
Brad Bishop19323692019-04-05 15:28:33 -040092 return CMP(index_a, index_b);
93 }
94
95-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096+static Table *user_table;
97+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040098+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080099 int r;
100
101- assert(t);
102- assert(t->sort_map);
103+ assert(user_table);
104+ assert(user_table->sort_map);
105
106 /* Make sure the header stays at the beginning */
107- if (*a < t->n_columns && *b < t->n_columns)
108+ if (*a < user_table->n_columns && *b < user_table->n_columns)
109 return 0;
110- if (*a < t->n_columns)
111+ if (*a < user_table->n_columns)
112 return -1;
113- if (*b < t->n_columns)
114+ if (*b < user_table->n_columns)
115 return 1;
116
117 /* Order other lines by the sorting map */
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600118- for (size_t i = 0; i < t->n_sort_map; i++) {
119+ for (size_t i = 0; i < user_table->n_sort_map; i++) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800120 TableData *d, *dd;
121
122- d = t->data[*a + t->sort_map[i]];
123- dd = t->data[*b + t->sort_map[i]];
124+ d = user_table->data[*a + user_table->sort_map[i]];
125+ dd = user_table->data[*b + user_table->sort_map[i]];
126
127 r = cell_data_compare(d, *a, dd, *b);
128 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -0400129- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
130+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
131 }
132
133 /* Order identical lines by the order there were originally added in */
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600134@@ -1844,7 +1846,12 @@ int table_print(Table *t, FILE *f) {
135 for (size_t i = 0; i < n_rows; i++)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800136 sorted[i] = i * t->n_columns;
137
Brad Bishop19323692019-04-05 15:28:33 -0400138- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139+ if (n_rows <= 1)
140+ return 0;
141+ assert(sorted);
142+ user_table = t;
143+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
144+ user_table = NULL;
145 }
146
147 if (t->display_map)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600148@@ -2440,7 +2447,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
149 for (size_t i = 0; i < n_rows; i++)
Brad Bishop19323692019-04-05 15:28:33 -0400150 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800151
Brad Bishop19323692019-04-05 15:28:33 -0400152- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
153+ if (n_rows <= 1)
154+ return 0;
155+ assert(sorted);
156+ user_table = t;
157+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
158+ user_table = NULL;
159 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800160
Brad Bishop19323692019-04-05 15:28:33 -0400161 if (t->display_map)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600162--
1632.27.0
164