1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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;
40 Curl_safefree(e->key);
52 /* Initializes a hash structure.
53 * Return 1 on error, 0 is fine.
59 Curl_hash_init(struct curl_hash *h,
62 comp_function comparator,
67 if(!slots || !hfunc || !comparator ||!dtor) {
68 return 1; /* failure */
72 h->comp_func = comparator;
77 h->table = malloc(slots * sizeof(struct curl_llist *));
79 for(i = 0; i < slots; ++i) {
80 h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
83 Curl_llist_destroy(h->table[i], NULL);
89 return 1; /* failure */
96 return 1; /* failure */
100 static struct curl_hash_element *
101 mk_hash_element(const void *key, size_t key_len, const void *p)
103 struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
106 void *dupkey = malloc(key_len);
109 memcpy(dupkey, key, key_len);
112 he->key_len = key_len;
113 he->ptr = (void *) p;
116 /* failed to duplicate the key, free memory and fail */
124 #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
126 /* Insert the data in the hash. If there already was a match in the hash,
127 * that data is replaced.
134 Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
136 struct curl_hash_element *he;
137 struct curl_llist_element *le;
138 struct curl_llist *l = FETCH_LIST (h, key, key_len);
140 for(le = l->head; le; le = le->next) {
141 he = (struct curl_hash_element *) le->ptr;
142 if(h->comp_func(he->key, he->key_len, key, key_len)) {
143 Curl_llist_remove(l, le, (void *)h);
149 he = mk_hash_element(key, key_len, p);
151 if(Curl_llist_insert_next(l, l->tail, he)) {
153 return p; /* return the new entry */
156 * Couldn't insert it, destroy the 'he' element and the key again. We
157 * don't call hash_element_dtor() since that would also call the
158 * "destructor" for the actual data 'p'. When we fail, we shall not touch
165 return NULL; /* failure */
168 /* Remove the identified hash entry.
169 * Returns non-zero on failure.
173 int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
175 struct curl_llist_element *le;
176 struct curl_hash_element *he;
177 struct curl_llist *l = FETCH_LIST(h, key, key_len);
179 for(le = l->head; le; le = le->next) {
181 if(h->comp_func(he->key, he->key_len, key, key_len)) {
182 Curl_llist_remove(l, le, (void *) h);
190 /* Retrieves a hash element.
195 Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
197 struct curl_llist_element *le;
198 struct curl_hash_element *he;
199 struct curl_llist *l;
202 l = FETCH_LIST(h, key, key_len);
203 for(le = l->head; le; le = le->next) {
205 if(h->comp_func(he->key, he->key_len, key, key_len)) {
214 #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
216 Curl_hash_apply(curl_hash *h, void *user,
217 void (*cb)(void *user, void *ptr))
219 struct curl_llist_element *le;
222 for(i = 0; i < h->slots; ++i) {
223 for(le = (h->table[i])->head;
226 curl_hash_element *el = le->ptr;
233 /* Destroys all the entries in the given hash and resets its attributes,
234 * prepping the given hash for [static|dynamic] deallocation.
241 Curl_hash_destroy(struct curl_hash *h)
245 for(i = 0; i < h->slots; ++i) {
246 Curl_llist_destroy(h->table[i], (void *) h);
250 Curl_safefree(h->table);
255 /* Removes all the entries in the given hash.
260 Curl_hash_clean(struct curl_hash *h)
262 Curl_hash_clean_with_criterium(h, NULL, NULL);
265 /* Cleans all entries that pass the comp function criteria. */
267 Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
268 int (*comp)(void *, void *))
270 struct curl_llist_element *le;
271 struct curl_llist_element *lnext;
272 struct curl_llist *list;
278 for(i = 0; i < h->slots; ++i) {
280 le = list->head; /* get first list entry */
282 struct curl_hash_element *he = le->ptr;
284 /* ask the callback function if we shall remove this entry or not */
285 if(comp == NULL || comp(user, he->ptr)) {
286 Curl_llist_remove(list, le, (void *) h);
287 --h->size; /* one less entry in the hash now */
294 size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
296 const char* key_str = (const char *) key;
297 const char *end = key_str + key_length;
298 unsigned long h = 5381;
300 while(key_str < end) {
302 h ^= (unsigned long) *key_str++;
305 return (h % slots_num);
308 size_t Curl_str_key_compare(void *k1, size_t key1_len,
309 void *k2, size_t key2_len)
311 if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
317 void Curl_hash_start_iterate(struct curl_hash *hash,
318 struct curl_hash_iterator *iter)
321 iter->slot_index = 0;
322 iter->current_element = NULL;
325 struct curl_hash_element *
326 Curl_hash_next_element(struct curl_hash_iterator *iter)
329 struct curl_hash *h = iter->hash;
331 /* Get the next element in the current list, if any */
332 if(iter->current_element)
333 iter->current_element = iter->current_element->next;
335 /* If we have reached the end of the list, find the next one */
336 if(!iter->current_element) {
337 for(i = iter->slot_index;i < h->slots;i++) {
338 if(h->table[i]->head) {
339 iter->current_element = h->table[i]->head;
340 iter->slot_index = i+1;
346 if(iter->current_element) {
347 struct curl_hash_element *he = iter->current_element->ptr;
351 iter->current_element = NULL;
356 #if 0 /* useful function for debugging hashes and their contents */
357 void Curl_hash_print(struct curl_hash *h,
358 void (*func)(void *))
360 struct curl_hash_iterator iter;
361 struct curl_hash_element *he;
367 fprintf(stderr, "=Hash dump=\n");
369 Curl_hash_start_iterate(h, &iter);
371 he = Curl_hash_next_element(&iter);
373 if(iter.slot_index != last_index) {
374 fprintf(stderr, "index %d:", iter.slot_index);
375 if(last_index >= 0) {
376 fprintf(stderr, "\n");
378 last_index = iter.slot_index;
384 fprintf(stderr, " [%p]", (void *)he->ptr);
386 he = Curl_hash_next_element(&iter);
388 fprintf(stderr, "\n");