Index: Objects/obmalloc.c =================================================================== --- Objects/obmalloc.c (revision 61828) +++ Objects/obmalloc.c (working copy) @@ -2,6 +2,21 @@ #ifdef WITH_PYMALLOC +#ifdef WITH_VALGRIND +#include + +/* If we're using GCC, use __builtin_expect() to reduce overhead of + the valgrind checks */ +#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) +# define UNLIKELY(value) __builtin_expect((value), 0) +#else +# define UNLIKELY(value) (value) +#endif + +/* -1 indicates that we haven't checked that we're running on valgrind yet. */ +static int running_on_valgrind = -1; +#endif + /* An object allocator for Python. Here is an introduction to the layers of the Python memory architecture, @@ -726,6 +741,13 @@ poolp next; uint size; +#ifdef WITH_VALGRIND + if (UNLIKELY(running_on_valgrind == -1)) + running_on_valgrind = RUNNING_ON_VALGRIND; + if (UNLIKELY(running_on_valgrind)) + goto redirect; +#endif + /* * This implicitly redirects malloc(0). */ @@ -916,6 +938,11 @@ if (p == NULL) /* free(NULL) has no effect */ return; +#ifdef WITH_VALGRIND + if (UNLIKELY(running_on_valgrind > 0)) + goto redirect; +#endif + pool = POOL_ADDR(p); if (Py_ADDRESS_IN_RANGE(p, pool)) { /* We allocated this address. */ @@ -1110,6 +1137,7 @@ return; } +redirect: /* We didn't allocate this address. */ free(p); } Index: configure.in =================================================================== --- configure.in (revision 61828) +++ configure.in (working copy) @@ -2232,6 +2232,19 @@ fi AC_MSG_RESULT($with_pymalloc) +# Check for --with-valgrind +AC_MSG_CHECKING(for --with-valgrind) +AC_ARG_WITH(valgrind, +[ --with(out)-valgrind enable/disable valgrind support], [ +if test "$withval" != no +then + AC_DEFINE(WITH_VALGRIND, 1, + [Define if you want to compile with valgrind support]) + AC_MSG_RESULT(yes) +else AC_MSG_RESULT(no) +fi], +[AC_MSG_RESULT(no)]) + # Check for --with-wctype-functions AC_MSG_CHECKING(for --with-wctype-functions) AC_ARG_WITH(wctype-functions,