lhash.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/lhash.h>
58 
59 #include <assert.h>
60 #include <limits.h>
61 #include <string.h>
62 
63 #include <openssl/mem.h>
64 
65 #include "internal.h"
66 #include "../internal.h"
67 
68 
69 // kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|.
70 static const size_t kMinNumBuckets = 16;
71 
72 // kMaxAverageChainLength contains the maximum, average chain length. When the
73 // average chain length exceeds this value, the hash table will be resized.
74 static const size_t kMaxAverageChainLength = 2;
75 static const size_t kMinAverageChainLength = 1;
76 
77 // lhash_item_st is an element of a hash chain. It points to the opaque data
78 // for this element and to the next item in the chain. The linked-list is NULL
79 // terminated.
80 typedef struct lhash_item_st {
81  void *data;
83  // hash contains the cached, hash value of |data|.
85 } LHASH_ITEM;
86 
87 struct lhash_st {
88  // num_items contains the total number of items in the hash table.
89  size_t num_items;
90  // buckets is an array of |num_buckets| pointers. Each points to the head of
91  // a chain of LHASH_ITEM objects that have the same hash value, mod
92  // |num_buckets|.
94  // num_buckets contains the length of |buckets|. This value is always >=
95  // kMinNumBuckets.
96  size_t num_buckets;
97  // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
98  // calls. If non-zero then this suppresses resizing of the |buckets| array,
99  // which would otherwise disrupt the iteration.
100  unsigned callback_depth;
101 
104 };
105 
107  _LHASH *ret = OPENSSL_malloc(sizeof(_LHASH));
108  if (ret == NULL) {
109  return NULL;
110  }
111  OPENSSL_memset(ret, 0, sizeof(_LHASH));
112 
113  ret->num_buckets = kMinNumBuckets;
114  ret->buckets = OPENSSL_malloc(sizeof(LHASH_ITEM *) * ret->num_buckets);
115  if (ret->buckets == NULL) {
116  OPENSSL_free(ret);
117  return NULL;
118  }
119  OPENSSL_memset(ret->buckets, 0, sizeof(LHASH_ITEM *) * ret->num_buckets);
120 
121  ret->comp = comp;
122  ret->hash = hash;
123  return ret;
124 }
125 
127  if (lh == NULL) {
128  return;
129  }
130 
131  for (size_t i = 0; i < lh->num_buckets; i++) {
132  LHASH_ITEM *next;
133  for (LHASH_ITEM *n = lh->buckets[i]; n != NULL; n = next) {
134  next = n->next;
135  OPENSSL_free(n);
136  }
137  }
138 
139  OPENSSL_free(lh->buckets);
140  OPENSSL_free(lh);
141 }
142 
143 size_t OPENSSL_lh_num_items(const _LHASH *lh) { return lh->num_items; }
144 
145 // get_next_ptr_and_hash returns a pointer to the pointer that points to the
146 // item equal to |data|. In other words, it searches for an item equal to |data|
147 // and, if it's at the start of a chain, then it returns a pointer to an
148 // element of |lh->buckets|, otherwise it returns a pointer to the |next|
149 // element of the previous item in the chain. If an element equal to |data| is
150 // not found, it returns a pointer that points to a NULL pointer. If |out_hash|
151 // is not NULL, then it also puts the hash value of |data| in |*out_hash|.
152 static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash,
153  const void *data,
154  lhash_hash_func_helper call_hash_func,
155  lhash_cmp_func_helper call_cmp_func) {
156  const uint32_t hash = call_hash_func(lh->hash, data);
157  if (out_hash != NULL) {
158  *out_hash = hash;
159  }
160 
161  LHASH_ITEM **ret = &lh->buckets[hash % lh->num_buckets];
162  for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
163  if (call_cmp_func(lh->comp, cur->data, data) == 0) {
164  break;
165  }
166  ret = &cur->next;
167  }
168 
169  return ret;
170 }
171 
172 // get_next_ptr_by_key behaves like |get_next_ptr_and_hash| but takes a key
173 // which may be a different type from the values stored in |lh|.
174 static LHASH_ITEM **get_next_ptr_by_key(const _LHASH *lh, const void *key,
175  uint32_t key_hash,
176  int (*cmp_key)(const void *key,
177  const void *value)) {
178  LHASH_ITEM **ret = &lh->buckets[key_hash % lh->num_buckets];
179  for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
180  if (cmp_key(key, cur->data) == 0) {
181  break;
182  }
183  ret = &cur->next;
184  }
185 
186  return ret;
187 }
188 
189 void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data,
190  lhash_hash_func_helper call_hash_func,
191  lhash_cmp_func_helper call_cmp_func) {
192  LHASH_ITEM **next_ptr =
193  get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);
194  return *next_ptr == NULL ? NULL : (*next_ptr)->data;
195 }
196 
197 void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key,
198  uint32_t key_hash,
199  int (*cmp_key)(const void *key,
200  const void *value)) {
201  LHASH_ITEM **next_ptr = get_next_ptr_by_key(lh, key, key_hash, cmp_key);
202  return *next_ptr == NULL ? NULL : (*next_ptr)->data;
203 }
204 
205 // lh_rebucket allocates a new array of |new_num_buckets| pointers and
206 // redistributes the existing items into it before making it |lh->buckets| and
207 // freeing the old array.
208 static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) {
209  LHASH_ITEM **new_buckets, *cur, *next;
210  size_t i, alloc_size;
211 
212  alloc_size = sizeof(LHASH_ITEM *) * new_num_buckets;
213  if (alloc_size / sizeof(LHASH_ITEM*) != new_num_buckets) {
214  return;
215  }
216 
217  new_buckets = OPENSSL_malloc(alloc_size);
218  if (new_buckets == NULL) {
219  return;
220  }
221  OPENSSL_memset(new_buckets, 0, alloc_size);
222 
223  for (i = 0; i < lh->num_buckets; i++) {
224  for (cur = lh->buckets[i]; cur != NULL; cur = next) {
225  const size_t new_bucket = cur->hash % new_num_buckets;
226  next = cur->next;
227  cur->next = new_buckets[new_bucket];
228  new_buckets[new_bucket] = cur;
229  }
230  }
231 
232  OPENSSL_free(lh->buckets);
233 
234  lh->num_buckets = new_num_buckets;
235  lh->buckets = new_buckets;
236 }
237 
238 // lh_maybe_resize resizes the |buckets| array if needed.
239 static void lh_maybe_resize(_LHASH *lh) {
240  size_t avg_chain_length;
241 
242  if (lh->callback_depth > 0) {
243  // Don't resize the hash if we are currently iterating over it.
244  return;
245  }
246 
247  assert(lh->num_buckets >= kMinNumBuckets);
248  avg_chain_length = lh->num_items / lh->num_buckets;
249 
250  if (avg_chain_length > kMaxAverageChainLength) {
251  const size_t new_num_buckets = lh->num_buckets * 2;
252 
253  if (new_num_buckets > lh->num_buckets) {
254  lh_rebucket(lh, new_num_buckets);
255  }
256  } else if (avg_chain_length < kMinAverageChainLength &&
257  lh->num_buckets > kMinNumBuckets) {
258  size_t new_num_buckets = lh->num_buckets / 2;
259 
260  if (new_num_buckets < kMinNumBuckets) {
261  new_num_buckets = kMinNumBuckets;
262  }
263 
264  lh_rebucket(lh, new_num_buckets);
265  }
266 }
267 
268 int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data,
269  lhash_hash_func_helper call_hash_func,
270  lhash_cmp_func_helper call_cmp_func) {
271  uint32_t hash;
272  LHASH_ITEM **next_ptr, *item;
273 
274  *old_data = NULL;
275  next_ptr =
276  get_next_ptr_and_hash(lh, &hash, data, call_hash_func, call_cmp_func);
277 
278 
279  if (*next_ptr != NULL) {
280  // An element equal to |data| already exists in the hash table. It will be
281  // replaced.
282  *old_data = (*next_ptr)->data;
283  (*next_ptr)->data = data;
284  return 1;
285  }
286 
287  // An element equal to |data| doesn't exist in the hash table yet.
288  item = OPENSSL_malloc(sizeof(LHASH_ITEM));
289  if (item == NULL) {
290  return 0;
291  }
292 
293  item->data = data;
294  item->hash = hash;
295  item->next = NULL;
296  *next_ptr = item;
297  lh->num_items++;
298  lh_maybe_resize(lh);
299 
300  return 1;
301 }
302 
303 void *OPENSSL_lh_delete(_LHASH *lh, const void *data,
304  lhash_hash_func_helper call_hash_func,
305  lhash_cmp_func_helper call_cmp_func) {
306  LHASH_ITEM **next_ptr, *item, *ret;
307 
308  next_ptr =
309  get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);
310 
311  if (*next_ptr == NULL) {
312  // No such element.
313  return NULL;
314  }
315 
316  item = *next_ptr;
317  *next_ptr = item->next;
318  ret = item->data;
319  OPENSSL_free(item);
320 
321  lh->num_items--;
322  lh_maybe_resize(lh);
323 
324  return ret;
325 }
326 
327 void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) {
328  if (lh == NULL) {
329  return;
330  }
331 
332  if (lh->callback_depth < UINT_MAX) {
333  // |callback_depth| is a saturating counter.
334  lh->callback_depth++;
335  }
336 
337  for (size_t i = 0; i < lh->num_buckets; i++) {
338  LHASH_ITEM *next;
339  for (LHASH_ITEM *cur = lh->buckets[i]; cur != NULL; cur = next) {
340  next = cur->next;
341  func(cur->data, arg);
342  }
343  }
344 
345  if (lh->callback_depth < UINT_MAX) {
346  lh->callback_depth--;
347  }
348 
349  // The callback may have added or removed elements and the non-zero value of
350  // |callback_depth| will have suppressed any resizing. Thus any needed
351  // resizing is done here.
352  lh_maybe_resize(lh);
353 }
OPENSSL_lh_retrieve_key
void * OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key, uint32_t key_hash, int(*cmp_key)(const void *key, const void *value))
Definition: lhash.c:197
get_next_ptr_and_hash
static LHASH_ITEM ** get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash, const void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func)
Definition: lhash.c:152
OPENSSL_lh_retrieve
void * OPENSSL_lh_retrieve(const _LHASH *lh, const void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func)
Definition: lhash.c:189
lhash_item_st::next
struct lhash_item_st * next
Definition: lhash.c:82
internal.h
string.h
OPENSSL_lh_free
void OPENSSL_lh_free(_LHASH *lh)
Definition: lhash.c:126
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
OPENSSL_lh_new
_LHASH * OPENSSL_lh_new(lhash_hash_func hash, lhash_cmp_func comp)
Definition: lhash.c:106
hash
uint64_t hash
Definition: ring_hash.cc:284
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
OPENSSL_lh_insert
int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func)
Definition: lhash.c:268
lhash_cmp_func_helper
int(* lhash_cmp_func_helper)(lhash_cmp_func func, const void *a, const void *b)
Definition: third_party/boringssl-with-bazel/src/crypto/lhash/internal.h:96
kMinAverageChainLength
static const size_t kMinAverageChainLength
Definition: lhash.c:75
get_next_ptr_by_key
static LHASH_ITEM ** get_next_ptr_by_key(const _LHASH *lh, const void *key, uint32_t key_hash, int(*cmp_key)(const void *key, const void *value))
Definition: lhash.c:174
lhash_st::num_buckets
size_t num_buckets
Definition: lhash.c:96
kMaxAverageChainLength
static const size_t kMaxAverageChainLength
Definition: lhash.c:74
lhash_hash_func
uint32_t(* lhash_hash_func)(const void *a)
Definition: third_party/boringssl-with-bazel/src/crypto/lhash/internal.h:105
kMinNumBuckets
static const size_t kMinNumBuckets
Definition: lhash.c:70
lhash_st::callback_depth
unsigned callback_depth
Definition: lhash.c:100
lhash_item_st::data
void * data
Definition: lhash.c:81
arg
Definition: cmdline.cc:40
lhash.h
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
lhash_st
Definition: lhash.c:87
lhash_st::buckets
LHASH_ITEM ** buckets
Definition: lhash.c:93
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
value
const char * value
Definition: hpack_parser_table.cc:165
memory_diff.cur
def cur
Definition: memory_diff.py:83
OPENSSL_lh_doall_arg
void OPENSSL_lh_doall_arg(_LHASH *lh, void(*func)(void *, void *), void *arg)
Definition: lhash.c:327
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
key
const char * key
Definition: hpack_parser_table.cc:164
lhash_st::hash
lhash_hash_func hash
Definition: lhash.c:103
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
OPENSSL_lh_num_items
size_t OPENSSL_lh_num_items(const _LHASH *lh)
Definition: lhash.c:143
lhash_st::comp
lhash_cmp_func comp
Definition: lhash.c:102
lhash_item_st
Definition: lhash.c:80
lhash_st::num_items
size_t num_items
Definition: lhash.c:89
lh_rebucket
static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets)
Definition: lhash.c:208
lhash_cmp_func
int(* lhash_cmp_func)(const void *a, const void *b)
Definition: third_party/boringssl-with-bazel/src/crypto/lhash/internal.h:95
lhash_hash_func_helper
uint32_t(* lhash_hash_func_helper)(lhash_hash_func func, const void *a)
Definition: third_party/boringssl-with-bazel/src/crypto/lhash/internal.h:106
mem.h
OPENSSL_lh_delete
void * OPENSSL_lh_delete(_LHASH *lh, const void *data, lhash_hash_func_helper call_hash_func, lhash_cmp_func_helper call_cmp_func)
Definition: lhash.c:303
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
lhash_item_st::hash
uint32_t hash
Definition: lhash.c:84
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
LHASH_ITEM
struct lhash_item_st LHASH_ITEM
lh_maybe_resize
static void lh_maybe_resize(_LHASH *lh)
Definition: lhash.c:239


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:28