blob: 9db22cd9d2ee95f00e52a590d1774aeb22dc9be2 [file] [log] [blame]
William A. Kennington IIIac69b482021-06-02 12:28:27 -07001From 159c53612444ec1df492bae528a5a88a275b93bf 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 Geissler82c905d2020-04-13 13:39:40 -050017 src/basic/sort-util.h | 14 ------------
18 src/libsystemd/sd-hwdb/hwdb-util.c | 19 +++++++++++-----
19 src/shared/format-table.c | 36 ++++++++++++++++++++----------
Brad Bishop19323692019-04-05 15:28:33 -040020 3 files changed, 38 insertions(+), 31 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080021
Andrew Geisslerd1e89492021-02-12 15:35:20 -060022--- a/src/basic/sort-util.h
23+++ b/src/basic/sort-util.h
Patrick Williams213cb262021-08-07 19:21:33 -050024@@ -56,18 +56,4 @@ static inline void _qsort_safe(void *bas
Andrew Geissler635e0e42020-08-21 15:58:33 -050025 _qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
Brad Bishop19323692019-04-05 15:28:33 -040026 })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070027
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- })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070041-
42 int cmp_int(const int *a, const int *b);
Patrick Williams213cb262021-08-07 19:21:33 -050043--- a/src/shared/hwdb-util.c
44+++ b/src/shared/hwdb-util.c
45@@ -127,9 +127,13 @@ static struct trie* trie_free(struct tri
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,
Patrick Williams213cb262021-08-07 19:21:33 -050062@@ -157,7 +161,10 @@ static int trie_node_add_value(struct tr
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. */
Patrick Williams213cb262021-08-07 19:21:33 -050074@@ -183,7 +190,9 @@ static int trie_node_add_value(struct tr
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
Andrew Geisslerd1e89492021-02-12 15:35:20 -060085--- a/src/shared/format-table.c
86+++ b/src/shared/format-table.c
Patrick Williams213cb262021-08-07 19:21:33 -050087@@ -1282,30 +1282,32 @@ static int cell_data_compare(TableData *
Brad Bishop19323692019-04-05 15:28:33 -040088 return CMP(index_a, index_b);
89 }
90
91-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080092+static Table *user_table;
93+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040094+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095 int r;
96
97- assert(t);
98- assert(t->sort_map);
99+ assert(user_table);
100+ assert(user_table->sort_map);
101
102 /* Make sure the header stays at the beginning */
103- if (*a < t->n_columns && *b < t->n_columns)
104+ if (*a < user_table->n_columns && *b < user_table->n_columns)
105 return 0;
106- if (*a < t->n_columns)
107+ if (*a < user_table->n_columns)
108 return -1;
109- if (*b < t->n_columns)
110+ if (*b < user_table->n_columns)
111 return 1;
112
113 /* Order other lines by the sorting map */
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600114- for (size_t i = 0; i < t->n_sort_map; i++) {
115+ for (size_t i = 0; i < user_table->n_sort_map; i++) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800116 TableData *d, *dd;
117
118- d = t->data[*a + t->sort_map[i]];
119- dd = t->data[*b + t->sort_map[i]];
120+ d = user_table->data[*a + user_table->sort_map[i]];
121+ dd = user_table->data[*b + user_table->sort_map[i]];
122
123 r = cell_data_compare(d, *a, dd, *b);
124 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -0400125- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
126+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
127 }
128
129 /* Order identical lines by the order there were originally added in */
Patrick Williams213cb262021-08-07 19:21:33 -0500130@@ -1944,7 +1946,12 @@ int table_print(Table *t, FILE *f) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600131 for (size_t i = 0; i < n_rows; i++)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800132 sorted[i] = i * t->n_columns;
133
Brad Bishop19323692019-04-05 15:28:33 -0400134- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135+ if (n_rows <= 1)
136+ return 0;
137+ assert(sorted);
138+ user_table = t;
139+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
140+ user_table = NULL;
141 }
142
143 if (t->display_map)
Patrick Williams213cb262021-08-07 19:21:33 -0500144@@ -2572,7 +2579,12 @@ int table_to_json(Table *t, JsonVariant
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600145 for (size_t i = 0; i < n_rows; i++)
Brad Bishop19323692019-04-05 15:28:33 -0400146 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147
Brad Bishop19323692019-04-05 15:28:33 -0400148- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
149+ if (n_rows <= 1)
150+ return 0;
151+ assert(sorted);
152+ user_table = t;
153+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
154+ user_table = NULL;
155 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800156
Brad Bishop19323692019-04-05 15:28:33 -0400157 if (t->display_map)