/* 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 Free Software Foundation, Inc.
This file is part of GDB.
#include "bcache.h"
#include "gdb_string.h" /* For memcpy declaration */
-
+/* 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(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. */
4194301, 8388617, 16777213, 33554467, 67108859, 134217757,
268435459, 536870923, 1073741827, 2147483659UL
};
- int new_num_buckets;
+ unsigned int new_num_buckets;
struct bstring **new_buckets;
- int i;
+ unsigned int i;
/* Find the next size. */
+ new_num_buckets = bcache->num_buckets * 2;
for (i = 0; i < (sizeof (sizes) / sizeof (sizes[0])); i++)
if (sizes[i] > bcache->num_buckets)
{
new_num_buckets = sizes[i];
break;
}
- if (i >= (sizeof (sizes) / sizeof (sizes[0])))
- new_num_buckets = bcache->num_buckets * 2;
/* Allocate the new table. */
{
/* Plug in the new table. */
if (bcache->bucket)
- free (bcache->bucket);
+ xfree (bcache->bucket);
bcache->bucket = new_buckets;
bcache->num_buckets = new_num_buckets;
}
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. */
/* Count the number of occupied buckets, and measure chain lengths. */
{
- int b;
+ unsigned int b;
int *chain_length
= (int *) alloca (c->num_buckets * sizeof (*chain_length));
printf_filtered (" Cached '%s' statistics:\n", type);
printf_filtered (" Total object count: %ld\n", c->total_count);
- printf_filtered (" Unique object count: %ld\n", c->unique_count);
+ printf_filtered (" Unique object count: %lu\n", c->unique_count);
printf_filtered (" Percentage of duplicates, by count: ");
print_percentage (c->total_count - c->unique_count, c->total_count);
printf_filtered ("\n");
median_chain_length);
printf_filtered (" Average hash chain length: ");
if (c->num_buckets > 0)
- printf_filtered ("%3ld\n", c->unique_count / c->num_buckets);
+ printf_filtered ("%3lu\n", c->unique_count / c->num_buckets);
else
printf_filtered ("(not applicable)\n");
printf_filtered (" Maximum hash chain length: %3d\n", max_chain_length);