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