From 16f330a950930727a7aa2d65bea362fe26c0c399 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 12 May 2011 13:01:32 +0100 Subject: [PATCH 3/8] dbus_message_cache_or_finalize: allow message cache to be disabled at runtime 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. --- dbus/dbus-message.c | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-) diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index cc1cfa1..2ba5f5b 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -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) -- 1.7.6.3