sim: profile: implement --profile-file backend
[deliverable/binutils-gdb.git] / gdb / bcache.c
index 589de6a252ca8fd4a24075d14a924adde469d59d..4badf6edf02b72baeb55a672c9c7655710fb6e93 100644 (file)
@@ -2,7 +2,7 @@
    Written by Fred Fish <fnf@cygnus.com>
    Rewritten by Jim Blandy <jimb@cygnus.com>
 
-   Copyright (C) 1999, 2000, 2002, 2003, 2007, 2008
+   Copyright (C) 1999, 2000, 2002, 2003, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -217,6 +217,16 @@ bcache_full (const void *addr, int length, struct bcache *bcache, int *added)
   if (added)
     *added = 0;
 
+  /* Lazily initialize the obstack.  This can save quite a bit of
+     memory in some cases.  */
+  if (bcache->total_count == 0)
+    {
+      /* We could use obstack_specify_allocation here instead, but
+        gdb_obstack.h specifies the allocation/deallocation
+        functions.  */
+      obstack_init (&bcache->cache);
+    }
+
   /* If our average chain length is too high, expand the hash table.  */
   if (bcache->unique_count >= bcache->num_buckets * CHAIN_LENGTH_THRESHOLD)
     expand_hash_table (bcache);
@@ -271,10 +281,6 @@ bcache_xmalloc (void)
 {
   /* Allocate the bcache pre-zeroed.  */
   struct bcache *b = XCALLOC (1, struct bcache);
-  /* We could use obstack_specify_allocation here instead, but
-     gdb_obstack.h specifies the allocation/deallocation
-     functions.  */
-  obstack_init (&b->cache);
   return b;
 }
 
@@ -284,7 +290,9 @@ bcache_xfree (struct bcache *bcache)
 {
   if (bcache == NULL)
     return;
-  obstack_free (&bcache->cache, 0);
+  /* Only free the obstack if we actually initialized it.  */
+  if (bcache->total_count > 0)
+    obstack_free (&bcache->cache, 0);
   xfree (bcache->bucket);
   xfree (bcache);
 }
@@ -293,15 +301,6 @@ bcache_xfree (struct bcache *bcache)
 \f
 /* Printing statistics.  */
 
-static int
-compare_ints (const void *ap, const void *bp)
-{
-  /* Because we know we're comparing two ints which are positive,
-     there's no danger of overflow here.  */
-  return * (int *) ap - * (int *) bp;
-}
-
-
 static void
 print_percentage (int portion, int total)
 {
@@ -359,9 +358,9 @@ print_bcache_statistics (struct bcache *c, char *type)
 
     /* To compute the median, we need the set of chain lengths sorted.  */
     qsort (chain_length, c->num_buckets, sizeof (chain_length[0]),
-          compare_ints);
+          compare_positive_ints);
     qsort (entry_size, c->unique_count, sizeof (entry_size[0]),
-          compare_ints);
+          compare_positive_ints);
 
     if (c->num_buckets > 0)
       {
@@ -443,5 +442,7 @@ print_bcache_statistics (struct bcache *c, char *type)
 int
 bcache_memory_used (struct bcache *bcache)
 {
+  if (bcache->total_count == 0)
+    return 0;
   return obstack_memory_used (&bcache->cache);
 }
This page took 0.02535 seconds and 4 git commands to generate.