Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 1 | From d74a4de6daea5a511c2b5636bbb552c15b3a4ad9 Mon Sep 17 00:00:00 2001 |
| 2 | From: Emil Renner Berthing <systemd@esmil.dk> |
| 3 | Date: Thu, 18 Sep 2014 15:24:56 +0200 |
| 4 | Subject: [PATCH] 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 | --- |
| 10 | src/basic/format-table.c | 27 ++++++++++++++++----------- |
| 11 | src/basic/util.h | 7 ------- |
| 12 | src/hwdb/hwdb.c | 18 +++++++++++------- |
| 13 | src/udev/udevadm-hwdb.c | 16 ++++++++++------ |
| 14 | 4 files changed, 37 insertions(+), 31 deletions(-) |
| 15 | |
| 16 | diff --git a/src/basic/format-table.c b/src/basic/format-table.c |
| 17 | index 94e796d1ca..9b3f35c29a 100644 |
| 18 | --- a/src/basic/format-table.c |
| 19 | +++ b/src/basic/format-table.c |
| 20 | @@ -745,29 +745,29 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | -static int table_data_compare(const void *x, const void *y, void *userdata) { |
| 25 | +static Table *user_table; |
| 26 | +static int table_data_compare(const void *x, const void *y) { |
| 27 | const size_t *a = x, *b = y; |
| 28 | - Table *t = userdata; |
| 29 | size_t i; |
| 30 | int r; |
| 31 | |
| 32 | - assert(t); |
| 33 | - assert(t->sort_map); |
| 34 | + assert(user_table); |
| 35 | + assert(user_table->sort_map); |
| 36 | |
| 37 | /* Make sure the header stays at the beginning */ |
| 38 | - if (*a < t->n_columns && *b < t->n_columns) |
| 39 | + if (*a < user_table->n_columns && *b < user_table->n_columns) |
| 40 | return 0; |
| 41 | - if (*a < t->n_columns) |
| 42 | + if (*a < user_table->n_columns) |
| 43 | return -1; |
| 44 | - if (*b < t->n_columns) |
| 45 | + if (*b < user_table->n_columns) |
| 46 | return 1; |
| 47 | |
| 48 | /* Order other lines by the sorting map */ |
| 49 | - for (i = 0; i < t->n_sort_map; i++) { |
| 50 | + for (i = 0; i < user_table->n_sort_map; i++) { |
| 51 | TableData *d, *dd; |
| 52 | |
| 53 | - d = t->data[*a + t->sort_map[i]]; |
| 54 | - dd = t->data[*b + t->sort_map[i]]; |
| 55 | + d = user_table->data[*a + user_table->sort_map[i]]; |
| 56 | + dd = user_table->data[*b + user_table->sort_map[i]]; |
| 57 | |
| 58 | r = cell_data_compare(d, *a, dd, *b); |
| 59 | if (r != 0) |
| 60 | @@ -960,7 +960,12 @@ int table_print(Table *t, FILE *f) { |
| 61 | for (i = 0; i < n_rows; i++) |
| 62 | sorted[i] = i * t->n_columns; |
| 63 | |
| 64 | - qsort_r_safe(sorted, n_rows, sizeof(size_t), table_data_compare, t); |
| 65 | + if (n_rows <= 1) |
| 66 | + return 0; |
| 67 | + assert(sorted); |
| 68 | + user_table = t; |
| 69 | + qsort(sorted, n_rows, sizeof(size_t), table_data_compare); |
| 70 | + user_table = NULL; |
| 71 | } |
| 72 | |
| 73 | if (t->display_map) |
| 74 | diff --git a/src/basic/util.h b/src/basic/util.h |
| 75 | index 9699d228f9..40eaf518cb 100644 |
| 76 | --- a/src/basic/util.h |
| 77 | +++ b/src/basic/util.h |
| 78 | @@ -105,13 +105,6 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_ |
| 79 | qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \ |
| 80 | }) |
| 81 | |
| 82 | -static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void *userdata) { |
| 83 | - if (nmemb <= 1) |
| 84 | - return; |
| 85 | - |
| 86 | - assert(base); |
| 87 | - qsort_r(base, nmemb, size, compar, userdata); |
| 88 | -} |
| 89 | |
| 90 | /** |
| 91 | * Normal memcpy requires src to be nonnull. We do nothing if n is 0. |
| 92 | diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c |
| 93 | index 317cad8a67..701d59a1eb 100644 |
| 94 | --- a/src/hwdb/hwdb.c |
| 95 | +++ b/src/hwdb/hwdb.c |
| 96 | @@ -135,13 +135,12 @@ static void trie_free(struct trie *trie) { |
| 97 | |
| 98 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free); |
| 99 | |
| 100 | -static int trie_values_cmp(const void *v1, const void *v2, void *arg) { |
| 101 | +static struct trie *trie_node_add_value_trie; |
| 102 | +static int trie_values_cmp(const void *v1, const void *v2) { |
| 103 | const struct trie_value_entry *val1 = v1; |
| 104 | const struct trie_value_entry *val2 = v2; |
| 105 | - struct trie *trie = arg; |
| 106 | - |
| 107 | - return strcmp(trie->strings->buf + val1->key_off, |
| 108 | - trie->strings->buf + val2->key_off); |
| 109 | + return strcmp(trie_node_add_value_trie->strings->buf + val1->key_off, |
| 110 | + trie_node_add_value_trie->strings->buf + val2->key_off); |
| 111 | } |
| 112 | |
| 113 | static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 114 | @@ -166,7 +165,10 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 115 | .value_off = v, |
| 116 | }; |
| 117 | |
| 118 | - val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); |
| 119 | + trie_node_add_value_trie = trie; |
| 120 | + val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp); |
| 121 | + trie_node_add_value_trie = NULL; |
| 122 | + |
| 123 | if (val) { |
| 124 | /* At this point we have 2 identical properties on the same match-string. |
| 125 | * Since we process files in order, we just replace the previous value. |
| 126 | @@ -191,7 +193,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 127 | node->values[node->values_count].file_priority = file_priority; |
| 128 | node->values[node->values_count].line_number = line_number; |
| 129 | node->values_count++; |
| 130 | - qsort_r(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); |
| 131 | + trie_node_add_value_trie = trie; |
| 132 | + qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp); |
| 133 | + trie_node_add_value_trie = NULL; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c |
| 138 | index 02408a4285..491d367d12 100644 |
| 139 | --- a/src/udev/udevadm-hwdb.c |
| 140 | +++ b/src/udev/udevadm-hwdb.c |
| 141 | @@ -114,13 +114,13 @@ static void trie_node_cleanup(struct trie_node *node) { |
| 142 | free(node); |
| 143 | } |
| 144 | |
| 145 | -static int trie_values_cmp(const void *v1, const void *v2, void *arg) { |
| 146 | +static struct trie *trie_node_add_value_trie; |
| 147 | +static int trie_values_cmp(const void *v1, const void *v2) { |
| 148 | const struct trie_value_entry *val1 = v1; |
| 149 | const struct trie_value_entry *val2 = v2; |
| 150 | - struct trie *trie = arg; |
| 151 | |
| 152 | - return strcmp(trie->strings->buf + val1->key_off, |
| 153 | - trie->strings->buf + val2->key_off); |
| 154 | + return strcmp(trie_node_add_value_trie->strings->buf + val1->key_off, |
| 155 | + trie_node_add_value_trie->strings->buf + val2->key_off); |
| 156 | } |
| 157 | |
| 158 | static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 159 | @@ -141,7 +141,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 160 | .value_off = v, |
| 161 | }; |
| 162 | |
| 163 | - val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); |
| 164 | + trie_node_add_value_trie = trie; |
| 165 | + val = bsearch(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp); |
| 166 | + trie_node_add_value_trie = NULL; |
| 167 | if (val) { |
| 168 | /* replace existing earlier key with new value */ |
| 169 | val->value_off = v; |
| 170 | @@ -158,7 +160,9 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, |
| 171 | node->values[node->values_count].key_off = k; |
| 172 | node->values[node->values_count].value_off = v; |
| 173 | node->values_count++; |
| 174 | - qsort_r(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); |
| 175 | + trie_node_add_value_trie = trie; |
| 176 | + qsort(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp); |
| 177 | + trie_node_add_value_trie = NULL; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | -- |
| 182 | 2.18.0 |
| 183 | |