dbus_message_cache_or_finalize: allow message cache to be disabled at runtime
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Thu, 12 May 2011 12:01:32 +0000 (13:01 +0100)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Mon, 13 Feb 2012 17:54:37 +0000 (17:54 +0000)
This should make it easier to diagnose message-related ref leaks,
use-after-free, etc. with Valgrind: for optimal results (and pessimal
performance), we want to avoid re-using memory blocks for as long as
possible.

For now this is conditional on DBUS_BUILD_TESTS. It could get its own
conditional if desired.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37286
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: Lennart Poettering <lennart@poettering.net>
dbus/dbus-message.c

index 0c7e80d..a09c4c8 100644 (file)
@@ -52,6 +52,37 @@ static void dbus_message_finalize (DBusMessage *message);
  * @{
  */
 
+#ifdef DBUS_BUILD_TESTS
+static dbus_bool_t
+_dbus_enable_message_cache (void)
+{
+  static int enabled = -1;
+
+  if (enabled < 0)
+    {
+      const char *s = _dbus_getenv ("DBUS_MESSAGE_CACHE");
+
+      enabled = TRUE;
+
+      if (s && *s)
+        {
+          if (*s == '0')
+            enabled = FALSE;
+          else if (*s == '1')
+            enabled = TRUE;
+          else
+            _dbus_warn ("DBUS_MESSAGE_CACHE should be 0 or 1 if set, not '%s'",
+                s);
+        }
+    }
+
+  return enabled;
+}
+#else
+    /* constant expression, should be optimized away */
+#   define _dbus_enable_message_cache() (TRUE)
+#endif
+
 /* Not thread locked, but strictly const/read-only so should be OK
  */
 /** An static string representing an empty signature */
@@ -632,6 +663,9 @@ dbus_message_cache_or_finalize (DBusMessage *message)
 
   _dbus_assert (message_cache_count >= 0);
 
+  if (!_dbus_enable_message_cache ())
+    goto out;
+
   if ((_dbus_string_get_length (&message->header.data) +
        _dbus_string_get_length (&message->body)) >
       MAX_MESSAGE_SIZE_TO_CACHE)