1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
29 #include "curl_memory.h"
31 /* The last #include file should be: */
35 hash_element_dtor(void *user, void *element)
37 struct curl_hash *h = (struct curl_hash *) user;
38 struct curl_hash_element *e = (struct curl_hash_element *) element;
50 /* Initializes a hash structure.
51 * Return 1 on error, 0 is fine.
57 Curl_hash_init(struct curl_hash *h,
60 comp_function comparator,
65 if(!slots || !hfunc || !comparator ||!dtor) {
66 return 1; /* failure */
70 h->comp_func = comparator;
75 h->table = malloc(slots * sizeof(struct curl_llist));
77 for(i = 0; i < slots; ++i)
78 Curl_llist_init(&h->table[i], (curl_llist_dtor) hash_element_dtor);
82 return 1; /* failure */
85 static struct curl_hash_element *
86 mk_hash_element(const void *key, size_t key_len, const void *p)
88 /* allocate the struct plus memory after it to store the key */
89 struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element) +
93 memcpy(he->key, key, key_len);
94 he->key_len = key_len;
100 #define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
102 /* Insert the data in the hash. If there already was a match in the hash,
103 * that data is replaced.
110 Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
112 struct curl_hash_element *he;
113 struct curl_llist_element *le;
114 struct curl_llist *l = FETCH_LIST(h, key, key_len);
116 for(le = l->head; le; le = le->next) {
117 he = (struct curl_hash_element *) le->ptr;
118 if(h->comp_func(he->key, he->key_len, key, key_len)) {
119 Curl_llist_remove(l, le, (void *)h);
125 he = mk_hash_element(key, key_len, p);
127 Curl_llist_insert_next(l, l->tail, he, &he->list);
129 return p; /* return the new entry */
132 return NULL; /* failure */
135 /* Remove the identified hash entry.
136 * Returns non-zero on failure.
140 int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
142 struct curl_llist_element *le;
143 struct curl_hash_element *he;
144 struct curl_llist *l = FETCH_LIST(h, key, key_len);
146 for(le = l->head; le; le = le->next) {
148 if(h->comp_func(he->key, he->key_len, key, key_len)) {
149 Curl_llist_remove(l, le, (void *) h);
157 /* Retrieves a hash element.
162 Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
164 struct curl_llist_element *le;
165 struct curl_hash_element *he;
166 struct curl_llist *l;
169 l = FETCH_LIST(h, key, key_len);
170 for(le = l->head; le; le = le->next) {
172 if(h->comp_func(he->key, he->key_len, key, key_len)) {
181 #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
183 Curl_hash_apply(curl_hash *h, void *user,
184 void (*cb)(void *user, void *ptr))
186 struct curl_llist_element *le;
189 for(i = 0; i < h->slots; ++i) {
190 for(le = (h->table[i])->head;
193 curl_hash_element *el = le->ptr;
200 /* Destroys all the entries in the given hash and resets its attributes,
201 * prepping the given hash for [static|dynamic] deallocation.
208 Curl_hash_destroy(struct curl_hash *h)
212 for(i = 0; i < h->slots; ++i) {
213 Curl_llist_destroy(&h->table[i], (void *) h);
216 Curl_safefree(h->table);
221 /* Removes all the entries in the given hash.
226 Curl_hash_clean(struct curl_hash *h)
228 Curl_hash_clean_with_criterium(h, NULL, NULL);
231 /* Cleans all entries that pass the comp function criteria. */
233 Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
234 int (*comp)(void *, void *))
236 struct curl_llist_element *le;
237 struct curl_llist_element *lnext;
238 struct curl_llist *list;
244 for(i = 0; i < h->slots; ++i) {
246 le = list->head; /* get first list entry */
248 struct curl_hash_element *he = le->ptr;
250 /* ask the callback function if we shall remove this entry or not */
251 if(comp == NULL || comp(user, he->ptr)) {
252 Curl_llist_remove(list, le, (void *) h);
253 --h->size; /* one less entry in the hash now */
260 size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
262 const char *key_str = (const char *) key;
263 const char *end = key_str + key_length;
264 unsigned long h = 5381;
266 while(key_str < end) {
268 h ^= (unsigned long) *key_str++;
271 return (h % slots_num);
274 size_t Curl_str_key_compare(void *k1, size_t key1_len,
275 void *k2, size_t key2_len)
277 if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
283 void Curl_hash_start_iterate(struct curl_hash *hash,
284 struct curl_hash_iterator *iter)
287 iter->slot_index = 0;
288 iter->current_element = NULL;
291 struct curl_hash_element *
292 Curl_hash_next_element(struct curl_hash_iterator *iter)
295 struct curl_hash *h = iter->hash;
297 /* Get the next element in the current list, if any */
298 if(iter->current_element)
299 iter->current_element = iter->current_element->next;
301 /* If we have reached the end of the list, find the next one */
302 if(!iter->current_element) {
303 for(i = iter->slot_index; i < h->slots; i++) {
304 if(h->table[i].head) {
305 iter->current_element = h->table[i].head;
306 iter->slot_index = i + 1;
312 if(iter->current_element) {
313 struct curl_hash_element *he = iter->current_element->ptr;
316 iter->current_element = NULL;
320 #if 0 /* useful function for debugging hashes and their contents */
321 void Curl_hash_print(struct curl_hash *h,
322 void (*func)(void *))
324 struct curl_hash_iterator iter;
325 struct curl_hash_element *he;
331 fprintf(stderr, "=Hash dump=\n");
333 Curl_hash_start_iterate(h, &iter);
335 he = Curl_hash_next_element(&iter);
337 if(iter.slot_index != last_index) {
338 fprintf(stderr, "index %d:", iter.slot_index);
339 if(last_index >= 0) {
340 fprintf(stderr, "\n");
342 last_index = iter.slot_index;
348 fprintf(stderr, " [%p]", (void *)he->ptr);
350 he = Curl_hash_next_element(&iter);
352 fprintf(stderr, "\n");