summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/systemd/systemd/0002-don-t-use-glibc-specific-qsort_r.patch
blob: 6b85ff0f89152eadc135902b093a12246c3712d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
From 49501c80d32c1bc5ecb07f40c324feb82af0b057 Mon Sep 17 00:00:00 2001
From: Chen Qi <Qi.Chen@windriver.com>
Date: Mon, 25 Feb 2019 13:41:41 +0800
Subject: [PATCH] don't use glibc-specific qsort_r

Upstream-Status: Inappropriate [musl specific]

Signed-off-by: Khem Raj <raj.khem@gmail.com>
[Rebased for v241]
Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
[Rebased for v242]
Signed-off-by: Andrej Valek <andrej.valek@siemens.com>

---
 src/basic/sort-util.h              | 14 ------------
 src/libsystemd/sd-hwdb/hwdb-util.c | 19 +++++++++++-----
 src/shared/format-table.c          | 36 ++++++++++++++++++++----------
 3 files changed, 38 insertions(+), 31 deletions(-)

diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h
index e029f8646eb0..27d68b341cf3 100644
--- a/src/basic/sort-util.h
+++ b/src/basic/sort-util.h
@@ -54,17 +54,3 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn
                 int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \
                 qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
         })
-
-static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) {
-        if (nmemb <= 1)
-                return;
-
-        assert(base);
-        qsort_r(base, nmemb, size, compar, userdata);
-}
-
-#define typesafe_qsort_r(p, n, func, userdata)                          \
-        ({                                                              \
-                int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \
-                qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \
-        })
diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c
index c83575c7c876..72f8f3a05048 100644
--- a/src/libsystemd/sd-hwdb/hwdb-util.c
+++ b/src/libsystemd/sd-hwdb/hwdb-util.c
@@ -128,9 +128,13 @@ static void trie_free(struct trie *trie) {
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
 
-static int trie_values_cmp(const struct trie_value_entry *a, const struct trie_value_entry *b, struct trie *trie) {
-        return strcmp(trie->strings->buf + a->key_off,
-                      trie->strings->buf + b->key_off);
+static struct trie *trie_node_add_value_trie;
+static int trie_values_cmp(const void *v1, const void *v2) {
+        const struct trie_value_entry *a = v1;
+        const struct trie_value_entry *b = v2;
+
+        return strcmp(trie_node_add_value_trie->strings->buf + a->key_off,
+                      trie_node_add_value_trie->strings->buf + b->key_off);
 }
 
 static int trie_node_add_value(struct trie *trie, struct trie_node *node,
@@ -158,7 +162,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                         .value_off = v,
                 };
 
-                val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
+                trie_node_add_value_trie = trie;
+                val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
+                trie_node_add_value_trie = NULL;
+
                 if (val) {
                         /* At this point we have 2 identical properties on the same match-string.
                          * Since we process files in order, we just replace the previous value. */
@@ -184,7 +191,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                 .line_number = line_number,
         };
         node->values_count++;
-        typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
+        trie_node_add_value_trie = trie;
+        qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp);
+        trie_node_add_value_trie = NULL;
         return 0;
 }
 
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index 4617ae8badc4..17d6b9616256 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -1109,31 +1109,33 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
         return CMP(index_a, index_b);
 }
 
-static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
+static Table *user_table;
+static int table_data_compare(const void *x, const void *y) {
+        const size_t *a = x, *b=y;
         size_t i;
         int r;
 
-        assert(t);
-        assert(t->sort_map);
+        assert(user_table);
+        assert(user_table->sort_map);
 
         /* Make sure the header stays at the beginning */
-        if (*a < t->n_columns && *b < t->n_columns)
+        if (*a < user_table->n_columns && *b < user_table->n_columns)
                 return 0;
-        if (*a < t->n_columns)
+        if (*a < user_table->n_columns)
                 return -1;
-        if (*b < t->n_columns)
+        if (*b < user_table->n_columns)
                 return 1;
 
         /* Order other lines by the sorting map */
-        for (i = 0; i < t->n_sort_map; i++) {
+        for (i = 0; i < user_table->n_sort_map; i++) {
                 TableData *d, *dd;
 
-                d = t->data[*a + t->sort_map[i]];
-                dd = t->data[*b + t->sort_map[i]];
+                d = user_table->data[*a + user_table->sort_map[i]];
+                dd = user_table->data[*b + user_table->sort_map[i]];
 
                 r = cell_data_compare(d, *a, dd, *b);
                 if (r != 0)
-                        return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
+                        return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r;
         }
 
         /* Order identical lines by the order there were originally added in */
@@ -1533,7 +1535,12 @@ int table_print(Table *t, FILE *f) {
                 for (i = 0; i < n_rows; i++)
                         sorted[i] = i * t->n_columns;
 
-                typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
+                if (n_rows <= 1)
+                        return 0;
+                assert(sorted);
+                user_table = t;
+                qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
+                user_table = NULL;
         }
 
         if (t->display_map)
@@ -1997,7 +2004,12 @@ int table_to_json(Table *t, JsonVariant **ret) {
                 for (i = 0; i < n_rows; i++)
                         sorted[i] = i * t->n_columns;
 
-                typesafe_qsort_r(sorted, n_rows, table_data_compare, t);
+                if (n_rows <= 1)
+                        return 0;
+                assert(sorted);
+                user_table = t;
+                qsort(sorted, n_rows, sizeof(size_t), table_data_compare);
+                user_table = NULL;
         }
 
         if (t->display_map)