blob: c6213ab88e7e8693b1bacd6dbe7583574767175d [file] [log] [blame]
Brad Bishop19323692019-04-05 15:28:33 -04001From 1eb84534dea05d41afed1d898cba212ad7d310dd Mon Sep 17 00:00:00 2001
2From: Chen Qi <Qi.Chen@windriver.com>
3Date: Mon, 25 Feb 2019 13:41:41 +0800
4Subject: [PATCH 02/24] 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>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080013---
Brad Bishopc342db32019-05-15 21:57:59 -040014 src/basic/sort-util.h | 14 --------------
Brad Bishop19323692019-04-05 15:28:33 -040015 src/libsystemd/sd-hwdb/hwdb-util.c | 19 ++++++++++++++-----
16 src/shared/format-table.c | 36 ++++++++++++++++++++++++------------
17 3 files changed, 38 insertions(+), 31 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080018
Brad Bishopc342db32019-05-15 21:57:59 -040019diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
20index e029f8646e..27d68b341c 100644
21--- a/src/basic/sort-util.h
22+++ b/src/basic/sort-util.h
23@@ -54,17 +54,3 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn
24 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
Brad Bishop19323692019-04-05 15:28:33 -040025 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
26 })
Brad Bishopc342db32019-05-15 21:57:59 -040027-
Brad Bishop19323692019-04-05 15:28:33 -040028-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
29- if (nmemb <= 1)
30- return;
31-
32- assert(base);
33- qsort_r(base, nmemb, size, compar, userdata);
34-}
35-
36-#define typesafe_qsort_r(p, n, func, userdata) \
37- ({ \
38- int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
39- qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
40- })
Brad Bishop19323692019-04-05 15:28:33 -040041diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
Brad Bishopc342db32019-05-15 21:57:59 -040042index c83575c7c8..72f8f3a050 100644
Brad Bishop19323692019-04-05 15:28:33 -040043--- a/src/libsystemd/sd-hwdb/hwdb-util.c
44+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
Brad Bishopc342db32019-05-15 21:57:59 -040045@@ -128,9 +128,13 @@ static void trie_free(struct trie *trie) {
Brad Bishop19323692019-04-05 15:28:33 -040046
47 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
48
49-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
50- return strcmp(trie->strings->buf + a->key_off,
51- trie->strings->buf + b->key_off);
52+static struct trie *trie_node_add_value_trie;
53+static int trie_values_cmp(const void *v1, const void *v2) {
54+ const struct trie_value_entry *a = v1;
55+ const struct trie_value_entry *b = v2;
56+
57+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
58+ trie_node_add_value_trie->strings->buf + b->key_off);
59 }
60
61 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishopc342db32019-05-15 21:57:59 -040062@@ -158,7 +162,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040063 .value_off = v,
64 };
65
66- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
67+ trie_node_add_value_trie = trie;
68+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
69+ trie_node_add_value_trie = NULL;
70+
71 if (val) {
72 /* At this point we have 2 identical properties on the same match-string.
73 * Since we process files in order, we just replace the previous value. */
Brad Bishopc342db32019-05-15 21:57:59 -040074@@ -184,7 +191,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040075 .line_number = line_number,
76 };
77 node->values_count++;
78- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
79+ trie_node_add_value_trie = trie;
80+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
81+ trie_node_add_value_trie = NULL;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080082 return 0;
83 }
84
Brad Bishop19323692019-04-05 15:28:33 -040085diff --git a/src/shared/format-table.c b/src/shared/format-table.c
Brad Bishopc342db32019-05-15 21:57:59 -040086index a5c0a99b08..d595cbe372 100644
Brad Bishop19323692019-04-05 15:28:33 -040087--- a/src/shared/format-table.c
88+++ b/src/shared/format-table.c
Brad Bishopc342db32019-05-15 21:57:59 -040089@@ -850,31 +850,33 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
Brad Bishop19323692019-04-05 15:28:33 -040090 return CMP(index_a, index_b);
91 }
92
93-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094+static Table *user_table;
95+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040096+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080097 size_t i;
98 int r;
99
100- assert(t);
101- assert(t->sort_map);
102+ assert(user_table);
103+ assert(user_table->sort_map);
104
105 /* Make sure the header stays at the beginning */
106- if (*a < t->n_columns && *b < t->n_columns)
107+ if (*a < user_table->n_columns && *b < user_table->n_columns)
108 return 0;
109- if (*a < t->n_columns)
110+ if (*a < user_table->n_columns)
111 return -1;
112- if (*b < t->n_columns)
113+ if (*b < user_table->n_columns)
114 return 1;
115
116 /* Order other lines by the sorting map */
117- for (i = 0; i < t->n_sort_map; i++) {
118+ for (i = 0; i < user_table->n_sort_map; i++) {
119 TableData *d, *dd;
120
121- d = t->data[*a + t->sort_map[i]];
122- dd = t->data[*b + t->sort_map[i]];
123+ d = user_table->data[*a + user_table->sort_map[i]];
124+ dd = user_table->data[*b + user_table->sort_map[i]];
125
126 r = cell_data_compare(d, *a, dd, *b);
127 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -0400128- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
129+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
130 }
131
132 /* Order identical lines by the order there were originally added in */
Brad Bishopc342db32019-05-15 21:57:59 -0400133@@ -1107,7 +1109,12 @@ int table_print(Table *t, FILE *f) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 for (i = 0; i < n_rows; i++)
135 sorted[i] = i * t->n_columns;
136
Brad Bishop19323692019-04-05 15:28:33 -0400137- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138+ if (n_rows <= 1)
139+ return 0;
140+ assert(sorted);
141+ user_table = t;
142+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
143+ user_table = NULL;
144 }
145
146 if (t->display_map)
Brad Bishopc342db32019-05-15 21:57:59 -0400147@@ -1534,7 +1541,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
Brad Bishop19323692019-04-05 15:28:33 -0400148 for (i = 0; i < n_rows; i++)
149 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800150
Brad Bishop19323692019-04-05 15:28:33 -0400151- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
152+ if (n_rows <= 1)
153+ return 0;
154+ assert(sorted);
155+ user_table = t;
156+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
157+ user_table = NULL;
158 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800159
Brad Bishop19323692019-04-05 15:28:33 -0400160 if (t->display_map)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800161--
Brad Bishopc342db32019-05-15 21:57:59 -04001622.11.0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800163