--- Python-2.5.2/Objects/obmalloc.c.old 2008-03-19 19:03:59.000000000 +0900 +++ Python-2.5.2/Objects/obmalloc.c 2008-03-20 12:26:44.000000000 +0900 @@ -2,6 +2,23 @@ #ifdef WITH_PYMALLOC +#define WITH_VALGRIND + +#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 +743,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 +940,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 +1139,7 @@ return; } +redirect: /* We didn't allocate this address. */ free(p); }