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 http://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 /* return 1 on error, 0 is fine */
51 Curl_hash_init(struct curl_hash *h,
54 comp_function comparator,
59 if(!slots || !hfunc || !comparator ||!dtor) {
60 return 1; /* failure */
64 h->comp_func = comparator;
69 h->table = malloc(slots * sizeof(struct curl_llist *));
71 for(i = 0; i < slots; ++i) {
72 h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
75 Curl_llist_destroy(h->table[i], NULL);
81 return 1; /* failure */
88 return 1; /* failure */
92 static struct curl_hash_element *
93 mk_hash_element(const void *key, size_t key_len, const void *p)
95 struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
98 void *dupkey = malloc(key_len);
101 memcpy(dupkey, key, key_len);
104 he->key_len = key_len;
105 he->ptr = (void *) p;
108 /* failed to duplicate the key, free memory and fail */
116 #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
118 /* Insert the data in the hash. If there already was a match in the hash,
119 * that data is replaced.
124 Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
126 struct curl_hash_element *he;
127 struct curl_llist_element *le;
128 struct curl_llist *l = FETCH_LIST (h, key, key_len);
130 for(le = l->head; le; le = le->next) {
131 he = (struct curl_hash_element *) le->ptr;
132 if(h->comp_func(he->key, he->key_len, key, key_len)) {
133 Curl_llist_remove(l, le, (void *)h);
139 he = mk_hash_element(key, key_len, p);
141 if(Curl_llist_insert_next(l, l->tail, he)) {
143 return p; /* return the new entry */
146 * Couldn't insert it, destroy the 'he' element and the key again. We
147 * don't call hash_element_dtor() since that would also call the
148 * "destructor" for the actual data 'p'. When we fail, we shall not touch
155 return NULL; /* failure */
158 /* remove the identified hash entry, returns non-zero on failure */
159 int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
161 struct curl_llist_element *le;
162 struct curl_hash_element *he;
163 struct curl_llist *l = FETCH_LIST(h, key, key_len);
165 for(le = l->head; le; le = le->next) {
167 if(h->comp_func(he->key, he->key_len, key, key_len)) {
168 Curl_llist_remove(l, le, (void *) h);
177 Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
179 struct curl_llist_element *le;
180 struct curl_hash_element *he;
181 struct curl_llist *l;
184 l = FETCH_LIST(h, key, key_len);
185 for(le = l->head; le; le = le->next) {
187 if(h->comp_func(he->key, he->key_len, key, key_len)) {
196 #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
198 Curl_hash_apply(curl_hash *h, void *user,
199 void (*cb)(void *user, void *ptr))
201 struct curl_llist_element *le;
204 for(i = 0; i < h->slots; ++i) {
205 for(le = (h->table[i])->head;
208 curl_hash_element *el = le->ptr;
215 /* Destroys all the entries in the given hash and resets its attributes,
216 * prepping the given hash for [static|dynamic] deallocation.
219 Curl_hash_destroy(struct curl_hash *h)
223 for(i = 0; i < h->slots; ++i) {
224 Curl_llist_destroy(h->table[i], (void *) h);
228 Curl_safefree(h->table);
233 /* Removes all the entries in the given hash.
238 Curl_hash_clean(struct curl_hash *h)
240 Curl_hash_clean_with_criterium(h, NULL, NULL);
243 /* Cleans all entries that pass the comp function criteria. */
245 Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
246 int (*comp)(void *, void *))
248 struct curl_llist_element *le;
249 struct curl_llist_element *lnext;
250 struct curl_llist *list;
256 for(i = 0; i < h->slots; ++i) {
258 le = list->head; /* get first list entry */
260 struct curl_hash_element *he = le->ptr;
262 /* ask the callback function if we shall remove this entry or not */
263 if(comp == NULL || comp(user, he->ptr)) {
264 Curl_llist_remove(list, le, (void *) h);
265 --h->size; /* one less entry in the hash now */
272 size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
274 const char* key_str = (const char *) key;
275 const char *end = key_str + key_length;
276 unsigned long h = 5381;
278 while(key_str < end) {
280 h ^= (unsigned long) *key_str++;
283 return (h % slots_num);
286 size_t Curl_str_key_compare(void *k1, size_t key1_len,
287 void *k2, size_t key2_len)
289 if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
295 void Curl_hash_start_iterate(struct curl_hash *hash,
296 struct curl_hash_iterator *iter)
299 iter->slot_index = 0;
300 iter->current_element = NULL;
303 struct curl_hash_element *
304 Curl_hash_next_element(struct curl_hash_iterator *iter)
307 struct curl_hash *h = iter->hash;
309 /* Get the next element in the current list, if any */
310 if(iter->current_element)
311 iter->current_element = iter->current_element->next;
313 /* If we have reached the end of the list, find the next one */
314 if(!iter->current_element) {
315 for(i = iter->slot_index;i < h->slots;i++) {
316 if(h->table[i]->head) {
317 iter->current_element = h->table[i]->head;
318 iter->slot_index = i+1;
324 if(iter->current_element) {
325 struct curl_hash_element *he = iter->current_element->ptr;
329 iter->current_element = NULL;
334 #if 0 /* useful function for debugging hashes and their contents */
335 void Curl_hash_print(struct curl_hash *h,
336 void (*func)(void *))
338 struct curl_hash_iterator iter;
339 struct curl_hash_element *he;
345 fprintf(stderr, "=Hash dump=\n");
347 Curl_hash_start_iterate(h, &iter);
349 he = Curl_hash_next_element(&iter);
351 if(iter.slot_index != last_index) {
352 fprintf(stderr, "index %d:", iter.slot_index);
353 if(last_index >= 0) {
354 fprintf(stderr, "\n");
356 last_index = iter.slot_index;
362 fprintf(stderr, " [%p]", (void *)he->ptr);
364 he = Curl_hash_next_element(&iter);
366 fprintf(stderr, "\n");