blob: 5027682df27dca56debbd192e71e0de191531bf0 [file] [log] [blame]
Andrew Geissler595f6302022-01-24 19:11:47 +00001From 5d730902f47498a2866b46875352f6810a01d67c 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 Geisslerd1e89492021-02-12 15:35:20 -060022--- a/src/basic/sort-util.h
23+++ b/src/basic/sort-util.h
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000024@@ -61,18 +61,4 @@ static inline void _qsort_safe(void *bas
25 _qsort_safe((p), (n), sizeof((p)[0]), (comparison_fn_t) _func_); \
Brad Bishop19323692019-04-05 15:28:33 -040026 })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070027
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000028-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, comparison_userdata_fn_t compar, void *userdata) {
Brad Bishop19323692019-04-05 15:28:33 -040029- 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; \
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000039- qsort_r_safe((p), (n), sizeof((p)[0]), (comparison_userdata_fn_t) _func_, userdata); \
Brad Bishop19323692019-04-05 15:28:33 -040040- })
William A. Kennington IIIac69b482021-06-02 12:28:27 -070041-
42 int cmp_int(const int *a, const int *b);
Andrew Geisslerd1e89492021-02-12 15:35:20 -060043--- a/src/shared/format-table.c
44+++ b/src/shared/format-table.c
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000045@@ -1324,30 +1324,32 @@ static int cell_data_compare(TableData *
Brad Bishop19323692019-04-05 15:28:33 -040046 return CMP(index_a, index_b);
47 }
48
49-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080050+static Table *user_table;
51+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040052+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080053 int r;
54
55- assert(t);
56- assert(t->sort_map);
57+ assert(user_table);
58+ assert(user_table->sort_map);
59
60 /* Make sure the header stays at the beginning */
61- if (*a < t->n_columns && *b < t->n_columns)
62+ if (*a < user_table->n_columns && *b < user_table->n_columns)
63 return 0;
64- if (*a < t->n_columns)
65+ if (*a < user_table->n_columns)
66 return -1;
67- if (*b < t->n_columns)
68+ if (*b < user_table->n_columns)
69 return 1;
70
71 /* Order other lines by the sorting map */
Andrew Geisslerd1e89492021-02-12 15:35:20 -060072- for (size_t i = 0; i < t->n_sort_map; i++) {
73+ for (size_t i = 0; i < user_table->n_sort_map; i++) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080074 TableData *d, *dd;
75
76- d = t->data[*a + t->sort_map[i]];
77- dd = t->data[*b + t->sort_map[i]];
78+ d = user_table->data[*a + user_table->sort_map[i]];
79+ dd = user_table->data[*b + user_table->sort_map[i]];
80
81 r = cell_data_compare(d, *a, dd, *b);
82 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -040083- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
84+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
85 }
86
87 /* Order identical lines by the order there were originally added in */
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000088@@ -2009,7 +2011,12 @@ int table_print(Table *t, FILE *f) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -060089 for (size_t i = 0; i < n_rows; i++)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080090 sorted[i] = i * t->n_columns;
91
Brad Bishop19323692019-04-05 15:28:33 -040092- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080093+ if (n_rows <= 1)
94+ return 0;
95+ assert(sorted);
96+ user_table = t;
97+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
98+ user_table = NULL;
99 }
100
101 if (t->display_map)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000102@@ -2647,7 +2654,12 @@ int table_to_json(Table *t, JsonVariant
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600103 for (size_t i = 0; i < n_rows; i++)
Brad Bishop19323692019-04-05 15:28:33 -0400104 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800105
Brad Bishop19323692019-04-05 15:28:33 -0400106- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
107+ if (n_rows <= 1)
108+ return 0;
109+ assert(sorted);
110+ user_table = t;
111+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
112+ user_table = NULL;
113 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800114
Brad Bishop19323692019-04-05 15:28:33 -0400115 if (t->display_map)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500116--- a/src/shared/hwdb-util.c
117+++ b/src/shared/hwdb-util.c
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000118@@ -127,9 +127,13 @@ static struct trie* trie_free(struct tri
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500119
120 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
121
122-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
123- return strcmp(trie->strings->buf + a->key_off,
124- trie->strings->buf + b->key_off);
125+static struct trie *trie_node_add_value_trie;
126+static int trie_values_cmp(const void *v1, const void *v2) {
127+ const struct trie_value_entry *a = v1;
128+ const struct trie_value_entry *b = v2;
129+
130+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
131+ trie_node_add_value_trie->strings->buf + b->key_off);
132 }
133
134 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000135@@ -157,7 +161,10 @@ static int trie_node_add_value(struct tr
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500136 .value_off = v,
137 };
138
139- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
140+ trie_node_add_value_trie = trie;
141+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
142+ trie_node_add_value_trie = NULL;
143+
144 if (val) {
145 /* At this point we have 2 identical properties on the same match-string.
146 * Since we process files in order, we just replace the previous value. */
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000147@@ -183,7 +190,9 @@ static int trie_node_add_value(struct tr
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500148 .line_number = line_number,
149 };
150 node->values_count++;
151- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
152+ trie_node_add_value_trie = trie;
153+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
154+ trie_node_add_value_trie = NULL;
155 return 0;
156 }
157