| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/lhash.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "../internal.h" |
| 24 | #include "internal.h" |
| 25 |
| 26 |
| 27 | // kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|. |
| 28 | static const size_t kMinNumBuckets = 16; |
| 29 |
| 30 | // kMaxAverageChainLength contains the maximum, average chain length. When the |
| 31 | // average chain length exceeds this value, the hash table will be resized. |
| 32 | static const size_t kMaxAverageChainLength = 2; |
| 33 | static const size_t kMinAverageChainLength = 1; |
| 34 |
| 35 | // lhash_item_st is an element of a hash chain. It points to the opaque data |
| 36 | // for this element and to the next item in the chain. The linked-list is NULL |
| 37 | // terminated. |
| 38 | typedef struct lhash_item_st { |
| 39 | void *data; |
| 40 | struct lhash_item_st *next; |
| 41 | // hash contains the cached, hash value of |data|. |
| 42 | uint32_t hash; |
| 43 | } LHASH_ITEM; |
| 44 |
| 45 | struct lhash_st { |
| 46 | // num_items contains the total number of items in the hash table. |
| 47 | size_t num_items; |
| 48 | // buckets is an array of |num_buckets| pointers. Each points to the head of |
| 49 | // a chain of LHASH_ITEM objects that have the same hash value, mod |
| 50 | // |num_buckets|. |
| 51 | LHASH_ITEM **buckets; |
| 52 | // num_buckets contains the length of |buckets|. This value is always >= |
| 53 | // kMinNumBuckets. |
| 54 | size_t num_buckets; |
| 55 | // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg| |
| 56 | // calls. If non-zero then this suppresses resizing of the |buckets| array, |
| 57 | // which would otherwise disrupt the iteration. |
| 58 | unsigned callback_depth; |
| 59 |
| 60 | lhash_cmp_func comp; |
| 61 | lhash_hash_func hash; |
| 62 | }; |
| 63 |
| 64 | _LHASH *OPENSSL_lh_new(lhash_hash_func hash, lhash_cmp_func comp) { |
| 65 | _LHASH *ret = reinterpret_cast<_LHASH *>(OPENSSL_zalloc(sizeof(_LHASH))); |
| 66 | if (ret == NULL) { |
| 67 | return NULL; |
| 68 | } |
| 69 |
| 70 | ret->num_buckets = kMinNumBuckets; |
| 71 | ret->buckets = reinterpret_cast<LHASH_ITEM **>( |
| 72 | OPENSSL_calloc(ret->num_buckets, sizeof(LHASH_ITEM *))); |
| 73 | if (ret->buckets == NULL) { |
| 74 | OPENSSL_free(ret); |
| 75 | return NULL; |
| 76 | } |
| 77 |
| 78 | ret->comp = comp; |
| 79 | ret->hash = hash; |
| 80 | return ret; |
| 81 | } |
| 82 |
| 83 | void OPENSSL_lh_free(_LHASH *lh) { |
| 84 | if (lh == NULL) { |
| 85 | return; |
| 86 | } |
| 87 |
| 88 | for (size_t i = 0; i < lh->num_buckets; i++) { |
| 89 | LHASH_ITEM *next; |
| 90 | for (LHASH_ITEM *n = lh->buckets[i]; n != NULL; n = next) { |
| 91 | next = n->next; |
| 92 | OPENSSL_free(n); |
| 93 | } |
| 94 | } |
| 95 |
| 96 | OPENSSL_free(lh->buckets); |
| 97 | OPENSSL_free(lh); |
| 98 | } |
| 99 |
| 100 | size_t OPENSSL_lh_num_items(const _LHASH *lh) { return lh->num_items; } |
| 101 |
| 102 | // get_next_ptr_and_hash returns a pointer to the pointer that points to the |
| 103 | // item equal to |data|. In other words, it searches for an item equal to |data| |
| 104 | // and, if it's at the start of a chain, then it returns a pointer to an |
| 105 | // element of |lh->buckets|, otherwise it returns a pointer to the |next| |
| 106 | // element of the previous item in the chain. If an element equal to |data| is |
| 107 | // not found, it returns a pointer that points to a NULL pointer. If |out_hash| |
| 108 | // is not NULL, then it also puts the hash value of |data| in |*out_hash|. |
| 109 | static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash, |
| 110 | const void *data, |
| 111 | lhash_hash_func_helper call_hash_func, |
| 112 | lhash_cmp_func_helper call_cmp_func) { |
| 113 | const uint32_t hash = call_hash_func(lh->hash, data); |
| 114 | if (out_hash != NULL) { |
| 115 | *out_hash = hash; |
| 116 | } |
| 117 |
| 118 | LHASH_ITEM **ret = &lh->buckets[hash % lh->num_buckets]; |
| 119 | for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) { |
| 120 | if (call_cmp_func(lh->comp, cur->data, data) == 0) { |
| 121 | break; |
| 122 | } |
| 123 | ret = &cur->next; |
| 124 | } |
| 125 |
| 126 | return ret; |
| 127 | } |
| 128 |
| 129 | // get_next_ptr_by_key behaves like |get_next_ptr_and_hash| but takes a key |
| 130 | // which may be a different type from the values stored in |lh|. |
| 131 | static LHASH_ITEM **get_next_ptr_by_key(const _LHASH *lh, const void *key, |
| 132 | uint32_t key_hash, |
| 133 | int (*cmp_key)(const void *key, |
| 134 | const void *value)) { |
| 135 | LHASH_ITEM **ret = &lh->buckets[key_hash % lh->num_buckets]; |
| 136 | for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) { |
| 137 | if (cmp_key(key, cur->data) == 0) { |
| 138 | break; |
| 139 | } |
| 140 | ret = &cur->next; |
| 141 | } |
| 142 |
| 143 | return ret; |
| 144 | } |
| 145 |
| 146 | void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data, |
| 147 | lhash_hash_func_helper call_hash_func, |
| 148 | lhash_cmp_func_helper call_cmp_func) { |
| 149 | LHASH_ITEM **next_ptr = |
| 150 | get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func); |
| 151 | return *next_ptr == NULL ? NULL : (*next_ptr)->data; |
| 152 | } |
| 153 |
| 154 | void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key, |
| 155 | uint32_t key_hash, |
| 156 | int (*cmp_key)(const void *key, |
| 157 | const void *value)) { |
| 158 | LHASH_ITEM **next_ptr = get_next_ptr_by_key(lh, key, key_hash, cmp_key); |
| 159 | return *next_ptr == NULL ? NULL : (*next_ptr)->data; |
| 160 | } |
| 161 |
| 162 | // lh_rebucket allocates a new array of |new_num_buckets| pointers and |
| 163 | // redistributes the existing items into it before making it |lh->buckets| and |
| 164 | // freeing the old array. |
| 165 | static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) { |
| 166 | LHASH_ITEM **new_buckets, *cur, *next; |
| 167 | size_t i, alloc_size; |
| 168 |
| 169 | alloc_size = sizeof(LHASH_ITEM *) * new_num_buckets; |
| 170 | if (alloc_size / sizeof(LHASH_ITEM *) != new_num_buckets) { |
| 171 | return; |
| 172 | } |
| 173 |
| 174 | new_buckets = reinterpret_cast<LHASH_ITEM **>(OPENSSL_zalloc(alloc_size)); |
| 175 | if (new_buckets == NULL) { |
| 176 | return; |
| 177 | } |
| 178 |
| 179 | for (i = 0; i < lh->num_buckets; i++) { |
| 180 | for (cur = lh->buckets[i]; cur != NULL; cur = next) { |
| 181 | const size_t new_bucket = cur->hash % new_num_buckets; |
| 182 | next = cur->next; |
| 183 | cur->next = new_buckets[new_bucket]; |
| 184 | new_buckets[new_bucket] = cur; |
| 185 | } |
| 186 | } |
| 187 |
| 188 | OPENSSL_free(lh->buckets); |
| 189 |
| 190 | lh->num_buckets = new_num_buckets; |
| 191 | lh->buckets = new_buckets; |
| 192 | } |
| 193 |
| 194 | // lh_maybe_resize resizes the |buckets| array if needed. |
| 195 | static void lh_maybe_resize(_LHASH *lh) { |
| 196 | size_t avg_chain_length; |
| 197 |
| 198 | if (lh->callback_depth > 0) { |
| 199 | // Don't resize the hash if we are currently iterating over it. |
| 200 | return; |
| 201 | } |
| 202 |
| 203 | assert(lh->num_buckets >= kMinNumBuckets); |
| 204 | avg_chain_length = lh->num_items / lh->num_buckets; |
| 205 |
| 206 | if (avg_chain_length > kMaxAverageChainLength) { |
| 207 | const size_t new_num_buckets = lh->num_buckets * 2; |
| 208 |
| 209 | if (new_num_buckets > lh->num_buckets) { |
| 210 | lh_rebucket(lh, new_num_buckets); |
| 211 | } |
| 212 | } else if (avg_chain_length < kMinAverageChainLength && |
| 213 | lh->num_buckets > kMinNumBuckets) { |
| 214 | size_t new_num_buckets = lh->num_buckets / 2; |
| 215 |
| 216 | if (new_num_buckets < kMinNumBuckets) { |
| 217 | new_num_buckets = kMinNumBuckets; |
| 218 | } |
| 219 |
| 220 | lh_rebucket(lh, new_num_buckets); |
| 221 | } |
| 222 | } |
| 223 |
| 224 | int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data, |
| 225 | lhash_hash_func_helper call_hash_func, |
| 226 | lhash_cmp_func_helper call_cmp_func) { |
| 227 | uint32_t hash; |
| 228 | LHASH_ITEM **next_ptr, *item; |
| 229 |
| 230 | *old_data = NULL; |
| 231 | next_ptr = |
| 232 | get_next_ptr_and_hash(lh, &hash, data, call_hash_func, call_cmp_func); |
| 233 |
| 234 |
| 235 | if (*next_ptr != NULL) { |
| 236 | // An element equal to |data| already exists in the hash table. It will be |
| 237 | // replaced. |
| 238 | *old_data = (*next_ptr)->data; |
| 239 | (*next_ptr)->data = data; |
| 240 | return 1; |
| 241 | } |
| 242 |
| 243 | // An element equal to |data| doesn't exist in the hash table yet. |
| 244 | item = reinterpret_cast<LHASH_ITEM *>(OPENSSL_malloc(sizeof(LHASH_ITEM))); |
| 245 | if (item == NULL) { |
| 246 | return 0; |
| 247 | } |
| 248 |
| 249 | item->data = data; |
| 250 | item->hash = hash; |
| 251 | item->next = NULL; |
| 252 | *next_ptr = item; |
| 253 | lh->num_items++; |
| 254 | lh_maybe_resize(lh); |
| 255 |
| 256 | return 1; |
| 257 | } |
| 258 |
| 259 | void *OPENSSL_lh_delete(_LHASH *lh, const void *data, |
| 260 | lhash_hash_func_helper call_hash_func, |
| 261 | lhash_cmp_func_helper call_cmp_func) { |
| 262 | LHASH_ITEM **next_ptr, *item, *ret; |
| 263 |
| 264 | next_ptr = |
| 265 | get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func); |
| 266 |
| 267 | if (*next_ptr == NULL) { |
| 268 | // No such element. |
| 269 | return NULL; |
| 270 | } |
| 271 |
| 272 | item = *next_ptr; |
| 273 | *next_ptr = item->next; |
| 274 | ret = reinterpret_cast<LHASH_ITEM *>(item->data); |
| 275 | OPENSSL_free(item); |
| 276 |
| 277 | lh->num_items--; |
| 278 | lh_maybe_resize(lh); |
| 279 |
| 280 | return ret; |
| 281 | } |
| 282 |
| 283 | void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) { |
| 284 | if (lh == NULL) { |
| 285 | return; |
| 286 | } |
| 287 |
| 288 | if (lh->callback_depth < UINT_MAX) { |
| 289 | // |callback_depth| is a saturating counter. |
| 290 | lh->callback_depth++; |
| 291 | } |
| 292 |
| 293 | for (size_t i = 0; i < lh->num_buckets; i++) { |
| 294 | LHASH_ITEM *next; |
| 295 | for (LHASH_ITEM *cur = lh->buckets[i]; cur != NULL; cur = next) { |
| 296 | next = cur->next; |
| 297 | func(cur->data, arg); |
| 298 | } |
| 299 | } |
| 300 |
| 301 | if (lh->callback_depth < UINT_MAX) { |
| 302 | lh->callback_depth--; |
| 303 | } |
| 304 |
| 305 | // The callback may have added or removed elements and the non-zero value of |
| 306 | // |callback_depth| will have suppressed any resizing. Thus any needed |
| 307 | // resizing is done here. |
| 308 | lh_maybe_resize(lh); |
| 309 | } |
| 310 |