From 1e08179bf82c1fdc29b3bae89b3a7b62e67e4ec6 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 23 Jun 2011 13:36:49 +0100 Subject: [PATCH 5/8] dbus-memory: use atomic accesses to block count, even for assertions --- dbus/dbus-memory.c | 26 ++++++++++++++++++-------- 1 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dbus/dbus-memory.c b/dbus/dbus-memory.c index a37759c..5725a34 100644 --- a/dbus/dbus-memory.c +++ b/dbus/dbus-memory.c @@ -295,7 +295,15 @@ _dbus_decrement_fail_alloc_counter (void) int _dbus_get_malloc_blocks_outstanding (void) { - return n_blocks_outstanding.value; + dbus_int32_t ret; + + /* We increment and then decrement the count, because there is no + * _dbus_atomic_get (mirroring the fact that there's no InterlockedGet + * on Windows). */ + ret = _dbus_atomic_inc (&n_blocks_outstanding); + _dbus_atomic_dec (&n_blocks_outstanding); + + return ret; } /** @@ -634,10 +642,11 @@ dbus_free (void *memory) check_guards (memory, TRUE); if (memory) { - _dbus_atomic_dec (&n_blocks_outstanding); - - _dbus_assert (n_blocks_outstanding.value >= 0); - + dbus_int32_t old_value; + + old_value = _dbus_atomic_dec (&n_blocks_outstanding); + _dbus_assert (old_value >= 1); + free (((unsigned char*)memory) - GUARD_START_OFFSET); } @@ -648,9 +657,10 @@ dbus_free (void *memory) if (memory) /* we guarantee it's safe to free (NULL) */ { #ifdef DBUS_BUILD_TESTS - _dbus_atomic_dec (&n_blocks_outstanding); - - _dbus_assert (n_blocks_outstanding.value >= 0); + dbus_int32_t old_value; + + old_value = _dbus_atomic_dec (&n_blocks_outstanding); + _dbus_assert (old_value >= 1); #endif free (memory); -- 1.7.5.4