Brad Bishop | e42b3e3 | 2020-01-15 22:08:42 -0500 | [diff] [blame^] | 1 | From 1eb84534dea05d41afed1d898cba212ad7d310dd Mon Sep 17 00:00:00 2001 |
| 2 | From: Chen Qi <Qi.Chen@windriver.com> |
| 3 | Date: Mon, 25 Feb 2019 13:41:41 +0800 |
| 4 | Subject: [PATCH 02/24] don't use glibc-specific qsort_r |
| 5 | |
| 6 | Upstream-Status: Inappropriate [musl specific] |
| 7 | |
| 8 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 9 | [Rebased for v241] |
| 10 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 11 | [Rebased for v242] |
| 12 | Signed-off-by: Andrej Valek <andrej.valek@siemens.com> |
| 13 | --- |
| 14 | src/basic/sort-util.h | 14 -------------- |
| 15 | src/libsystemd/sd-hwdb/hwdb-util.c | 19 ++++++++++++++----- |
| 16 | src/shared/format-table.c | 36 ++++++++++++++++++++++++------------ |
| 17 | 3 files changed, 38 insertions(+), 31 deletions(-) |
| 18 | |
| 19 | diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h |
| 20 | index e029f8646e..27d68b341c 100644 |
| 21 | --- a/src/basic/sort-util.h |
| 22 | +++ b/src/basic/sort-util.h |
| 23 | @@ -54,17 +54,3 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn |
| 24 | int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \ |
| 25 | qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \ |
| 26 | }) |
| 27 | - |
| 28 | -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 | - }) |
| 41 | diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c |
| 42 | index c83575c7c8..72f8f3a050 100644 |
| 43 | --- a/src/libsystemd/sd-hwdb/hwdb-util.c |
| 44 | +++ b/src/libsystemd/sd-hwdb/hwdb-util.c |
| 45 | @@ -128,9 +128,13 @@ static void trie_free(struct trie *trie) { |
| 46 | |
| 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, |
| 62 | @@ -158,7 +162,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 63 | .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. */ |
| 74 | @@ -184,7 +191,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 75 | .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; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | diff --git a/src/shared/format-table.c b/src/shared/format-table.c |
| 86 | index a5c0a99b08..d595cbe372 100644 |
| 87 | --- a/src/shared/format-table.c |
| 88 | +++ b/src/shared/format-table.c |
| 89 | @@ -850,31 +850,33 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t |
| 90 | return CMP(index_a, index_b); |
| 91 | } |
| 92 | |
| 93 | -static int table_data_compare(const size_t *a, const size_t *b, Table *t) { |
| 94 | +static Table *user_table; |
| 95 | +static int table_data_compare(const void *x, const void *y) { |
| 96 | + const size_t *a = x, *b=y; |
| 97 | size_t i; |
| 98 | int r; |
| 99 | |
| 100 | - assert(t); |
| 101 | - assert(t->sort_map); |
| 102 | + assert(user_table); |
| 103 | + assert(user_table->sort_map); |
| 104 | |
| 105 | /* Make sure the header stays at the beginning */ |
| 106 | - if (*a < t->n_columns && *b < t->n_columns) |
| 107 | + if (*a < user_table->n_columns && *b < user_table->n_columns) |
| 108 | return 0; |
| 109 | - if (*a < t->n_columns) |
| 110 | + if (*a < user_table->n_columns) |
| 111 | return -1; |
| 112 | - if (*b < t->n_columns) |
| 113 | + if (*b < user_table->n_columns) |
| 114 | return 1; |
| 115 | |
| 116 | /* Order other lines by the sorting map */ |
| 117 | - for (i = 0; i < t->n_sort_map; i++) { |
| 118 | + for (i = 0; i < user_table->n_sort_map; i++) { |
| 119 | TableData *d, *dd; |
| 120 | |
| 121 | - d = t->data[*a + t->sort_map[i]]; |
| 122 | - dd = t->data[*b + t->sort_map[i]]; |
| 123 | + d = user_table->data[*a + user_table->sort_map[i]]; |
| 124 | + dd = user_table->data[*b + user_table->sort_map[i]]; |
| 125 | |
| 126 | r = cell_data_compare(d, *a, dd, *b); |
| 127 | if (r != 0) |
| 128 | - return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r; |
| 129 | + return user_table->reverse_map && user_table->reverse_map[user_table->sort_map[i]] ? -r : r; |
| 130 | } |
| 131 | |
| 132 | /* Order identical lines by the order there were originally added in */ |
| 133 | @@ -1107,7 +1109,12 @@ int table_print(Table *t, FILE *f) { |
| 134 | for (i = 0; i < n_rows; i++) |
| 135 | sorted[i] = i * t->n_columns; |
| 136 | |
| 137 | - typesafe_qsort_r(sorted, n_rows, table_data_compare, t); |
| 138 | + if (n_rows <= 1) |
| 139 | + return 0; |
| 140 | + assert(sorted); |
| 141 | + user_table = t; |
| 142 | + qsort(sorted, n_rows, sizeof(size_t), table_data_compare); |
| 143 | + user_table = NULL; |
| 144 | } |
| 145 | |
| 146 | if (t->display_map) |
| 147 | @@ -1534,7 +1541,12 @@ int table_to_json(Table *t, JsonVariant **ret) { |
| 148 | for (i = 0; i < n_rows; i++) |
| 149 | sorted[i] = i * t->n_columns; |
| 150 | |
| 151 | - typesafe_qsort_r(sorted, n_rows, table_data_compare, t); |
| 152 | + if (n_rows <= 1) |
| 153 | + return 0; |
| 154 | + assert(sorted); |
| 155 | + user_table = t; |
| 156 | + qsort(sorted, n_rows, sizeof(size_t), table_data_compare); |
| 157 | + user_table = NULL; |
| 158 | } |
| 159 | |
| 160 | if (t->display_map) |
| 161 | -- |
| 162 | 2.11.0 |
| 163 | |