diff options
author | Harald Fernengel <harald@trolltech.com> | 2009-08-03 13:12:46 (GMT) |
---|---|---|
committer | Harald Fernengel <harald@trolltech.com> | 2009-08-03 13:12:46 (GMT) |
commit | 41a83e1ff19ad1396e6001e6b0ac003c701ba55a (patch) | |
tree | 609e40eda10418bbf925002c36552074796b96b6 /tests/auto/exceptionsafety_objects/oomsimulator.h | |
parent | d1f3b9df2bc5c57d414da73a7d4f9ed7b25df3db (diff) | |
download | Qt-41a83e1ff19ad1396e6001e6b0ac003c701ba55a.zip Qt-41a83e1ff19ad1396e6001e6b0ac003c701ba55a.tar.gz Qt-41a83e1ff19ad1396e6001e6b0ac003c701ba55a.tar.bz2 |
Squashed commit of the topic/exceptions branch.
Contains some smaller fixes and renaming of macros. Looks big,
but isn't scary at all ;)
Diffstat (limited to 'tests/auto/exceptionsafety_objects/oomsimulator.h')
-rw-r--r-- | tests/auto/exceptionsafety_objects/oomsimulator.h | 158 |
1 files changed, 123 insertions, 35 deletions
diff --git a/tests/auto/exceptionsafety_objects/oomsimulator.h b/tests/auto/exceptionsafety_objects/oomsimulator.h index 3c8b389..11dd769 100644 --- a/tests/auto/exceptionsafety_objects/oomsimulator.h +++ b/tests/auto/exceptionsafety_objects/oomsimulator.h @@ -39,10 +39,29 @@ ** ****************************************************************************/ +#ifndef Q_OS_SYMBIAN #include <malloc.h> +#endif #include <limits.h> -#include "3rdparty/memcheck.h" +#include <stdio.h> +#include <exception> +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN) +# include "3rdparty/memcheck.h" +#endif + +static bool mallocFailActive = false; +static int mallocFailIndex = 0; +static int mallocCount = 0; + +static void my_terminate_handler() +{ + // set a breakpoint here to get a backtrace for your uncaught exceptions + fprintf(stderr, "Uncaught Exception Detected. Set a breakpoint in my_terminate_handler()\n"); + exit(1); +} + +#ifdef __GLIBC__ /* Use glibc's memory allocation hooks */ /* our hooks */ @@ -88,40 +107,6 @@ void my_init_hook() enableHooks(); } -static bool mallocFailActive = false; -static int mallocFailIndex = 0; -static int mallocCount = 0; - -struct AllocFailer -{ - inline AllocFailer() { mallocFailActive = true; setAllocFailIndex(0); } - inline ~AllocFailer() { deactivate(); } - - inline void setAllocFailIndex(int index) - { - if (RUNNING_ON_VALGRIND) { - VALGRIND_ENABLE_OOM_AT_ALLOC_INDEX(VALGRIND_GET_ALLOC_INDEX + index + 1); - } else { - mallocFailIndex = index; - } - } - - inline void deactivate() - { - mallocFailActive = false; - VALGRIND_ENABLE_OOM_AT_ALLOC_INDEX(INT_MAX); - } - - inline int currentAllocIndex() const - { - if (RUNNING_ON_VALGRIND) { - return VALGRIND_GET_ALLOC_INDEX; - } else { - return mallocCount; - } - } -}; - void *my_malloc_hook(size_t size, const void *) { ++mallocCount; @@ -173,6 +158,98 @@ void my_free_hook(void *ptr, const void *) __free_hook = my_free_hook; } +#elif defined(Q_CC_MSVC) + +#include "crtdbg.h" + +static int qCrtAllocHook(int allocType, void * /*userData*/, size_t /*size*/, + int blockType, long /*requestNumber*/, + const unsigned char * /*filename*/, int /*lineNumber*/) +{ + if (blockType == _CRT_BLOCK) + return TRUE; // ignore allocations from the C library + + switch (allocType) { + case _HOOK_ALLOC: + case _HOOK_REALLOC: + ++mallocCount; + if (mallocFailActive && --mallocFailIndex < 0) + return FALSE; // simulate OOM + } + + return TRUE; +} + +static struct QCrtDebugRegistrator +{ + QCrtDebugRegistrator() + { + _CrtSetAllocHook(qCrtAllocHook); + } + +} crtDebugRegistrator; + +#endif + +struct AllocFailer +{ + inline AllocFailer(int index) { reactivateAt(index); } + inline ~AllocFailer() { deactivate(); } + + inline void reactivateAt(int index) + { +#ifdef RUNNING_ON_VALGRIND + if (RUNNING_ON_VALGRIND) + VALGRIND_ENABLE_OOM_AT_ALLOC_INDEX(VALGRIND_GET_ALLOC_INDEX + index + 1); +#elif defined(Q_OS_SYMBIAN) + // symbian alloc fail index is 1 based + __UHEAP_BURSTFAILNEXT(index+1, KMaxTUint16); +#endif + mallocFailIndex = index; + mallocFailActive = true; + } + + inline void deactivate() + { + mallocFailActive = false; +#ifdef RUNNING_ON_VALGRIND + VALGRIND_ENABLE_OOM_AT_ALLOC_INDEX(0); +#elif defined(Q_OS_SYMBIAN) + __UHEAP_RESET; +#endif + } + + inline int currentAllocIndex() const + { +#ifdef RUNNING_ON_VALGRIND + if (RUNNING_ON_VALGRIND) + return VALGRIND_GET_ALLOC_INDEX; +#endif + return mallocCount; + } + + static bool initialize() + { + std::set_terminate(my_terminate_handler); +#ifdef RUNNING_ON_VALGRIND + if (RUNNING_ON_VALGRIND) { + if (VALGRIND_GET_ALLOC_INDEX == -1u) { + qWarning("You must use a valgrind with oom simulation support"); + return false; + } + // running in valgrind - don't use glibc hooks + disableHooks(); + + // never stop simulating OOM + VALGRIND_DISABLE_OOM_AT_ALLOC_INDEX(-1u); + } +#endif + return true; + } +}; + +#ifndef Q_OS_SYMBIAN + static void *new_helper(std::size_t size) { void *ptr = malloc(size); @@ -181,6 +258,11 @@ static void *new_helper(std::size_t size) return ptr; } +#ifdef Q_CC_MSVC +# pragma warning(push) +# pragma warning(disable: 4290) +#endif + // overload operator new void* operator new(size_t size) throw (std::bad_alloc) { return new_helper(size); } void* operator new[](size_t size) throw (std::bad_alloc) { return new_helper(size); } @@ -193,6 +275,12 @@ void operator delete[](void *ptr) throw() { if (ptr) free(ptr); } void operator delete(void *ptr, const std::nothrow_t&) throw() { if (ptr) free(ptr); } void operator delete[](void *ptr, const std::nothrow_t&) throw() { if (ptr) free (ptr); } +#ifdef Q_CC_MSVC +# pragma warning(pop) +#endif + +#endif + // ignore placement new and placement delete - those don't allocate. |