Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [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 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 5 | |
| 6 | Upstream-Status: Inappropriate [musl specific] |
| 7 | |
| 8 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 9 | [Rebased for v241] |
| 10 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | --- |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 12 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 16 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 17 | diff --git a/src/basic/util.h b/src/basic/util.h |
| 18 | index 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) |
| 42 | diff --git a/src/libsystemd/sd-hwdb/hwdb-util.c b/src/libsystemd/sd-hwdb/hwdb-util.c |
| 43 | index 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 86 | diff --git a/src/shared/format-table.c b/src/shared/format-table.c |
| 87 | index 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 95 | +static Table *user_table; |
| 96 | +static int table_data_compare(const void *x, const void *y) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 97 | + const size_t *a = x, *b=y; |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 98 | 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 Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 129 | - 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 135 | for (i = 0; i < n_rows; i++) |
| 136 | sorted[i] = i * t->n_columns; |
| 137 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 138 | - typesafe_qsort_r(sorted, n_rows, table_data_compare, t); |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 139 | + 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 Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 148 | @@ -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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 151 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 152 | - 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 160 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 161 | if (t->display_map) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 162 | -- |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame^] | 163 | 2.7.4 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 164 | |