--- 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-19 19:05:45.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 EXPECT(value, expected) __builtin_expect((value), (expected)) +#else +# define EXPECT(value, expected) (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,14 @@ poolp next; uint size; +#ifdef WITH_VALGRIND + if (EXPECT(running_on_valgrind == -1, 0)) { + running_on_valgrind = RUNNING_ON_VALGRIND; + } + if (EXPECT(running_on_valgrind, 0)) + goto redirect; +#endif + /* * This implicitly redirects malloc(0). */ @@ -916,6 +941,13 @@ if (p == NULL) /* free(NULL) has no effect */ return; +#ifdef WITH_VALGRIND + if (EXPECT(running_on_valgrind > 0, 0)) { + free(p); + return; + } +#endif + pool = POOL_ADDR(p); if (Py_ADDRESS_IN_RANGE(p, pool)) { /* We allocated this address. */