Revert "Update to 7.44.0"
[platform/upstream/curl.git] / lib / hash.c
index c8b5f8b..4a12e1a 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id$
  ***************************************************************************/
 
-#include "setup.h"
-
-#include <string.h>
-#include <stdlib.h>
+#include "curl_setup.h"
 
 #include "hash.h"
 #include "llist.h"
-#include "memory.h"
 
-/* this must be the last include file */
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
+#include "curl_memory.h"
+/* The last #include file should be: */
 #include "memdebug.h"
 
 static void
@@ -39,10 +38,14 @@ hash_element_dtor(void *user, void *element)
   struct curl_hash *h = (struct curl_hash *) user;
   struct curl_hash_element *e = (struct curl_hash_element *) element;
 
-  if (e->key)
-    free(e->key);
+  Curl_safefree(e->key);
 
-  h->dtor(e->ptr);
+  if(e->ptr) {
+    h->dtor(e->ptr);
+    e->ptr = NULL;
+  }
+
+  e->key_len = 0;
 
   free(e);
 }
@@ -57,7 +60,7 @@ Curl_hash_init(struct curl_hash *h,
 {
   int i;
 
-  if (!slots || !hfunc || !comparator ||!dtor) {
+  if(!slots || !hfunc || !comparator ||!dtor) {
     return 1; /* failure */
   }
 
@@ -67,21 +70,27 @@ Curl_hash_init(struct curl_hash *h,
   h->size = 0;
   h->slots = slots;
 
-  h->table = (struct curl_llist **) malloc(slots * sizeof(struct curl_llist *));
+  h->table = malloc(slots * sizeof(struct curl_llist *));
   if(h->table) {
-    for (i = 0; i < slots; ++i) {
+    for(i = 0; i < slots; ++i) {
       h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
       if(!h->table[i]) {
-        while(i--)
+        while(i--) {
           Curl_llist_destroy(h->table[i], NULL);
+          h->table[i] = NULL;
+        }
         free(h->table);
+        h->table = NULL;
+        h->slots = 0;
         return 1; /* failure */
       }
     }
     return 0; /* fine */
   }
-  else
+  else {
+    h->slots = 0;
     return 1; /* failure */
+  }
 }
 
 struct curl_hash *
@@ -92,12 +101,12 @@ Curl_hash_alloc(int slots,
 {
   struct curl_hash *h;
 
-  if (!slots || !hfunc || !comparator ||!dtor) {
+  if(!slots || !hfunc || !comparator ||!dtor) {
     return NULL; /* failure */
   }
 
-  h = (struct curl_hash *) malloc(sizeof(struct curl_hash));
-  if (h) {
+  h = malloc(sizeof(struct curl_hash));
+  if(h) {
     if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
       /* failure */
       free(h);
@@ -113,16 +122,15 @@ Curl_hash_alloc(int slots,
 static struct curl_hash_element *
 mk_hash_element(const void *key, size_t key_len, const void *p)
 {
-  struct curl_hash_element *he =
-    (struct curl_hash_element *) malloc(sizeof(struct curl_hash_element));
+  struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
 
   if(he) {
-    void *dup = malloc(key_len);
-    if(dup) {
+    void *dupkey = malloc(key_len);
+    if(dupkey) {
       /* copy the key */
-      memcpy(dup, key, key_len);
+      memcpy(dupkey, key, key_len);
 
-      he->key = dup;
+      he->key = dupkey;
       he->key_len = key_len;
       he->ptr = (void *) p;
     }
@@ -137,8 +145,11 @@ mk_hash_element(const void *key, size_t key_len, const void *p)
 
 #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
 
-/* Return the data in the hash. If there already was a match in the hash,
-   that data is returned. */
+/* Insert the data in the hash. If there already was a match in the hash,
+ * that data is replaced.
+ *
+ * @unittest: 1305
+ */
 void *
 Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
 {
@@ -146,16 +157,17 @@ Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
   struct curl_llist_element *le;
   struct curl_llist *l = FETCH_LIST (h, key, key_len);
 
-  for (le = l->head; le; le = le->next) {
+  for(le = l->head; le; le = le->next) {
     he = (struct curl_hash_element *) le->ptr;
-    if (h->comp_func(he->key, he->key_len, key, key_len)) {
-      h->dtor(p);     /* remove the NEW entry */
-      return he->ptr; /* return the EXISTING entry */
+    if(h->comp_func(he->key, he->key_len, key, key_len)) {
+      Curl_llist_remove(l, le, (void *)h);
+      --h->size;
+      break;
     }
   }
 
   he = mk_hash_element(key, key_len, p);
-  if (he) {
+  if(he) {
     if(Curl_llist_insert_next(l, l->tail, he)) {
       ++h->size;
       return p; /* return the new entry */
@@ -180,10 +192,11 @@ int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
   struct curl_hash_element  *he;
   struct curl_llist *l = FETCH_LIST(h, key, key_len);
 
-  for (le = l->head; le; le = le->next) {
+  for(le = l->head; le; le = le->next) {
     he = le->ptr;
-    if (h->comp_func(he->key, he->key_len, key, key_len)) {
+    if(h->comp_func(he->key, he->key_len, key, key_len)) {
       Curl_llist_remove(l, le, (void *) h);
+      --h->size;
       return 0;
     }
   }
@@ -195,19 +208,22 @@ Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
 {
   struct curl_llist_element *le;
   struct curl_hash_element  *he;
-  struct curl_llist *l = FETCH_LIST(h, key, key_len);
-
-  for (le = l->head; le; le = le->next) {
-    he = le->ptr;
-    if (h->comp_func(he->key, he->key_len, key, key_len)) {
-      return he->ptr;
+  struct curl_llist *l;
+
+  if(h) {
+    l = FETCH_LIST(h, key, key_len);
+    for(le = l->head; le; le = le->next) {
+      he = le->ptr;
+      if(h->comp_func(he->key, he->key_len, key, key_len)) {
+        return he->ptr;
+      }
     }
   }
 
   return NULL;
 }
 
-#if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
+#if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
 void
 Curl_hash_apply(curl_hash *h, void *user,
                 void (*cb)(void *user, void *ptr))
@@ -215,10 +231,10 @@ Curl_hash_apply(curl_hash *h, void *user,
   struct curl_llist_element  *le;
   int                  i;
 
-  for (i = 0; i < h->slots; ++i) {
-    for (le = (h->table[i])->head;
-         le;
-         le = le->next) {
+  for(i = 0; i < h->slots; ++i) {
+    for(le = (h->table[i])->head;
+        le;
+        le = le->next) {
       curl_hash_element *el = le->ptr;
       cb(user, el->ptr);
     }
@@ -231,11 +247,14 @@ Curl_hash_clean(struct curl_hash *h)
 {
   int i;
 
-  for (i = 0; i < h->slots; ++i) {
+  for(i = 0; i < h->slots; ++i) {
     Curl_llist_destroy(h->table[i], (void *) h);
+    h->table[i] = NULL;
   }
 
-  free(h->table);
+  Curl_safefree(h->table);
+  h->size = 0;
+  h->slots = 0;
 }
 
 void
@@ -247,14 +266,17 @@ Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
   struct curl_llist *list;
   int i;
 
-  for (i = 0; i < h->slots; ++i) {
+  if(!h)
+    return;
+
+  for(i = 0; i < h->slots; ++i) {
     list = h->table[i];
     le = list->head; /* get first list entry */
     while(le) {
       struct curl_hash_element *he = le->ptr;
       lnext = le->next;
       /* ask the callback function if we shall remove this entry or not */
-      if (comp(user, he->ptr)) {
+      if(comp(user, he->ptr)) {
         Curl_llist_remove(list, le, (void *) h);
         --h->size; /* one less entry in the hash now */
       }
@@ -266,10 +288,11 @@ Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
 void
 Curl_hash_destroy(struct curl_hash *h)
 {
-  if (!h)
+  if(!h)
     return;
 
   Curl_hash_clean(h);
+
   free(h);
 }
 
@@ -279,7 +302,7 @@ size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
   const char *end = key_str + key_length;
   unsigned long h = 5381;
 
-  while (key_str < end) {
+  while(key_str < end) {
     h += h << 5;
     h ^= (unsigned long) *key_str++;
   }
@@ -292,7 +315,7 @@ size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2, size_t key2_len)
   char *key1 = (char *)k1;
   char *key2 = (char *)k2;
 
-  if (key1_len == key2_len &&
+  if(key1_len == key2_len &&
       *key1 == *key2 &&
       memcmp(key1, key2, key1_len) == 0) {
     return 1;
@@ -301,34 +324,77 @@ size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2, size_t key2_len)
   return 0;
 }
 
+void Curl_hash_start_iterate(struct curl_hash *hash,
+                             struct curl_hash_iterator *iter)
+{
+  iter->hash = hash;
+  iter->slot_index = 0;
+  iter->current_element = NULL;
+}
+
+struct curl_hash_element *
+Curl_hash_next_element(struct curl_hash_iterator *iter)
+{
+  int i;
+  struct curl_hash *h = iter->hash;
+
+  /* Get the next element in the current list, if any */
+  if(iter->current_element)
+    iter->current_element = iter->current_element->next;
+
+  /* If we have reached the end of the list, find the next one */
+  if(!iter->current_element) {
+    for(i = iter->slot_index;i < h->slots;i++) {
+      if(h->table[i]->head) {
+        iter->current_element = h->table[i]->head;
+        iter->slot_index = i+1;
+        break;
+      }
+    }
+  }
+
+  if(iter->current_element) {
+    struct curl_hash_element *he = iter->current_element->ptr;
+    return he;
+  }
+  else {
+    iter->current_element = NULL;
+    return NULL;
+  }
+}
+
 #if 0 /* useful function for debugging hashes and their contents */
 void Curl_hash_print(struct curl_hash *h,
                      void (*func)(void *))
 {
-  int i;
-  struct curl_llist_element *le;
-  struct curl_llist *list;
-  struct curl_hash_element  *he;
-  if (!h)
+  struct curl_hash_iterator iter;
+  struct curl_hash_element *he;
+  int last_index = -1;
+
+  if(!h)
     return;
 
   fprintf(stderr, "=Hash dump=\n");
 
-  for (i = 0; i < h->slots; i++) {
-    list = h->table[i];
-    le = list->head; /* get first list entry */
-    if(le) {
-      fprintf(stderr, "index %d:", i);
-      while(le) {
-        he = le->ptr;
-        if(func)
-          func(he->ptr);
-        else
-          fprintf(stderr, " [%p]", he->ptr);
-        le = le->next;
+  Curl_hash_start_iterate(h, &iter);
+
+  he = Curl_hash_next_element(&iter);
+  while(he) {
+    if(iter.slot_index != last_index) {
+      fprintf(stderr, "index %d:", iter.slot_index);
+      if(last_index >= 0) {
+        fprintf(stderr, "\n");
       }
-      fprintf(stderr, "\n");
+      last_index = iter.slot_index;
     }
+
+    if(func)
+      func(he->ptr);
+    else
+      fprintf(stderr, " [%p]", (void *)he->ptr);
+
+    he = Curl_hash_next_element(&iter);
   }
+  fprintf(stderr, "\n");
 }
 #endif