daemon fix and development
[platform/upstream/dbus.git] / dbus / dbus-mempool.c
index a53a488..e744585 100644 (file)
@@ -1,9 +1,9 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-mempool.h Memory pools
  * 
  * Copyright (C) 2002, 2003  Red Hat, Inc.
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-mempool.h"
 #include "dbus-internals.h"
+#include "dbus-valgrind-internal.h"
 
 /**
  * @defgroup DBusMemPool memory pools
@@ -170,7 +172,9 @@ _dbus_mem_pool_new (int element_size,
 
   _dbus_assert ((pool->block_size %
                  pool->element_size) == 0);
-  
+
+  VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
+
   return pool;
 }
 
@@ -184,6 +188,8 @@ _dbus_mem_pool_free (DBusMemPool *pool)
 {
   DBusMemBlock *block;
 
+  VALGRIND_DESTROY_MEMPOOL (pool);
+
   block = pool->blocks;
   while (block != NULL)
     {
@@ -207,7 +213,7 @@ _dbus_mem_pool_free (DBusMemPool *pool)
 void*
 _dbus_mem_pool_alloc (DBusMemPool *pool)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (_dbus_disable_mem_pools ())
     {
       DBusMemBlock *block;
@@ -234,6 +240,8 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
           pool->blocks = block;
           pool->allocated_elements += 1;
 
+          VALGRIND_MEMPOOL_ALLOC (pool, (void *) &block->elements[0],
+              pool->element_size);
           return (void*) &block->elements[0];
         }
       else
@@ -253,11 +261,13 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
 
           pool->free_elements = pool->free_elements->next;
 
+          VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
+
           if (pool->zero_elements)
             memset (element, '\0', pool->element_size);
 
           pool->allocated_elements += 1;
-          
+
           return element;
         }
       else
@@ -270,7 +280,7 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
               /* Need a new block */
               DBusMemBlock *block;
               int alloc_size;
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
               int saved_counter;
 #endif
           
@@ -284,7 +294,7 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
 
               alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
               /* We save/restore the counter, so that memory pools won't
                * cause a given function to have different number of
                * allocations on different invocations. i.e.  when testing
@@ -300,7 +310,7 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
               else
                 block = dbus_malloc (alloc_size);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
               _dbus_set_fail_alloc_counter (saved_counter);
               _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
 #endif
@@ -318,7 +328,8 @@ _dbus_mem_pool_alloc (DBusMemPool *pool)
           pool->blocks->used_so_far += pool->element_size;
 
           pool->allocated_elements += 1;
-          
+
+          VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
           return element;
         }
     }
@@ -336,7 +347,9 @@ dbus_bool_t
 _dbus_mem_pool_dealloc (DBusMemPool *pool,
                         void        *element)
 {
-#ifdef DBUS_BUILD_TESTS
+  VALGRIND_MEMPOOL_FREE (pool, element);
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (_dbus_disable_mem_pools ())
     {
       DBusMemBlock *block;
@@ -379,6 +392,9 @@ _dbus_mem_pool_dealloc (DBusMemPool *pool,
       DBusFreedElement *freed;
       
       freed = element;
+      /* used for internal mempool administration */
+      VALGRIND_MAKE_MEM_UNDEFINED (freed, sizeof (freed));
+
       freed->next = pool->free_elements;
       pool->free_elements = freed;
       
@@ -389,9 +405,51 @@ _dbus_mem_pool_dealloc (DBusMemPool *pool,
     }
 }
 
+#ifdef DBUS_ENABLE_STATS
+void
+_dbus_mem_pool_get_stats (DBusMemPool   *pool,
+                          dbus_uint32_t *in_use_p,
+                          dbus_uint32_t *in_free_list_p,
+                          dbus_uint32_t *allocated_p)
+{
+  DBusMemBlock *block;
+  DBusFreedElement *freed;
+  dbus_uint32_t in_use = 0;
+  dbus_uint32_t in_free_list = 0;
+  dbus_uint32_t allocated = 0;
+
+  if (pool != NULL)
+    {
+      in_use = pool->element_size * pool->allocated_elements;
+
+      for (freed = pool->free_elements; freed != NULL; freed = freed->next)
+        {
+          in_free_list += pool->element_size;
+        }
+
+      for (block = pool->blocks; block != NULL; block = block->next)
+        {
+          if (block == pool->blocks)
+            allocated += pool->block_size;
+          else
+            allocated += block->used_so_far;
+        }
+    }
+
+  if (in_use_p != NULL)
+    *in_use_p = in_use;
+
+  if (in_free_list_p != NULL)
+    *in_free_list_p = in_free_list;
+
+  if (allocated_p != NULL)
+    *allocated_p = allocated;
+}
+#endif /* DBUS_ENABLE_STATS */
+
 /** @} */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 #include <stdio.h>
 #include <time.h>
@@ -401,8 +459,10 @@ time_for_size (int size)
 {
   int i;
   int j;
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   clock_t start;
   clock_t end;
+#endif
 #define FREE_ARRAY_SIZE 512
 #define N_ITERATIONS FREE_ARRAY_SIZE * 512
   void *to_free[FREE_ARRAY_SIZE];
@@ -412,8 +472,10 @@ time_for_size (int size)
   
   _dbus_verbose (" malloc\n");
   
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   start = clock ();
-  
+#endif
+
   i = 0;
   j = 0;
   while (i < N_ITERATIONS)
@@ -438,6 +500,7 @@ time_for_size (int size)
       ++i;
     }
 
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   end = clock ();
 
   _dbus_verbose ("  created/destroyed %d elements in %g seconds\n",
@@ -448,6 +511,7 @@ time_for_size (int size)
   _dbus_verbose (" mempools\n");
   
   start = clock ();
+#endif
 
   pool = _dbus_mem_pool_new (size, FALSE);
   
@@ -477,6 +541,7 @@ time_for_size (int size)
 
   _dbus_mem_pool_free (pool);
   
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   end = clock ();
 
   _dbus_verbose ("  created/destroyed %d elements in %g seconds\n",
@@ -485,6 +550,7 @@ time_for_size (int size)
   _dbus_verbose (" zeroed malloc\n");
     
   start = clock ();
+#endif
   
   i = 0;
   j = 0;
@@ -510,6 +576,7 @@ time_for_size (int size)
       ++i;
     }
 
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   end = clock ();
 
   _dbus_verbose ("  created/destroyed %d elements in %g seconds\n",
@@ -518,6 +585,7 @@ time_for_size (int size)
   _dbus_verbose (" zeroed mempools\n");
   
   start = clock ();
+#endif
 
   pool = _dbus_mem_pool_new (size, TRUE);
   
@@ -547,10 +615,12 @@ time_for_size (int size)
 
   _dbus_mem_pool_free (pool);
   
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   end = clock ();
 
   _dbus_verbose ("  created/destroyed %d elements in %g seconds\n",
                  N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
+#endif
 }
 
 /**
@@ -574,4 +644,4 @@ _dbus_mem_pool_test (void)
   return TRUE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */