blob: 36e0699da9afe761395845c33edaa04afcb62e4f [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 Bishop1a4b7ee2018-12-16 17:11:34 -080011---
Brad Bishop19323692019-04-05 15:28:33 -040012 src/basic/util.h | 14 --------------
13 src/libsystemd/sd-hwdb/hwdb-util.c | 19 ++++++++++++++-----
14 src/shared/format-table.c | 36 ++++++++++++++++++++++++------------
15 3 files changed, 38 insertions(+), 31 deletions(-)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080016
Brad Bishop19323692019-04-05 15:28:33 -040017diff --git a/src/basic/util.h b/src/basic/util.h
18index dc33d66..9f6a6ce 100644
19--- a/src/basic/util.h
20+++ b/src/basic/util.h
21@@ -116,20 +116,6 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn
22 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
23 })
24
25-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
26- if (nmemb <= 1)
27- return;
28-
29- assert(base);
30- qsort_r(base, nmemb, size, compar, userdata);
31-}
32-
33-#define typesafe_qsort_r(p, n, func, userdata) \
34- ({ \
35- int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
36- qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
37- })
38-
39 /* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */
40 static inline void memcpy_safe(void *dst, const void *src, size_t n) {
41 if (n == 0)
42diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
43index f852967..b570ce1 100644
44--- a/src/libsystemd/sd-hwdb/hwdb-util.c
45+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
46@@ -126,9 +126,13 @@ static void trie_free(struct trie *trie) {
47
48 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
49
50-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
51- return strcmp(trie->strings->buf + a->key_off,
52- trie->strings->buf + b->key_off);
53+static struct trie *trie_node_add_value_trie;
54+static int trie_values_cmp(const void *v1, const void *v2) {
55+ const struct trie_value_entry *a = v1;
56+ const struct trie_value_entry *b = v2;
57+
58+ return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
59+ trie_node_add_value_trie->strings->buf + b->key_off);
60 }
61
62 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
63@@ -156,7 +160,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
64 .value_off = v,
65 };
66
67- val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
68+ trie_node_add_value_trie = trie;
69+ val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
70+ trie_node_add_value_trie = NULL;
71+
72 if (val) {
73 /* At this point we have 2 identical properties on the same match-string.
74 * Since we process files in order, we just replace the previous value. */
75@@ -182,7 +189,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
76 .line_number = line_number,
77 };
78 node->values_count++;
79- typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
80+ trie_node_add_value_trie = trie;
81+ qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
82+ trie_node_add_value_trie = NULL;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080083 return 0;
84 }
85
Brad Bishop19323692019-04-05 15:28:33 -040086diff --git a/src/shared/format-table.c b/src/shared/format-table.c
87index 7d52980..75dbfe1 100644
88--- a/src/shared/format-table.c
89+++ b/src/shared/format-table.c
90@@ -848,31 +848,33 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
91 return CMP(index_a, index_b);
92 }
93
94-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095+static Table *user_table;
96+static int table_data_compare(const void *x, const void *y) {
Brad Bishop19323692019-04-05 15:28:33 -040097+ const size_t *a = x, *b=y;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080098 size_t i;
99 int r;
100
101- assert(t);
102- assert(t->sort_map);
103+ assert(user_table);
104+ assert(user_table->sort_map);
105
106 /* Make sure the header stays at the beginning */
107- if (*a < t->n_columns && *b < t->n_columns)
108+ if (*a < user_table->n_columns && *b < user_table->n_columns)
109 return 0;
110- if (*a < t->n_columns)
111+ if (*a < user_table->n_columns)
112 return -1;
113- if (*b < t->n_columns)
114+ if (*b < user_table->n_columns)
115 return 1;
116
117 /* Order other lines by the sorting map */
118- for (i = 0; i < t->n_sort_map; i++) {
119+ for (i = 0; i < user_table->n_sort_map; i++) {
120 TableData *d, *dd;
121
122- d = t->data[*a + t->sort_map[i]];
123- dd = t->data[*b + t->sort_map[i]];
124+ d = user_table->data[*a + user_table->sort_map[i]];
125+ dd = user_table->data[*b + user_table->sort_map[i]];
126
127 r = cell_data_compare(d, *a, dd, *b);
128 if (r != 0)
Brad Bishop19323692019-04-05 15:28:33 -0400129- return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
130+ return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
131 }
132
133 /* Order identical lines by the order there were originally added in */
134@@ -1105,7 +1107,12 @@ int table_print(Table *t, FILE *f) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135 for (i = 0; i < n_rows; i++)
136 sorted[i] = i * t->n_columns;
137
Brad Bishop19323692019-04-05 15:28:33 -0400138- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139+ if (n_rows <= 1)
140+ return 0;
141+ assert(sorted);
142+ user_table = t;
143+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
144+ user_table = NULL;
145 }
146
147 if (t->display_map)
Brad Bishop19323692019-04-05 15:28:33 -0400148@@ -1532,7 +1539,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
149 for (i = 0; i < n_rows; i++)
150 sorted[i] = i * t->n_columns;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800151
Brad Bishop19323692019-04-05 15:28:33 -0400152- typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
153+ if (n_rows <= 1)
154+ return 0;
155+ assert(sorted);
156+ user_table = t;
157+ qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
158+ user_table = NULL;
159 }
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800160
Brad Bishop19323692019-04-05 15:28:33 -0400161 if (t->display_map)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800162--
Brad Bishop19323692019-04-05 15:28:33 -04001632.7.4
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800164