1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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"
27 #include "curl_memory.h"
28 /* The last #include file should be: */
32 hash_element_dtor(void *user, void *element)
34 struct curl_hash *h = (struct curl_hash *) user;
35 struct curl_hash_element *e = (struct curl_hash_element *) element;
37 Curl_safefree(e->key);
49 /* Initializes a hash structure.
50 * Return 1 on error, 0 is fine.
56 Curl_hash_init(struct curl_hash *h,
59 comp_function comparator,
64 if(!slots || !hfunc || !comparator ||!dtor) {
65 return 1; /* failure */
69 h->comp_func = comparator;
74 h->table = malloc(slots * sizeof(struct curl_llist *));
76 for(i = 0; i < slots; ++i) {
77 h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
80 Curl_llist_destroy(h->table[i], NULL);
86 return 1; /* failure */
93 return 1; /* failure */
97 static struct curl_hash_element *
98 mk_hash_element(const void *key, size_t key_len, const void *p)
100 struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
103 void *dupkey = malloc(key_len);
106 memcpy(dupkey, key, key_len);
109 he->key_len = key_len;
110 he->ptr = (void *) p;
113 /* failed to duplicate the key, free memory and fail */
121 #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
123 /* Insert the data in the hash. If there already was a match in the hash,
124 * that data is replaced.
131 Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
133 struct curl_hash_element *he;
134 struct curl_llist_element *le;
135 struct curl_llist *l = FETCH_LIST (h, key, key_len);
137 for(le = l->head; le; le = le->next) {
138 he = (struct curl_hash_element *) le->ptr;
139 if(h->comp_func(he->key, he->key_len, key, key_len)) {
140 Curl_llist_remove(l, le, (void *)h);
146 he = mk_hash_element(key, key_len, p);
148 if(Curl_llist_insert_next(l, l->tail, he)) {
150 return p; /* return the new entry */
153 * Couldn't insert it, destroy the 'he' element and the key again. We
154 * don't call hash_element_dtor() since that would also call the
155 * "destructor" for the actual data 'p'. When we fail, we shall not touch
162 return NULL; /* failure */
165 /* Remove the identified hash entry.
166 * Returns non-zero on failure.
170 int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
172 struct curl_llist_element *le;
173 struct curl_hash_element *he;
174 struct curl_llist *l = FETCH_LIST(h, key, key_len);
176 for(le = l->head; le; le = le->next) {
178 if(h->comp_func(he->key, he->key_len, key, key_len)) {
179 Curl_llist_remove(l, le, (void *) h);
187 /* Retrieves a hash element.
192 Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
194 struct curl_llist_element *le;
195 struct curl_hash_element *he;
196 struct curl_llist *l;
199 l = FETCH_LIST(h, key, key_len);
200 for(le = l->head; le; le = le->next) {
202 if(h->comp_func(he->key, he->key_len, key, key_len)) {
211 #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
213 Curl_hash_apply(curl_hash *h, void *user,
214 void (*cb)(void *user, void *ptr))
216 struct curl_llist_element *le;
219 for(i = 0; i < h->slots; ++i) {
220 for(le = (h->table[i])->head;
223 curl_hash_element *el = le->ptr;
230 /* Destroys all the entries in the given hash and resets its attributes,
231 * prepping the given hash for [static|dynamic] deallocation.
238 Curl_hash_destroy(struct curl_hash *h)
242 for(i = 0; i < h->slots; ++i) {
243 Curl_llist_destroy(h->table[i], (void *) h);
247 Curl_safefree(h->table);
252 /* Removes all the entries in the given hash.
257 Curl_hash_clean(struct curl_hash *h)
259 Curl_hash_clean_with_criterium(h, NULL, NULL);
262 /* Cleans all entries that pass the comp function criteria. */
264 Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
265 int (*comp)(void *, void *))
267 struct curl_llist_element *le;
268 struct curl_llist_element *lnext;
269 struct curl_llist *list;
275 for(i = 0; i < h->slots; ++i) {
277 le = list->head; /* get first list entry */
279 struct curl_hash_element *he = le->ptr;
281 /* ask the callback function if we shall remove this entry or not */
282 if(comp == NULL || comp(user, he->ptr)) {
283 Curl_llist_remove(list, le, (void *) h);
284 --h->size; /* one less entry in the hash now */
291 size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
293 const char* key_str = (const char *) key;
294 const char *end = key_str + key_length;
295 unsigned long h = 5381;
297 while(key_str < end) {
299 h ^= (unsigned long) *key_str++;
302 return (h % slots_num);
305 size_t Curl_str_key_compare(void *k1, size_t key1_len,
306 void *k2, size_t key2_len)
308 if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
314 void Curl_hash_start_iterate(struct curl_hash *hash,
315 struct curl_hash_iterator *iter)
318 iter->slot_index = 0;
319 iter->current_element = NULL;
322 struct curl_hash_element *
323 Curl_hash_next_element(struct curl_hash_iterator *iter)
326 struct curl_hash *h = iter->hash;
328 /* Get the next element in the current list, if any */
329 if(iter->current_element)
330 iter->current_element = iter->current_element->next;
332 /* If we have reached the end of the list, find the next one */
333 if(!iter->current_element) {
334 for(i = iter->slot_index;i < h->slots;i++) {
335 if(h->table[i]->head) {
336 iter->current_element = h->table[i]->head;
337 iter->slot_index = i+1;
343 if(iter->current_element) {
344 struct curl_hash_element *he = iter->current_element->ptr;
348 iter->current_element = NULL;
353 #if 0 /* useful function for debugging hashes and their contents */
354 void Curl_hash_print(struct curl_hash *h,
355 void (*func)(void *))
357 struct curl_hash_iterator iter;
358 struct curl_hash_element *he;
364 fprintf(stderr, "=Hash dump=\n");
366 Curl_hash_start_iterate(h, &iter);
368 he = Curl_hash_next_element(&iter);
370 if(iter.slot_index != last_index) {
371 fprintf(stderr, "index %d:", iter.slot_index);
372 if(last_index >= 0) {
373 fprintf(stderr, "\n");
375 last_index = iter.slot_index;
381 fprintf(stderr, " [%p]", (void *)he->ptr);
383 he = Curl_hash_next_element(&iter);
385 fprintf(stderr, "\n");