/src/openssl/include/internal/hashtable.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #ifndef OPENSSL_HASHTABLE_H |
11 | | #define OPENSSL_HASHTABLE_H |
12 | | #pragma once |
13 | | |
14 | | #include <stddef.h> |
15 | | #include <stdint.h> |
16 | | #include <openssl/e_os2.h> |
17 | | #include <internal/rcu.h> |
18 | | #include "crypto/context.h" |
19 | | |
20 | | typedef struct ht_internal_st HT; |
21 | | |
22 | | /* |
23 | | * Represents a key to a hashtable |
24 | | */ |
25 | | typedef struct ht_key_header_st { |
26 | | uint64_t cached_hash; |
27 | | size_t keysize; |
28 | | size_t bufsize; |
29 | | uint8_t *keybuf; |
30 | | } HT_KEY; |
31 | | |
32 | | /* |
33 | | * Represents a value in the hash table |
34 | | */ |
35 | | typedef struct ht_value_st { |
36 | | void *value; |
37 | | uintptr_t *type_id; |
38 | | HT_KEY key; |
39 | | } HT_VALUE; |
40 | | |
41 | | /* |
42 | | * Represents a list of values filtered from a hash table |
43 | | */ |
44 | | typedef struct ht_value_list_st { |
45 | | size_t list_len; |
46 | | HT_VALUE **list; |
47 | | } HT_VALUE_LIST; |
48 | | |
49 | | /* |
50 | | * Hashtable configuration |
51 | | */ |
52 | | typedef struct ht_config_st { |
53 | | OSSL_LIB_CTX *ctx; |
54 | | void (*ht_free_fn)(HT_VALUE *obj); |
55 | | uint64_t (*ht_hash_fn)(HT_KEY *key); |
56 | | size_t init_neighborhoods; |
57 | | uint32_t collision_check; |
58 | | uint32_t lockless_reads; |
59 | | uint32_t no_rcu; |
60 | | } HT_CONFIG; |
61 | | |
62 | | /* |
63 | | * Hashtable key rules |
64 | | * Any struct can be used to formulate a hash table key, as long as the |
65 | | * following rules |
66 | | * 1) The first element of the struct defining the key must be an HT_KEY |
67 | | * 2) All struct elements must have a compile time defined length |
68 | | * 3) Pointers can be used, but the value of the pointer, rather than |
69 | | * the contents of the address it points to will be used to compute |
70 | | * the hash |
71 | | * The key definition macros will assist with enforcing these rules |
72 | | */ |
73 | | |
74 | | /* |
75 | | * Starts the definition of a hash table key |
76 | | */ |
77 | | #define HT_START_KEY_DEFN(keyname) \ |
78 | | typedef struct keyname##_st { \ |
79 | | HT_KEY key_header; \ |
80 | | struct { |
81 | | |
82 | | /* |
83 | | * Ends a hash table key definitions |
84 | | */ |
85 | | #define HT_END_KEY_DEFN(keyname) \ |
86 | | } \ |
87 | | keyfields; \ |
88 | | } \ |
89 | | keyname; |
90 | | |
91 | | /* |
92 | | * Defines a field in a hash table key |
93 | | */ |
94 | | #define HT_DEF_KEY_FIELD(name, type) type name; |
95 | | |
96 | | /* |
97 | | * convenience macro to define a static char |
98 | | * array field in a hash table key |
99 | | */ |
100 | | #define HT_DEF_KEY_FIELD_CHAR_ARRAY(name, size) \ |
101 | | HT_DEF_KEY_FIELD(name[size], char) |
102 | | |
103 | | /* |
104 | | * Defines a uint8_t (blob) field in a hash table key |
105 | | */ |
106 | | #define HT_DEF_KEY_FIELD_UINT8T_ARRAY(name, size) \ |
107 | | HT_DEF_KEY_FIELD(name[size], uint8_t) |
108 | | |
109 | | /* |
110 | | * Initializes a key |
111 | | */ |
112 | | #define HT_INIT_KEY(key) \ |
113 | 194k | do { \ |
114 | 194k | memset((key), 0, sizeof(*(key))); \ |
115 | 194k | (key)->key_header.keysize = (key)->key_header.bufsize = (sizeof(*(key)) - sizeof(HT_KEY)); \ |
116 | 194k | (key)->key_header.keybuf = (((uint8_t *)key) + sizeof(HT_KEY)); \ |
117 | 194k | } while (0) |
118 | | |
119 | | /* |
120 | | * Initializes a key as a raw buffer |
121 | | * This operates identically to HT_INIT_KEY |
122 | | * but it treats the provided key as a raw buffer |
123 | | * and iteratively accounts the running amount of |
124 | | * data copied into the key from the caller. |
125 | | * |
126 | | * This MUST be used with the RAW macros below: |
127 | | * HT_COPY_RAW_KEY |
128 | | * HT_COPY_RAW_KEY_CASE |
129 | | */ |
130 | | #define HT_INIT_RAW_KEY(key) \ |
131 | 112k | do { \ |
132 | 112k | HT_INIT_KEY((key)); \ |
133 | 112k | (key)->key_header.keysize = 0; \ |
134 | 112k | } while (0) |
135 | | |
136 | | /* |
137 | | * Helper function to copy raw data into a key |
138 | | * This should not be called independently |
139 | | * use the HT_COPY_RAW_KEY macro instead |
140 | | */ |
141 | | static ossl_inline ossl_unused int ossl_key_raw_copy(HT_KEY *key, const uint8_t *buf, size_t len) |
142 | 0 | { |
143 | 0 | if (key->keysize + len > key->bufsize) |
144 | 0 | return 0; |
145 | 0 | memcpy(&key->keybuf[key->keysize], buf, len); |
146 | 0 | key->keysize += len; |
147 | 0 | return 1; |
148 | 0 | } Unexecuted instantiation: core_namemap.c:ossl_key_raw_copy Unexecuted instantiation: property.c:ossl_key_raw_copy Unexecuted instantiation: by_dir.c:ossl_key_raw_copy Unexecuted instantiation: by_file.c:ossl_key_raw_copy Unexecuted instantiation: by_store.c:ossl_key_raw_copy Unexecuted instantiation: v3_lib.c:ossl_key_raw_copy Unexecuted instantiation: v3_purp.c:ossl_key_raw_copy Unexecuted instantiation: v3_tlsf.c:ossl_key_raw_copy Unexecuted instantiation: v3_utl.c:ossl_key_raw_copy Unexecuted instantiation: x509_att.c:ossl_key_raw_copy Unexecuted instantiation: x509_lu.c:ossl_key_raw_copy Unexecuted instantiation: x509_set.c:ossl_key_raw_copy Unexecuted instantiation: x509_v3.c:ossl_key_raw_copy Unexecuted instantiation: x509_vfy.c:ossl_key_raw_copy Unexecuted instantiation: x509_vpm.c:ossl_key_raw_copy Unexecuted instantiation: x_all.c:ossl_key_raw_copy Unexecuted instantiation: x_attrib.c:ossl_key_raw_copy Unexecuted instantiation: x_crl.c:ossl_key_raw_copy Unexecuted instantiation: x_exten.c:ossl_key_raw_copy Unexecuted instantiation: x_name.c:ossl_key_raw_copy Unexecuted instantiation: hashtable.c:ossl_key_raw_copy Unexecuted instantiation: v3_ac_tgt.c:ossl_key_raw_copy Unexecuted instantiation: v3_addr.c:ossl_key_raw_copy Unexecuted instantiation: v3_asid.c:ossl_key_raw_copy Unexecuted instantiation: v3_battcons.c:ossl_key_raw_copy Unexecuted instantiation: v3_bcons.c:ossl_key_raw_copy Unexecuted instantiation: v3_cpols.c:ossl_key_raw_copy Unexecuted instantiation: v3_crld.c:ossl_key_raw_copy |
149 | | |
150 | | /* |
151 | | * Copy data directly into a key |
152 | | * When initialized with HT_INIT_RAW_KEY, this macro |
153 | | * can be used to copy packed data into a key for hashtable usage |
154 | | * It is advantageous as it limits the amount of data that needs to |
155 | | * be hashed when doing inserts/lookups/deletes, as it tracks how much |
156 | | * key data is actually valid |
157 | | */ |
158 | | #define HT_COPY_RAW_KEY(key, buf, len) ossl_key_raw_copy(key, buf, len) |
159 | | |
160 | | /* |
161 | | * Similar to HT_COPY_RAW_KEY but accepts a character buffer, and copies |
162 | | * data while converting case for case insensitive matches |
163 | | */ |
164 | | #define HT_COPY_RAW_KEY_CASE(key, buf, len) \ |
165 | 112k | do { \ |
166 | 112k | size_t tmplen = (size_t)(len); \ |
167 | 112k | if (tmplen > (key)->bufsize - (key)->keysize) \ |
168 | 112k | tmplen = (key)->bufsize - (key)->keysize; \ |
169 | 112k | ossl_ht_strcase((key), (char *)&((key)->keybuf[(key)->keysize]), buf, tmplen); \ |
170 | 112k | (key)->keysize += tmplen; \ |
171 | 112k | } while (0) |
172 | | |
173 | | #define HT_INIT_KEY_CACHED(key, hash) \ |
174 | 0 | do { \ |
175 | 0 | HT_INIT_KEY((key)); \ |
176 | 0 | (key)->key_header.cached_hash = hash; \ |
177 | 0 | } while (0) |
178 | | |
179 | | #define HT_INIT_KEY_EXTERNAL(key, buf, len) \ |
180 | 82.5k | do { \ |
181 | 82.5k | HT_INIT_KEY((key)); \ |
182 | 82.5k | (key)->key_header.keybuf = (buf); \ |
183 | 82.5k | (key)->key_header.keysize = (len); \ |
184 | 82.5k | } while (0) |
185 | | |
186 | 191 | #define HT_KEY_GET_HASH(key) (key)->key_header.cached_hash |
187 | | |
188 | | /* |
189 | | * Resets a hash table key to a known state |
190 | | */ |
191 | | #define HT_KEY_RESET(key) \ |
192 | | do { \ |
193 | | memset((key)->key_header.keybuf, 0, (key)->key_header.keysize); \ |
194 | | (key)->key_header.cached_hash = 0; \ |
195 | | } while (0) |
196 | | |
197 | | /* |
198 | | * Sets a scalar field in a hash table key |
199 | | */ |
200 | 0 | #define HT_SET_KEY_FIELD(key, member, value) (key)->keyfields.member = value; |
201 | | |
202 | | /* |
203 | | * Sets a string field in a hash table key, preserving |
204 | | * null terminator |
205 | | */ |
206 | | #define HT_SET_KEY_STRING(key, member, value) \ |
207 | | do { \ |
208 | | if ((value) != NULL) \ |
209 | | strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \ |
210 | | } while (0) |
211 | | |
212 | | /* |
213 | | * This is the same as HT_SET_KEY_STRING, except that it uses |
214 | | * ossl_ht_strcase to make the value being passed case insensitive |
215 | | * This is useful for instances in which we want upper and lower case |
216 | | * key value to hash to the same entry |
217 | | */ |
218 | | #define HT_SET_KEY_STRING_CASE(key, member, value) \ |
219 | | do { \ |
220 | | ossl_ht_strcase(NULL, (key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \ |
221 | | } while (0) |
222 | | |
223 | | /* |
224 | | * Same as HT_SET_KEY_STRING but also takes length of the string. |
225 | | */ |
226 | | #define HT_SET_KEY_STRING_N(key, member, value, len) \ |
227 | | do { \ |
228 | | if ((value) != NULL) { \ |
229 | | if (len < sizeof((key)->keyfields.member)) \ |
230 | | strncpy((key)->keyfields.member, value, len); \ |
231 | | else \ |
232 | | strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \ |
233 | | } \ |
234 | | } while (0) |
235 | | |
236 | | /* Same as HT_SET_KEY_STRING_CASE but also takes length of the string. */ |
237 | | #define HT_SET_KEY_STRING_CASE_N(key, member, value, len) \ |
238 | | do { \ |
239 | | if ((size_t)len < sizeof((key)->keyfields.member)) \ |
240 | | ossl_ht_strcase(NULL, (key)->keyfields.member, value, len); \ |
241 | | else \ |
242 | | ossl_ht_strcase(NULL, (key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \ |
243 | | } while (0) |
244 | | |
245 | | /* |
246 | | * Sets a uint8_t (blob) field in a hash table key |
247 | | */ |
248 | | #define HT_SET_KEY_BLOB(key, member, value, len) \ |
249 | | do { \ |
250 | | if (value != NULL) \ |
251 | | memcpy((key)->keyfields.member, value, len); \ |
252 | | } while (0) |
253 | | |
254 | | /* |
255 | | * Converts a defined key type to an HT_KEY |
256 | | */ |
257 | 194k | #define TO_HT_KEY(key) &(key)->key_header |
258 | | |
259 | | /* |
260 | | * Converts an HT_KEY back to its defined |
261 | | * type |
262 | | */ |
263 | | #define FROM_HT_KEY(key, type) (type)(key) |
264 | | |
265 | | /* |
266 | | * Implements the following type safe operations for a hash table |
267 | | * ossl_ht_NAME_TYPE_insert - insert a value to a hash table of type TYPE |
268 | | * ossl_ht_NAME_TYPE_get - gets a value of a specific type from the hash table |
269 | | * ossl_ht_NAME_TYPE_from_value - converts an HT_VALUE to its type |
270 | | * ossl_ht_NAME_TYPE_to_value - converts a TYPE to an HT_VALUE |
271 | | * ossl_ht_NAME_TYPE_type - boolean to detect if a value is of TYPE |
272 | | */ |
273 | | #define IMPLEMENT_HT_VALUE_TYPE_FNS(vtype, name, pfx) \ |
274 | | static uintptr_t name##_##vtype##_id = 0; \ |
275 | | pfx ossl_unused int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, \ |
276 | | vtype *data, \ |
277 | | vtype **olddata) \ |
278 | 170 | { \ |
279 | 170 | HT_VALUE inval; \ |
280 | 170 | HT_VALUE *oval = NULL; \ |
281 | 170 | int rc; \ |
282 | 170 | \ |
283 | 170 | inval.value = data; \ |
284 | 170 | inval.type_id = &name##_##vtype##_id; \ |
285 | 170 | rc = ossl_ht_insert(h, key, &inval, olddata == NULL ? NULL : &oval); \ |
286 | 170 | if (oval != NULL) \ |
287 | 170 | *olddata = (vtype *)ossl_ht_inner_value(h, oval); \ |
288 | 170 | return rc; \ |
289 | 170 | } \ |
290 | | \ |
291 | | pfx ossl_unused vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v) \ |
292 | 82.3k | { \ |
293 | 82.3k | uintptr_t *expect_type = &name##_##vtype##_id; \ |
294 | 82.3k | if (v == NULL) \ |
295 | 82.3k | return NULL; \ |
296 | 82.3k | if (v->type_id != expect_type) \ |
297 | 82.3k | return NULL; \ |
298 | 82.3k | return (vtype *)v->value; \ |
299 | 82.3k | } \ |
300 | | \ |
301 | | pfx ossl_unused vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \ |
302 | | HT_KEY *key, \ |
303 | | HT_VALUE **v) \ |
304 | 82.3k | { \ |
305 | 82.3k | HT_VALUE *vv; \ |
306 | 82.3k | vv = ossl_ht_get(h, key); \ |
307 | 82.3k | if (vv == NULL) \ |
308 | 82.3k | return NULL; \ |
309 | 82.3k | *v = ossl_ht_deref_value(h, &vv); \ |
310 | 82.3k | return ossl_ht_##name##_##vtype##_from_value(*v); \ |
311 | 82.3k | } \ |
312 | | \ |
313 | | pfx ossl_unused HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, \ |
314 | | HT_VALUE *v) \ |
315 | 0 | { \ |
316 | 0 | v->type_id = &name##_##vtype##_id; \ |
317 | 0 | v->value = data; \ |
318 | 0 | return v; \ |
319 | 0 | } \ |
320 | | \ |
321 | | pfx ossl_unused int ossl_ht_##name##_##vtype##_type(HT_VALUE *h) \ |
322 | 0 | { \ |
323 | 0 | return h->type_id == &name##_##vtype##_id; \ |
324 | 0 | } |
325 | | |
326 | | #define DECLARE_HT_VALUE_TYPE_FNS(vtype, name) \ |
327 | | int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, vtype *data, \ |
328 | | vtype **olddata); \ |
329 | | vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v); \ |
330 | | vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \ |
331 | | HT_KEY *key, \ |
332 | | HT_VALUE **v); \ |
333 | | HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, HT_VALUE *v); \ |
334 | | int ossl_ht_##name##_##vtype##_type(HT_VALUE *h); |
335 | | |
336 | | /* |
337 | | * Helper function to construct case insensitive keys |
338 | | */ |
339 | | static ossl_inline ossl_unused void ossl_ht_strcase(HT_KEY *key, char *tgt, const char *src, size_t len) |
340 | 112k | { |
341 | 112k | size_t i; |
342 | | #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST) |
343 | | const long int case_adjust = ~0x40; |
344 | | #else |
345 | 112k | const long int case_adjust = ~0x20; |
346 | 112k | #endif |
347 | | |
348 | 112k | if (src == NULL) |
349 | 0 | return; |
350 | | |
351 | | /* |
352 | | * If we're passed a key, we're doing raw key copies |
353 | | * so check that we don't overflow here, and truncate if |
354 | | * we copy more space than we have available |
355 | | */ |
356 | 112k | if (key != NULL && key->keysize + len > key->bufsize) |
357 | 0 | len = (size_t)(key->bufsize - key->keysize); |
358 | | |
359 | 983k | for (i = 0; src[i] != '\0' && i < len; i++) |
360 | 870k | tgt[i] = case_adjust & src[i]; |
361 | 112k | } core_namemap.c:ossl_ht_strcase Line | Count | Source | 340 | 112k | { | 341 | 112k | size_t i; | 342 | | #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST) | 343 | | const long int case_adjust = ~0x40; | 344 | | #else | 345 | 112k | const long int case_adjust = ~0x20; | 346 | 112k | #endif | 347 | | | 348 | 112k | if (src == NULL) | 349 | 0 | return; | 350 | | | 351 | | /* | 352 | | * If we're passed a key, we're doing raw key copies | 353 | | * so check that we don't overflow here, and truncate if | 354 | | * we copy more space than we have available | 355 | | */ | 356 | 112k | if (key != NULL && key->keysize + len > key->bufsize) | 357 | 0 | len = (size_t)(key->bufsize - key->keysize); | 358 | | | 359 | 983k | for (i = 0; src[i] != '\0' && i < len; i++) | 360 | 870k | tgt[i] = case_adjust & src[i]; | 361 | 112k | } |
Unexecuted instantiation: property.c:ossl_ht_strcase Unexecuted instantiation: by_dir.c:ossl_ht_strcase Unexecuted instantiation: by_file.c:ossl_ht_strcase Unexecuted instantiation: by_store.c:ossl_ht_strcase Unexecuted instantiation: v3_lib.c:ossl_ht_strcase Unexecuted instantiation: v3_purp.c:ossl_ht_strcase Unexecuted instantiation: v3_tlsf.c:ossl_ht_strcase Unexecuted instantiation: v3_utl.c:ossl_ht_strcase Unexecuted instantiation: x509_att.c:ossl_ht_strcase Unexecuted instantiation: x509_lu.c:ossl_ht_strcase Unexecuted instantiation: x509_set.c:ossl_ht_strcase Unexecuted instantiation: x509_v3.c:ossl_ht_strcase Unexecuted instantiation: x509_vfy.c:ossl_ht_strcase Unexecuted instantiation: x509_vpm.c:ossl_ht_strcase Unexecuted instantiation: x_all.c:ossl_ht_strcase Unexecuted instantiation: x_attrib.c:ossl_ht_strcase Unexecuted instantiation: x_crl.c:ossl_ht_strcase Unexecuted instantiation: x_exten.c:ossl_ht_strcase Unexecuted instantiation: x_name.c:ossl_ht_strcase Unexecuted instantiation: hashtable.c:ossl_ht_strcase Unexecuted instantiation: v3_ac_tgt.c:ossl_ht_strcase Unexecuted instantiation: v3_addr.c:ossl_ht_strcase Unexecuted instantiation: v3_asid.c:ossl_ht_strcase Unexecuted instantiation: v3_battcons.c:ossl_ht_strcase Unexecuted instantiation: v3_bcons.c:ossl_ht_strcase Unexecuted instantiation: v3_cpols.c:ossl_ht_strcase Unexecuted instantiation: v3_crld.c:ossl_ht_strcase |
362 | | |
363 | | /* |
364 | | * Create a new hashtable |
365 | | */ |
366 | | HT *ossl_ht_new(const HT_CONFIG *conf); |
367 | | |
368 | | /* |
369 | | * Frees a hash table, potentially freeing all elements |
370 | | */ |
371 | | void ossl_ht_free(HT *htable); |
372 | | |
373 | | /* |
374 | | * Lock the table for reading |
375 | | */ |
376 | | int ossl_ht_read_lock(HT *htable); |
377 | | |
378 | | /* |
379 | | * Lock the table for writing |
380 | | */ |
381 | | void ossl_ht_write_lock(HT *htable); |
382 | | |
383 | | /* |
384 | | * Read unlock |
385 | | */ |
386 | | void ossl_ht_read_unlock(HT *htable); |
387 | | |
388 | | /* |
389 | | * Write unlock |
390 | | */ |
391 | | void ossl_ht_write_unlock(HT *htable); |
392 | | |
393 | | /* |
394 | | * Empties a hash table, potentially freeing all elements |
395 | | */ |
396 | | int ossl_ht_flush(HT *htable); |
397 | | |
398 | | /* |
399 | | * Inserts an element to a hash table, optionally returning |
400 | | * replaced data to caller |
401 | | * Returns 1 if the insert was successful, 0 on error |
402 | | */ |
403 | | int ossl_ht_insert(HT *htable, HT_KEY *key, HT_VALUE *data, |
404 | | HT_VALUE **olddata); |
405 | | |
406 | | /* |
407 | | * Deletes a value from a hash table, based on key |
408 | | * Returns 1 if the key was removed, 0 if they key was not found |
409 | | */ |
410 | | int ossl_ht_delete(HT *htable, HT_KEY *key); |
411 | | |
412 | | /* |
413 | | * Returns number of elements in the hash table |
414 | | */ |
415 | | size_t ossl_ht_count(HT *htable); |
416 | | |
417 | | /* |
418 | | * Iterates over each element in the table. |
419 | | * aborts the loop when cb returns 0 |
420 | | * Contents of elements in the list may be modified during |
421 | | * this traversal, assuming proper thread safety is observed while doing |
422 | | * so (holding the table write lock is sufficient). However, elements of the |
423 | | * table may not be inserted or removed while iterating. |
424 | | */ |
425 | | void ossl_ht_foreach_until(HT *htable, int (*cb)(HT_VALUE *obj, void *arg), |
426 | | void *arg); |
427 | | /* |
428 | | * Returns a list of elements in a hash table based on |
429 | | * filter function return value. Returns NULL on error, |
430 | | * or an HT_VALUE_LIST object on success. Note it is possible |
431 | | * That a list will be returned with 0 entries, if none were found. |
432 | | * The zero length list must still be freed via ossl_ht_value_list_free |
433 | | */ |
434 | | HT_VALUE_LIST *ossl_ht_filter(HT *htable, size_t max_len, |
435 | | int (*filter)(HT_VALUE *obj, void *arg), |
436 | | void *arg); |
437 | | /* |
438 | | * Frees the list returned from ossl_ht_filter |
439 | | */ |
440 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list); |
441 | | |
442 | | /* |
443 | | * Fetches a value from the hash table, based |
444 | | * on key. Returns NULL if the element was not found. |
445 | | */ |
446 | | HT_VALUE *ossl_ht_get(HT *htable, HT_KEY *key); |
447 | | |
448 | | /** |
449 | | * ossl_ht_deref_value - Dereference a value stored in a hash table entry |
450 | | * @h: The hash table handle |
451 | | * @val: Pointer to the value pointer inside the hash table |
452 | | * |
453 | | * This helper returns the actual value stored in a hash table entry, |
454 | | * with awareness of whether the table is configured for RCU (Read-Copy-Update) |
455 | | * safe lookups. |
456 | | * |
457 | | * If the hash table is configured to use RCU lookups, the function |
458 | | * calls ossl_rcu_deref() to safely read the value under RCU protection. |
459 | | * This ensures that the caller sees a consistent pointer in concurrent environments. |
460 | | * |
461 | | * If RCU is not enabled (i.e. `h->config.no_rcu` is true), the function |
462 | | * simply dereferences @val directly. |
463 | | * |
464 | | * Return: |
465 | | * A pointer to the dereferenced hash table value (`HT_VALUE *`), or NULL if |
466 | | * the underlying pointer is NULL. |
467 | | */ |
468 | | HT_VALUE *ossl_ht_deref_value(HT *p, HT_VALUE **val); |
469 | | |
470 | | /** |
471 | | * ossl_ht_inner_value - Extract the user payload from a hash table value |
472 | | * @h: The hash table handle |
473 | | * @v: The hash table value wrapper (HT_VALUE) |
474 | | * |
475 | | * This helper returns the user-provided payload stored inside a |
476 | | * hash table value container. The behavior differs depending on |
477 | | * whether the hash table is configured to use RCU (Read-Copy-Update) |
478 | | * for concurrency control. |
479 | | * |
480 | | * - If RCU is enabled, the function simply returns `v->value` without |
481 | | * modifying or freeing the container. |
482 | | * |
483 | | * - If RCU is disabled the container structure `v` is no longer needed once |
484 | | * the inner pointer has been extracted. In this case, the function frees |
485 | | * `v` and returns the inner `value` pointer directly. |
486 | | * |
487 | | * Return: |
488 | | * A pointer to the user payload (`void *`) contained in the hash table |
489 | | * value wrapper. |
490 | | */ |
491 | | void *ossl_ht_inner_value(HT *h, HT_VALUE *v); |
492 | | |
493 | | #endif |