From 07a795f1db3b09beeec647643a4f263f617bc371 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 14 Feb 2003 04:54:55 +0000 Subject: 2003-02-14 Havoc Pennington * dbus/dbus-mempool.c: fail if the debug functions so indicate * dbus/dbus-memory.c: fail if the debug functions indicate we should * dbus/dbus-internals.c (_dbus_set_fail_alloc_counter) (_dbus_decrement_fail_alloc_counter): debug functions to simulate memory allocation failures --- dbus/dbus-internals.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'dbus/dbus-internals.c') diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index 78a1b687..d51f5a97 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -391,4 +391,56 @@ _dbus_type_to_string (int type) } } +#ifdef DBUS_BUILD_TESTS +static int fail_alloc_counter = _DBUS_INT_MAX; +/** + * 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. + * + * @param until_next_fail number of successful allocs before one fails + */ +void +_dbus_set_fail_alloc_counter (int until_next_fail) +{ + fail_alloc_counter = until_next_fail; +} + +/** + * Gets the number of successful allocs until we'll simulate + * a failed alloc. + * + * @returns current counter value + */ +int +_dbus_get_fail_alloc_counter (void) +{ + return fail_alloc_counter; +} + +/** + * Called when about to alloc some memory; if + * it returns #TRUE, then the allocation should + * fail. If it returns #FALSE, then the allocation + * should not fail. + * + * @returns #TRUE if this alloc should fail + */ +dbus_bool_t +_dbus_decrement_fail_alloc_counter (void) +{ + if (fail_alloc_counter <= 0) + { + fail_alloc_counter = _DBUS_INT_MAX; + return TRUE; + } + else + { + fail_alloc_counter -= 1; + return FALSE; + } +} +#endif /* DBUS_BUILD_TESTS */ + /** @} */ -- cgit