n_mallocs -= 1;
}
- _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
+ _dbus_set_fail_alloc_counter (-1);
return TRUE;
}
/* Run once to see about how many mallocs are involved */
- _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
+ _dbus_set_fail_alloc_counter (-1);
_dbus_test_diag ("Running \"%s\" once to count mallocs", description);
if (!(* func) (data, TRUE))
return FALSE;
- approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
+ /* We have decremented the counter once per allocation, so for example
+ * if there were 10 allocations, it will have changed from -1 to -11.
+ * Subtract from -1 to get the positive number of allocations that took
+ * place. */
+ approx_mallocs = -1 - _dbus_get_fail_alloc_counter ();
_dbus_test_diag ("\"%s\" has about %d mallocs in total",
description, approx_mallocs);
static dbus_bool_t debug_initialized = FALSE;
static int fail_nth = -1;
static size_t fail_size = 0;
-static int fail_alloc_counter = _DBUS_INT_MAX;
+static int fail_alloc_counter = -1;
static int n_failures_per_failure = 1;
static int n_failures_this_failure = 0;
static dbus_bool_t guards = FALSE;
* Sets the number of allocations until we simulate a failed
* allocation. If set to 0, the next allocation to run
* fails; if set to 1, one succeeds then the next fails; etc.
- * Set to _DBUS_INT_MAX to not fail anything.
+ *
+ * Set to a negative number to not fail anything.
+ * In this case, the counter is still decremented every time we allocate
+ * memory (until it reaches _DBUS_INT_MIN), which can be used to estimate
+ * the number of allocations in a particular unit test case.
*
* @param until_next_fail number of successful allocs before one fails
*/
/**
* Gets the number of successful allocs until we'll simulate
- * a failed alloc.
+ * a failed alloc, or negative if we will not simulate a failed alloc.
+ *
+ * If negative, the counter indicates the number of allocations since
+ * it was set to a known value, which can be used to count allocations:
+ * see _dbus_set_fail_alloc_counter() for details.
*
* @returns current counter value
*/
{
_dbus_initialize_malloc_debug ();
- if (fail_alloc_counter <= 0)
+ if (fail_alloc_counter < 0)
+ {
+ /* We never fail in this case, but we still decrement the counter,
+ * so that _dbus_test_oom_handling() can use it to count allocations.
+ * Saturate at _DBUS_INT_MIN to avoid undefined integer overflow
+ * (or in this case underflow). */
+ if (fail_alloc_counter > _DBUS_INT_MIN)
+ fail_alloc_counter -= 1;
+
+ return FALSE;
+ }
+ else if (fail_alloc_counter == 0)
{
+ /* It's time to pretend we ran out of memory. */
if (backtrace_on_fail_alloc)
_dbus_print_backtrace ();
n_failures_this_failure += 1;
if (n_failures_this_failure >= n_failures_per_failure)
{
- if (fail_nth >= 0)
- fail_alloc_counter = fail_nth;
- else
- fail_alloc_counter = _DBUS_INT_MAX;
-
+ fail_alloc_counter = fail_nth;
n_failures_this_failure = 0;
_dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
}
else
{
+ /* Don't fail this allocation, but count down to the next time we
+ * will fail an allocation. */
fail_alloc_counter -= 1;
return FALSE;
}