summaryrefslogtreecommitdiff
path: root/gdb/bcache.c
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/bcache.c')
-rw-r--r--gdb/bcache.c75
1 files changed, 66 insertions, 9 deletions
diff --git a/gdb/bcache.c b/gdb/bcache.c
index 73b86e8cf53..5a310f3ee6d 100644
--- a/gdb/bcache.c
+++ b/gdb/bcache.c
@@ -22,13 +22,57 @@
Boston, MA 02111-1307, USA. */
#include "defs.h"
-#include "obstack.h"
+#include "gdb_obstack.h"
#include "bcache.h"
#include "gdb_string.h" /* For memcpy declaration */
#include <stddef.h>
#include <stdlib.h>
+/* The type used to hold a single bcache string. The user data is
+ stored in d.data. Since it can be any type, it needs to have the
+ same alignment as the most strict alignment of any type on the host
+ machine. I don't know of any really correct way to do this in
+ stock ANSI C, so just do it the same way obstack.h does. */
+
+struct bstring
+{
+ struct bstring *next;
+ size_t length;
+
+ union
+ {
+ char data[1];
+ double dummy;
+ }
+ d;
+};
+
+
+/* The structure for a bcache itself. The bcache is initialized, in
+ bcache_xmalloc(), by filling it with zeros and then setting the
+ corresponding obstack's malloc() and free() methods. */
+
+struct bcache
+{
+ /* All the bstrings are allocated here. */
+ struct obstack cache;
+
+ /* How many hash buckets we're using. */
+ unsigned int num_buckets;
+
+ /* Hash buckets. This table is allocated using malloc, so when we
+ grow the table we can return the old table to the system. */
+ struct bstring **bucket;
+
+ /* Statistics. */
+ unsigned long unique_count; /* number of unique strings */
+ long total_count; /* total number of strings cached, including dups */
+ long unique_size; /* size of unique strings, in bytes */
+ long total_size; /* total number of bytes cached, including dups */
+ long structure_size; /* total size of bcache, including infrastructure */
+};
+
/* The old hash function was stolen from SDBM. This is what DB 3.0 uses now,
* and is better than the old one.
*/
@@ -166,19 +210,26 @@ bcache (const void *addr, int length, struct bcache *bcache)
}
-/* Freeing bcaches. */
+/* Allocating and freeing bcaches. */
+
+struct bcache *
+bcache_xmalloc (void)
+{
+ /* Allocate the bcache pre-zeroed. */
+ struct bcache *b = XCALLOC (1, struct bcache);
+ obstack_specify_allocation (&b->cache, 0, 0, xmalloc, xfree);
+ return b;
+}
/* Free all the storage associated with BCACHE. */
void
-free_bcache (struct bcache *bcache)
+bcache_xfree (struct bcache *bcache)
{
+ if (bcache == NULL)
+ return;
obstack_free (&bcache->cache, 0);
- if (bcache->bucket)
- xfree (bcache->bucket);
-
- /* This isn't necessary, but at least the bcache is always in a
- consistent state. */
- memset (bcache, 0, sizeof (*bcache));
+ xfree (bcache->bucket);
+ xfree (bcache);
}
@@ -291,3 +342,9 @@ print_bcache_statistics (struct bcache *c, char *type)
printf_filtered (" Maximum hash chain length: %3d\n", max_chain_length);
printf_filtered ("\n");
}
+
+int
+bcache_memory_used (struct bcache *bcache)
+{
+ return obstack_memory_used (&bcache->cache);
+}