Vladimir Marangozov's long-awaited malloc restructuring.
For more comments, read the patches@python.org archives.
For documentation read the comments in mymalloc.h and objimpl.h.

(This is not exactly what Vladimir posted to the patches list; I've
made a few changes, and Vladimir sent me a fix in private email for a
problem that only occurs in debug mode.  I'm also holding back on his
change to main.c, which seems unnecessary to me.)
diff --git a/Parser/myreadline.c b/Parser/myreadline.c
index d626139..9bf770e 100644
--- a/Parser/myreadline.c
+++ b/Parser/myreadline.c
@@ -89,7 +89,7 @@
 	int n;
 	char *p;
 	n = 100;
-	if ((p = malloc(n)) == NULL)
+	if ((p = PyMem_MALLOC(n)) == NULL)
 		return NULL;
 	fflush(stdout);
 	if (prompt)
@@ -99,7 +99,7 @@
 	case 0: /* Normal case */
 		break;
 	case 1: /* Interrupt */
-		free(p);
+		PyMem_FREE(p);
 		return NULL;
 	case -1: /* EOF */
 	case -2: /* Error */
@@ -117,19 +117,21 @@
 	n = strlen(p);
 	while (n > 0 && p[n-1] != '\n') {
 		int incr = n+2;
-		p = realloc(p, n + incr);
+		p = PyMem_REALLOC(p, n + incr);
 		if (p == NULL)
 			return NULL;
 		if (my_fgets(p+n, incr, stdin) != 0)
 			break;
 		n += strlen(p+n);
 	}
-	return realloc(p, n+1);
+	return PyMem_REALLOC(p, n+1);
 }
 
 
 /* By initializing this function pointer, systems embedding Python can
-   override the readline function. */
+   override the readline function.
+
+   Note: Python expects in return a buffer allocated with PyMem_Malloc. */
 
 char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));