blob: f233ee662162a3b11f12be3c0ff85bf7a8259994 [file] [log] [blame]
Andrew Geisslereff27472021-10-29 15:35:00 -05001From 30bd749c6777701496124272b59e1283252c3267 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
William A. Kennington IIIac69b482021-06-02 12:28:27 -07004Subject: [PATCH] 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>
William A. Kennington IIIac69b482021-06-02 12:28:27 -070015
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080016---
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050017 src/basic/sort-util.h | 14 --------------
18 src/shared/format-table.c | 36 ++++++++++++++++++++++++------------
19 src/shared/hwdb-util.c | 19 ++++++++++++++-----
Brad Bishop19323692019-04-05 15:28:33 -040020 3 files changed, 38 insertions(+), 31 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080021
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050022diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
23index 49586a4a24..d92a5ab0ed 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060024--- a/src/basic/sort-util.h
25+++ b/src/basic/sort-util.h
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050026@@ -55,18 +55,4 @@ static inline void _qsort_safe(void *base, size_t nmemb, size_t size, __compar_f
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 })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070029
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- })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070043-
44 int cmp_int(const int *a, const int *b);
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050045diff --git a/src/shared/format-table.c b/src/shared/format-table.c
46index 4c4e4593d8..17b329f315 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060047--- a/src/shared/format-table.c
48+++ b/src/shared/format-table.c
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050049@@ -1282,30 +1282,32 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
Brad Bishop19323692019-04-05 15:28:33 -040050 return CMP(index_a, index_b);
51 }
52
53-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080054+static Table *user_table;
55+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040056+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 int r;
58
59- assert(t);
60- assert(t->sort_map);
61+ assert(user_table);
62+ assert(user_table->sort_map);
63
64 /* Make sure the header stays at the beginning */
65- if (*a < t->n_columns && *b < t->n_columns)
66+ if (*a < user_table->n_columns && *b < user_table->n_columns)
67 return 0;
68- if (*a < t->n_columns)
69+ if (*a < user_table->n_columns)
70 return -1;
71- if (*b < t->n_columns)
72+ if (*b < user_table->n_columns)
73 return 1;
74
75 /* Order other lines by the sorting map */
Andrew Geisslerd1e89492021-02-12 15:35:20 -060076- for (size_t i = 0; i < t->n_sort_map; i++) {
77+ for (size_t i = 0; i < user_table->n_sort_map; i++) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080078 TableData *d, *dd;
79
80- d = t->data[*a + t->sort_map[i]];
81- dd = t->data[*b + t->sort_map[i]];
82+ d = user_table->data[*a + user_table->sort_map[i]];
83+ dd = user_table->data[*b + user_table->sort_map[i]];
84
85 r = cell_data_compare(d, *a, dd, *b);
86 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -040087- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
88+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
89 }
90
91 /* Order identical lines by the order there were originally added in */
Patrick Williams213cb262021-08-07 19:21:33 -050092@@ -1944,7 +1946,12 @@ int table_print(Table *t, FILE *f) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -060093 for (size_t i = 0; i < n_rows; i++)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094 sorted[i] = i * t->n_columns;
95
Brad Bishop19323692019-04-05 15:28:33 -040096- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080097+ if (n_rows <= 1)
98+ return 0;
99+ assert(sorted);
100+ user_table = t;
101+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
102+ user_table = NULL;
103 }
104
105 if (t->display_map)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500106@@ -2572,7 +2579,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600107 for (size_t i = 0; i < n_rows; i++)
Brad Bishop19323692019-04-05 15:28:33 -0400108 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109
Brad Bishop19323692019-04-05 15:28:33 -0400110- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
111+ if (n_rows <= 1)
112+ return 0;
113+ assert(sorted);
114+ user_table = t;
115+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
116+ user_table = NULL;
117 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118
Brad Bishop19323692019-04-05 15:28:33 -0400119 if (t->display_map)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500120diff --git a/src/shared/hwdb-util.c b/src/shared/hwdb-util.c
121index d7626aed95..2003fac7c3 100644
122--- a/src/shared/hwdb-util.c
123+++ b/src/shared/hwdb-util.c
124@@ -127,9 +127,13 @@ static struct trie* trie_free(struct trie *trie) {
125
126 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
127
128-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
129- return strcmp(trie->strings->buf + a->key_off,
130- trie->strings->buf + b->key_off);
131+static struct trie *trie_node_add_value_trie;
132+static int trie_values_cmp(const void *v1, const void *v2) {
133+ const struct trie_value_entry *a = v1;
134+ const struct trie_value_entry *b = v2;
135+
136+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
137+ trie_node_add_value_trie->strings->buf + b->key_off);
138 }
139
140 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
141@@ -157,7 +161,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
142 .value_off = v,
143 };
144
145- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
146+ trie_node_add_value_trie = trie;
147+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
148+ trie_node_add_value_trie = NULL;
149+
150 if (val) {
151 /* At this point we have 2 identical properties on the same match-string.
152 * Since we process files in order, we just replace the previous value. */
153@@ -183,7 +190,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
154 .line_number = line_number,
155 };
156 node->values_count++;
157- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
158+ trie_node_add_value_trie = trie;
159+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
160+ trie_node_add_value_trie = NULL;
161 return 0;
162 }
163