2002-06-11 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / bcache.c
index 766aff9c3bb8be82ef315559f549e74741e06b41..73b86e8cf53e6c3813ae7699d34d5e5248dae092 100644 (file)
@@ -1,7 +1,8 @@
 /* Implement a cached obstack.
    Written by Fred Fish <fnf@cygnus.com>
    Rewritten by Jim Blandy <jimb@cygnus.com>
-   Copyright 1999 Free Software Foundation, Inc.
+
+   Copyright 1999, 2000, 2002 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#include <stddef.h>
-#include <stdlib.h>
-
 #include "defs.h"
 #include "obstack.h"
 #include "bcache.h"
 #include "gdb_string.h"                /* For memcpy declaration */
 
+#include <stddef.h>
+#include <stdlib.h>
 
+/* The old hash function was stolen from SDBM. This is what DB 3.0 uses now,
+ * and is better than the old one. 
+ */
 \f
-/* The hash function.  */
-
 unsigned long
-hash (void *addr, int length)
+hash(const void *addr, int length)
 {
-  /* If it's a short string, hash on every character.  Otherwise, sample
-     characters from throughout the string.  */
-  if (length <= 64)
-    {
-      char *byte = addr;
-      unsigned long h = 0;
-      int i;
-
-      for (i = 0; i < length; i++)
-       h = h * 65793 ^ (h >> (sizeof (h) * 8 - 6)) ^ byte[i];
-
-      return h;
-    }
-  else
-    {
-      char *byte = addr;
-      int n, i;
-      unsigned long h = 0;
-
-      for (n = i = 0; n < 64; n++)
-       {
-         h = h * 65793 + (h >> (sizeof (h) * 8 - 6)) + byte[i];
-         i = h % length;
-       }
-
-      return h;
-    }
+               const unsigned char *k, *e;
+               unsigned long h;
+               
+               k = (const unsigned char *)addr;
+               e = k+length;
+               for (h=0; k< e;++k)
+               {
+                               h *=16777619;
+                               h ^= *k;
+               }
+               return (h);
 }
-
 \f
 /* Growing the bcache's hash table.  */
 
@@ -128,7 +112,7 @@ expand_hash_table (struct bcache *bcache)
 
   /* Plug in the new table.  */
   if (bcache->bucket)
-    free (bcache->bucket);
+    xfree (bcache->bucket);
   bcache->bucket = new_buckets;
   bcache->num_buckets = new_num_buckets;
 }
@@ -144,7 +128,7 @@ expand_hash_table (struct bcache *bcache)
    never seen those bytes before, add a copy of them to BCACHE.  In
    either case, return a pointer to BCACHE's copy of that string.  */
 void *
-bcache (void *addr, int length, struct bcache *bcache)
+bcache (const void *addr, int length, struct bcache *bcache)
 {
   int hash_index;
   struct bstring *s;
@@ -189,7 +173,8 @@ void
 free_bcache (struct bcache *bcache)
 {
   obstack_free (&bcache->cache, 0);
-  free (bcache->bucket);
+  if (bcache->bucket)
+    xfree (bcache->bucket);
 
   /* This isn't necessary, but at least the bcache is always in a
      consistent state.  */
This page took 0.025304 seconds and 4 git commands to generate.