blob: 4cdf66e7676c3da7985783cb3c25b036c5f77862 [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 -060022diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
William A. Kennington IIIac69b482021-06-02 12:28:27 -070023index 49586a4a24..d92a5ab0ed 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060024--- a/src/basic/sort-util.h
25+++ b/src/basic/sort-util.h
William A. Kennington IIIac69b482021-06-02 12:28:27 -070026@@ -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 Geisslerd1e89492021-02-12 15:35:20 -060045diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
William A. Kennington IIIac69b482021-06-02 12:28:27 -070046index fd45ff0f54..ac4b63c49b 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060047--- a/src/libsystemd/sd-hwdb/hwdb-util.c
48+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
William A. Kennington IIIac69b482021-06-02 12:28:27 -070049@@ -126,9 +126,13 @@ static struct trie* trie_free(struct trie *trie) {
Brad Bishop19323692019-04-05 15:28:33 -040050
51 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
52
53-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
54- return strcmp(trie->strings->buf + a->key_off,
55- trie->strings->buf + b->key_off);
56+static struct trie *trie_node_add_value_trie;
57+static int trie_values_cmp(const void *v1, const void *v2) {
58+ const struct trie_value_entry *a = v1;
59+ const struct trie_value_entry *b = v2;
60+
61+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
62+ trie_node_add_value_trie->strings->buf + b->key_off);
63 }
64
65 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Andrew Geisslerd1e89492021-02-12 15:35:20 -060066@@ -156,7 +160,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040067 .value_off = v,
68 };
69
70- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
71+ trie_node_add_value_trie = trie;
72+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
73+ trie_node_add_value_trie = NULL;
74+
75 if (val) {
76 /* At this point we have 2 identical properties on the same match-string.
77 * Since we process files in order, we just replace the previous value. */
Andrew Geisslerd1e89492021-02-12 15:35:20 -060078@@ -182,7 +189,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
Brad Bishop19323692019-04-05 15:28:33 -040079 .line_number = line_number,
80 };
81 node->values_count++;
82- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
83+ trie_node_add_value_trie = trie;
84+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
85+ trie_node_add_value_trie = NULL;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080086 return 0;
87 }
88
Andrew Geisslerd1e89492021-02-12 15:35:20 -060089diff --git a/src/shared/format-table.c b/src/shared/format-table.c
William A. Kennington IIIac69b482021-06-02 12:28:27 -070090index dccb796b26..c3ab8ac296 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060091--- a/src/shared/format-table.c
92+++ b/src/shared/format-table.c
William A. Kennington IIIac69b482021-06-02 12:28:27 -070093@@ -1290,30 +1290,32 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
Brad Bishop19323692019-04-05 15:28:33 -040094 return CMP(index_a, index_b);
95 }
96
97-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080098+static Table *user_table;
99+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -0400100+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800101 int r;
102
103- assert(t);
104- assert(t->sort_map);
105+ assert(user_table);
106+ assert(user_table->sort_map);
107
108 /* Make sure the header stays at the beginning */
109- if (*a < t->n_columns && *b < t->n_columns)
110+ if (*a < user_table->n_columns && *b < user_table->n_columns)
111 return 0;
112- if (*a < t->n_columns)
113+ if (*a < user_table->n_columns)
114 return -1;
115- if (*b < t->n_columns)
116+ if (*b < user_table->n_columns)
117 return 1;
118
119 /* Order other lines by the sorting map */
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600120- for (size_t i = 0; i < t->n_sort_map; i++) {
121+ for (size_t i = 0; i < user_table->n_sort_map; i++) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 TableData *d, *dd;
123
124- d = t->data[*a + t->sort_map[i]];
125- dd = t->data[*b + t->sort_map[i]];
126+ d = user_table->data[*a + user_table->sort_map[i]];
127+ dd = user_table->data[*b + user_table->sort_map[i]];
128
129 r = cell_data_compare(d, *a, dd, *b);
130 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -0400131- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
132+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
133 }
134
135 /* Order identical lines by the order there were originally added in */
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700136@@ -1952,7 +1954,12 @@ int table_print(Table *t, FILE *f) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600137 for (size_t i = 0; i < n_rows; i++)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138 sorted[i] = i * t->n_columns;
139
Brad Bishop19323692019-04-05 15:28:33 -0400140- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141+ if (n_rows <= 1)
142+ return 0;
143+ assert(sorted);
144+ user_table = t;
145+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
146+ user_table = NULL;
147 }
148
149 if (t->display_map)
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700150@@ -2580,7 +2587,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600151 for (size_t i = 0; i < n_rows; i++)
Brad Bishop19323692019-04-05 15:28:33 -0400152 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800153
Brad Bishop19323692019-04-05 15:28:33 -0400154- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
155+ if (n_rows <= 1)
156+ return 0;
157+ assert(sorted);
158+ user_table = t;
159+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
160+ user_table = NULL;
161 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800162
Brad Bishop19323692019-04-05 15:28:33 -0400163 if (t->display_map)