diff options
Diffstat (limited to 'gdb/bcache.c')
-rw-r--r-- | gdb/bcache.c | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/gdb/bcache.c b/gdb/bcache.c index 73b86e8..c5173e1 100644 --- a/gdb/bcache.c +++ b/gdb/bcache.c @@ -29,6 +29,50 @@ #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); +} |