DBusMemPool: add usage stats
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Tue, 8 Feb 2011 12:29:52 +0000 (12:29 +0000)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Fri, 24 Jun 2011 15:00:57 +0000 (16:00 +0100)
Reviewed-by: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=34040

dbus/dbus-mempool.c
dbus/dbus-mempool.h

index 680586b761a0b9dc02b6fdc612e9a1b3c2b5186d..563bffc6bc422e4888d5866ade0f7fee8ae4c902 100644 (file)
@@ -390,6 +390,48 @@ _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
index afe52472ac18f95f61b3e0bc89d67c38feef0d8e..6693eeb23d9f264e5e666f5593d71a5e8f0aa8cf 100644 (file)
@@ -39,6 +39,12 @@ void*        _dbus_mem_pool_alloc   (DBusMemPool *pool);
 dbus_bool_t  _dbus_mem_pool_dealloc (DBusMemPool *pool,
                                      void        *element);
 
+/* if 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);
+
 DBUS_END_DECLS
 
 #endif /* DBUS_MEMPOOL_H */