562f19e
Index: configure.in
562f19e
===================================================================
562f19e
--- configure.in	(revision 61828)
562f19e
+++ configure.in	(working copy)
562f19e
@@ -2232,6 +2232,19 @@ then
562f19e
 fi
562f19e
 AC_MSG_RESULT($with_pymalloc)
562f19e
 
562f19e
+# Check for Valgrind support
562f19e
+AC_MSG_CHECKING([for --with-valgrind])
562f19e
+AC_ARG_WITH([valgrind],
562f19e
+  AC_HELP_STRING([--with-valgrind], [Enable Valgrind support]),,
562f19e
+  with_valgrind=no)
562f19e
+AC_MSG_RESULT([$with_valgrind])
562f19e
+if test "$with_valgrind" != no; then
562f19e
+    AC_CHECK_HEADER([valgrind/valgrind.h],
562f19e
+      [AC_DEFINE([WITH_VALGRIND], 1, [Define if you want pymalloc to be disabled when running under valgrind])],
562f19e
+      [AC_MSG_ERROR([Valgrind support requested but headers not available])]
562f19e
+    )
562f19e
+fi
562f19e
+
562f19e
 # Check for --with-wctype-functions
562f19e
 AC_MSG_CHECKING(for --with-wctype-functions)
562f19e
 AC_ARG_WITH(wctype-functions, 
562f19e
Index: Objects/obmalloc.c
562f19e
===================================================================
562f19e
--- Objects/obmalloc.c	(revision 61828)
562f19e
+++ Objects/obmalloc.c	(working copy)
562f19e
@@ -2,6 +2,21 @@
562f19e
 
562f19e
 #ifdef WITH_PYMALLOC
562f19e
 
562f19e
+#ifdef WITH_VALGRIND
562f19e
+#include <valgrind/valgrind.h>
562f19e
+
562f19e
+/* If we're using GCC, use __builtin_expect() to reduce overhead of
562f19e
+   the valgrind checks */
562f19e
+#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
562f19e
+#  define UNLIKELY(value) __builtin_expect((value), 0)
562f19e
+#else
562f19e
+#  define UNLIKELY(value) (value)
562f19e
+#endif
562f19e
+
562f19e
+/* -1 indicates that we haven't checked that we're running on valgrind yet. */
562f19e
+static int running_on_valgrind = -1;
562f19e
+#endif
562f19e
+
562f19e
 /* An object allocator for Python.
562f19e
 
562f19e
    Here is an introduction to the layers of the Python memory architecture,
562f19e
@@ -726,6 +741,13 @@ PyObject_Malloc(size_t nbytes)
562f19e
 	poolp next;
562f19e
 	uint size;
562f19e
 
562f19e
+#ifdef WITH_VALGRIND
562f19e
+	if (UNLIKELY(running_on_valgrind == -1))
562f19e
+		running_on_valgrind = RUNNING_ON_VALGRIND;
562f19e
+	if (UNLIKELY(running_on_valgrind))
562f19e
+		goto redirect;
562f19e
+#endif
562f19e
+
562f19e
 	/*
562f19e
 	 * This implicitly redirects malloc(0).
562f19e
 	 */
562f19e
@@ -916,6 +938,11 @@ PyObject_Free(void *p)
562f19e
 	if (p == NULL)	/* free(NULL) has no effect */
562f19e
 		return;
562f19e
 
562f19e
+#ifdef WITH_VALGRIND
562f19e
+	if (UNLIKELY(running_on_valgrind > 0))
562f19e
+		goto redirect;
562f19e
+#endif
562f19e
+
562f19e
 	pool = POOL_ADDR(p);
562f19e
 	if (Py_ADDRESS_IN_RANGE(p, pool)) {
562f19e
 		/* We allocated this address. */
562f19e
@@ -1110,6 +1137,7 @@ PyObject_Free(void *p)
562f19e
 		return;
562f19e
 	}
562f19e
 
562f19e
+redirect:
562f19e
 	/* We didn't allocate this address. */
562f19e
 	free(p);
562f19e
 }
562f19e
@@ -1130,6 +1158,12 @@ PyObject_Realloc(void *p, size_t nbytes)
562f19e
 	if (p == NULL)
562f19e
 		return PyObject_Malloc(nbytes);
562f19e
 
562f19e
+#ifdef WITH_VALGRIND
562f19e
+	/* Treat running_on_valgrind == -1 the same as 0 */
562f19e
+	if (UNLIKELY(running_on_valgrind > 0))
562f19e
+		goto redirect;
562f19e
+#endif
562f19e
+
562f19e
 	pool = POOL_ADDR(p);
562f19e
 	if (Py_ADDRESS_IN_RANGE(p, pool)) {
562f19e
 		/* We're in charge of this block */
562f19e
@@ -1157,6 +1191,7 @@ PyObject_Realloc(void *p, size_t nbytes)
562f19e
 		}
562f19e
 		return bp;
562f19e
 	}
562f19e
+ redirect:
562f19e
 	/* We're not managing this block.  If nbytes <=
562f19e
 	 * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this
562f19e
 	 * block.  However, if we do, we need to copy the valid data from
562f19e
Index: Misc/NEWS
562f19e
===================================================================
562f19e
--- Misc/NEWS	(revision 61828)
562f19e
+++ Misc/NEWS	(working copy)
562f19e
@@ -60,6 +60,11 @@ Core and builtins
562f19e
 
562f19e
 - Issue #2143: Fix embedded readline() hang on SSL socket EOF.
562f19e
 
562f19e
+- Issue #2422: When compiled with the ``--with-valgrind`` option, the
562f19e
+  pymalloc allocator will be automatically disabled when running under
562f19e
+  Valgrind.  This gives improved memory leak detection when running
562f19e
+  under Valgrind, while taking advantage of pymalloc at other times.
562f19e
+
562f19e
 Library
562f19e
 -------
562f19e
 
562f19e
Index: pyconfig.h.in
562f19e
===================================================================
562f19e
--- pyconfig.h.in	(revision 61828)
562f19e
+++ pyconfig.h.in	(working copy)
562f19e
@@ -958,6 +958,9 @@
562f19e
 /* Define to profile with the Pentium timestamp counter */
562f19e
 #undef WITH_TSC
562f19e
 
562f19e
+/* Define if you want pymalloc to be disabled when running under valgrind */
562f19e
+#undef WITH_VALGRIND
562f19e
+
562f19e
 
562f19e
  /* Define to 1 if your processor stores words with the most significant byte
562f19e
     first (like Motorola and SPARC, unlike Intel and VAX).