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/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index f76bf03..d654b29 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -273,7 +273,7 @@
 	PyFileObject *in_fo;
 	PyFileObject *out_fo;
 	PyCursesScreenObject *xp;
-	xp = PyObject_NEW(PyCursesScreenObject, &PyCursesScreen_Type);
+	xp = PyObject_New(PyCursesScreenObject, &PyCursesScreen_Type);
 	if (xp == NULL)
 		return NULL;
 	return (PyObject *)xp;
@@ -289,7 +289,7 @@
 {
 	PyCursesWindowObject *wo;
 
-	wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
+	wo = PyObject_New(PyCursesWindowObject, &PyCursesWindow_Type);
 	if (wo == NULL)
 		return NULL;
 	wo->win = win;
@@ -303,7 +303,7 @@
 {
   if (wo->win != stdscr)
     delwin(wo->win);
-  PyMem_DEL(wo);
+  PyObject_Del(wo);
 }
 
 static PyObject *
@@ -1125,7 +1125,7 @@
 	WINDOW *pad;
 {
 	PyCursesPadObject *po;
-	po = PyObject_NEW(PyCursesPadObject, &PyCursesPad_Type);
+	po = PyObject_New(PyCursesPadObject, &PyCursesPad_Type);
 	if (po == NULL)
 		return NULL;
 	po->pad = pad;
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 9eec035..47b80c5 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -848,7 +848,7 @@
                           &PyString_Type, &code, &groups, &groupindex))
 		return NULL;
 
-	self = PyObject_NEW(PatternObject, &Pattern_Type);
+	self = PyObject_New(PatternObject, &Pattern_Type);
 	if (self == NULL)
 		return NULL;
 
@@ -886,7 +886,7 @@
 	if (status > 0) {
 
 		/* create match object (with room for extra group marks) */
-		match = PyObject_NEW_VAR(MatchObject, &Match_Type, 2*pattern->groups);
+		match = PyObject_NewVar(MatchObject, &Match_Type, 2*pattern->groups);
 		if (match == NULL)
 			return NULL;
 
@@ -1002,7 +1002,7 @@
 	Py_XDECREF(self->code);
 	Py_XDECREF(self->pattern);
 	Py_XDECREF(self->groupindex);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject*
@@ -1163,7 +1163,7 @@
 {
 	Py_XDECREF(self->string);
 	Py_DECREF(self->pattern);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject*
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 008378d..15cc7e7 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -469,7 +469,7 @@
 	TkappObject *v;
 	char *argv0;
   
-	v = PyObject_NEW(TkappObject, &Tkapp_Type);
+	v = PyObject_New(TkappObject, &Tkapp_Type);
 	if (v == NULL)
 		return NULL;
 
@@ -1640,7 +1640,7 @@
 {
 	TkttObject *v;
   
-	v = PyObject_NEW(TkttObject, &Tktt_Type);
+	v = PyObject_New(TkttObject, &Tktt_Type);
 	if (v == NULL)
 		return NULL;
 
@@ -1662,7 +1662,7 @@
 
 	Py_XDECREF(func);
 
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -1910,7 +1910,7 @@
 	ENTER_TCL
 	Tcl_DeleteInterp(Tkapp_Interp(self));
 	LEAVE_TCL
-	PyMem_DEL(self);
+	PyObject_Del(self);
 	DisableEventHook();
 }
 
diff --git a/Modules/almodule.c b/Modules/almodule.c
index 1033c07..c75fe89 100644
--- a/Modules/almodule.c
+++ b/Modules/almodule.c
@@ -648,7 +648,7 @@
 {
 	alcobject *self;
 	
-	self = PyObject_NEW(alcobject, &Alctype);
+	self = PyObject_New(alcobject, &Alctype);
 	if (self == NULL)
 		return NULL;
 	/* XXXX Add your own initializers here */
@@ -667,7 +667,7 @@
 #else
 	(void) ALfreeconfig(self->config);	/* ignore errors */
 #endif
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -1421,7 +1421,7 @@
 {
 	alpobject *self;
 	
-	self = PyObject_NEW(alpobject, &Alptype);
+	self = PyObject_New(alpobject, &Alptype);
 	if (self == NULL)
 		return NULL;
 	/* XXXX Add your own initializers here */
@@ -1442,7 +1442,7 @@
 		ALcloseport(self->port);
 #endif
 	}
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index dcd931b..9a09c7d 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -346,7 +346,7 @@
 	if (nbytes / descr->itemsize != (size_t)size) {
 		return PyErr_NoMemory();
 	}
-	op = PyMem_NEW(arrayobject, 1);
+	op = PyObject_NewVar(arrayobject, &Arraytype, size);
 	if (op == NULL) {
 		return PyErr_NoMemory();
 	}
@@ -356,14 +356,11 @@
 	else {
 		op->ob_item = PyMem_NEW(char, nbytes);
 		if (op->ob_item == NULL) {
-			PyMem_DEL(op);
+			PyObject_Del(op);
 			return PyErr_NoMemory();
 		}
 	}
-	op->ob_type = &Arraytype;
-	op->ob_size = size;
 	op->ob_descr = descr;
-	_Py_NewReference((PyObject *)op);
 	return (PyObject *) op;
 }
 
@@ -466,7 +463,7 @@
 {
 	if (op->ob_item != NULL)
 		PyMem_DEL(op->ob_item);
-	PyMem_DEL(op);
+	PyObject_Del(op);
 }
 
 static int
diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c
index 16178ed..97a8e8b 100644
--- a/Modules/bsddbmodule.c
+++ b/Modules/bsddbmodule.c
@@ -89,7 +89,7 @@
 	bsddbobject *dp;
 	HASHINFO info;
 
-	if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
+	if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
 		return NULL;
 
 	info.bsize = bsize;
@@ -143,7 +143,7 @@
 	bsddbobject *dp;
 	BTREEINFO info;
 
-	if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
+	if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
 		return NULL;
 
 	info.flags = btflags;
@@ -200,7 +200,7 @@
 	bsddbobject *dp;
 	RECNOINFO info;
 
-	if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
+	if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
 		return NULL;
 
 	info.flags = rnflags;
@@ -261,7 +261,7 @@
 				"Python bsddb: close errno %d in dealloc\n",
 				errno);
 	}
-	PyMem_DEL(dp);
+	PyObject_Del(dp);
 }
 
 #ifdef WITH_THREAD
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index aa2c7cb..73cb6ba 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -171,7 +171,7 @@
 
     if (self->data) free(self->data);
 
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static PyTypeObject PdataType = {
@@ -186,7 +186,7 @@
 Pdata_New() {
     Pdata *self;
 
-    UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
+    UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
     self->size=8;
     self->length=0;
     self->data=malloc(self->size * sizeof(PyObject*));
@@ -2132,7 +2132,7 @@
 newPicklerobject(PyObject *file, int bin) {
     Picklerobject *self;
 
-    UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
+    UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
         return NULL;
 
     self->fp = NULL;
@@ -2243,7 +2243,7 @@
         free(self->write_buf);
     }
 
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 
@@ -2885,7 +2885,7 @@
               PyInstanceObject *inst;
 
               PyErr_Clear();
-              UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
+              UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
                 goto err;
               inst->in_class=(PyClassObject*)cls;
               Py_INCREF(cls);
@@ -4036,7 +4036,7 @@
 newUnpicklerobject(PyObject *f) {
     Unpicklerobject *self;
 
-    UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
+    UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
         return NULL;
 
     self->file = NULL;
@@ -4141,7 +4141,7 @@
         free(self->buf);
     }
     
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 
diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c
index e816178..557545f 100644
--- a/Modules/cStringIO.c
+++ b/Modules/cStringIO.c
@@ -56,7 +56,7 @@
 "\n"
 "This module provides a simple useful replacement for\n"
 "the StringIO module that is written in C.  It does not provide the\n"
-"full generality if StringIO, but it provides anough for most\n"
+"full generality if StringIO, but it provides enough for most\n"
 "applications and is especially useful in conjuction with the\n"
 "pickle module.\n"
 "\n"
@@ -407,7 +407,7 @@
 O_dealloc(Oobject *self) {
   if (self->buf != NULL)
     free(self->buf);
-  PyMem_DEL(self);
+  PyObject_Del(self);
 }
 
 static PyObject *
@@ -465,7 +465,7 @@
 newOobject(int  size) {
   Oobject *self;
 	
-  self = PyObject_NEW(Oobject, &Otype);
+  self = PyObject_New(Oobject, &Otype);
   if (self == NULL)
     return NULL;
   self->pos=0;
@@ -536,7 +536,7 @@
 static void
 I_dealloc(Iobject *self) {
   Py_XDECREF(self->pbuf);
-  PyMem_DEL(self);
+  PyObject_Del(self);
 }
 
 static PyObject *
@@ -586,7 +586,7 @@
   }
   buf = PyString_AS_STRING(s);
   size = PyString_GET_SIZE(s);
-  UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
+  UNLESS(self = PyObject_New(Iobject, &Itype)) return NULL;
   Py_INCREF(s);
   self->buf=buf;
   self->string_size=size;
diff --git a/Modules/cdmodule.c b/Modules/cdmodule.c
index 1b414c1..4a04e43 100644
--- a/Modules/cdmodule.c
+++ b/Modules/cdmodule.c
@@ -447,7 +447,7 @@
 {
 	if (self->ob_cdplayer != NULL)
 		CDclose(self->ob_cdplayer);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -483,7 +483,7 @@
 {
 	cdplayerobject *p;
 
-	p = PyObject_NEW(cdplayerobject, &CdPlayertype);
+	p = PyObject_New(cdplayerobject, &CdPlayertype);
 	if (p == NULL)
 		return NULL;
 	p->ob_cdplayer = cdp;
@@ -761,7 +761,7 @@
 		self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
 	}
 	CDdeleteparser(self->ob_cdparser);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -799,7 +799,7 @@
 	cdparserobject *p;
 	int i;
 
-	p = PyObject_NEW(cdparserobject, &CdParsertype);
+	p = PyObject_New(cdparserobject, &CdParsertype);
 	if (p == NULL)
 		return NULL;
 	p->ob_cdparser = cdp;
diff --git a/Modules/clmodule.c b/Modules/clmodule.c
index 05b00da..976346a 100644
--- a/Modules/clmodule.c
+++ b/Modules/clmodule.c
@@ -673,7 +673,7 @@
 		else
 			clCloseDecompressor(SELF->ob_compressorHdl);
 	}
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -713,7 +713,7 @@
 	if (!PyArg_Parse(args, "i", &scheme))
 		return NULL;
 
-	new = PyObject_NEW(clobject, &Cltype);
+	new = PyObject_New(clobject, &Cltype);
 	if (new == NULL)
 		return NULL;
 
diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c
index 501c3f4..d4fc1bf 100644
--- a/Modules/dbmmodule.c
+++ b/Modules/dbmmodule.c
@@ -62,7 +62,7 @@
 {
         dbmobject *dp;
 
-	dp = PyObject_NEW(dbmobject, &Dbmtype);
+	dp = PyObject_New(dbmobject, &Dbmtype);
 	if (dp == NULL)
 		return NULL;
 	dp->di_size = -1;
@@ -82,7 +82,7 @@
 {
         if ( dp->di_dbm )
 		dbm_close(dp->di_dbm);
-	PyMem_DEL(dp);
+	PyObject_Del(dp);
 }
 
 static int
diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c
index 76d0540..b40e05e 100644
--- a/Modules/dlmodule.c
+++ b/Modules/dlmodule.c
@@ -54,7 +54,7 @@
 	PyUnivPtr *handle;
 {
 	dlobject *xp;
-	xp = PyObject_NEW(dlobject, &Dltype);
+	xp = PyObject_New(dlobject, &Dltype);
 	if (xp == NULL)
 		return NULL;
 	xp->dl_handle = handle;
@@ -67,7 +67,7 @@
 {
 	if (xp->dl_handle != NULL)
 		dlclose(xp->dl_handle);
-	PyMem_DEL(xp);
+	PyObject_Del(xp);
 }
 
 static PyObject *
diff --git a/Modules/flmodule.c b/Modules/flmodule.c
index c921d4b..c6ce439 100644
--- a/Modules/flmodule.c
+++ b/Modules/flmodule.c
@@ -332,7 +332,7 @@
 	fl_free_object(g->ob_generic);
 	Py_XDECREF(g->ob_callback);
 	Py_XDECREF(g->ob_callback_arg);
-	PyMem_DEL(g);
+	PyObject_Del(g);
 }
 
 #define OFF(x) offsetof(FL_OBJECT, x)
@@ -461,7 +461,7 @@
 	PyMethodDef *methods;
 {
 	genericobject *g;
-	g = PyObject_NEW(genericobject, &GenericObjecttype);
+	g = PyObject_New(genericobject, &GenericObjecttype);
 	if (g == NULL)
 		return NULL;
 	g-> ob_generic = generic;
@@ -1852,7 +1852,7 @@
 	if (f->ob_form->visible)
 		fl_hide_form(f->ob_form);
 	fl_free_form(f->ob_form);
-	PyMem_DEL(f);
+	PyObject_Del(f);
 }
 
 #define OFF(x) offsetof(FL_FORM, x)
@@ -1931,7 +1931,7 @@
 	FL_FORM *form;
 {
 	formobject *f;
-	f = PyObject_NEW(formobject, &Formtype);
+	f = PyObject_New(formobject, &Formtype);
 	if (f == NULL)
 		return NULL;
 	f->ob_form = form;
diff --git a/Modules/fmmodule.c b/Modules/fmmodule.c
index dd5b397..21f08d8 100644
--- a/Modules/fmmodule.c
+++ b/Modules/fmmodule.c
@@ -59,7 +59,7 @@
 				"error creating new font handle");
 		return NULL;
 	}
-	fhp = PyObject_NEW(fhobject, &Fhtype);
+	fhp = PyObject_New(fhobject, &Fhtype);
 	if (fhp == NULL)
 		return NULL;
 	fhp->fh_fh = fh;
@@ -196,7 +196,7 @@
 	fhobject *fhp;
 {
 	fmfreefont(fhp->fh_fh);
-	PyMem_DEL(fhp);
+	PyObject_Del(fhp);
 }
 
 static PyTypeObject Fhtype = {
diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c
index 96b5866..066cf3b 100644
--- a/Modules/gdbmmodule.c
+++ b/Modules/gdbmmodule.c
@@ -93,7 +93,7 @@
 {
         dbmobject *dp;
 
-	dp = PyObject_NEW(dbmobject, &Dbmtype);
+	dp = PyObject_New(dbmobject, &Dbmtype);
 	if (dp == NULL)
 		return NULL;
 	dp->di_size = -1;
@@ -117,7 +117,7 @@
 {
         if ( dp->di_dbm )
 		gdbm_close(dp->di_dbm);
-	PyMem_DEL(dp);
+	PyObject_Del(dp);
 }
 
 static int
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 78b4915..78568137 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -529,7 +529,7 @@
 	bufsz += strlen(exec_prefix) + 1;
 
 	/* This is the only malloc call in this file */
-	buf = malloc(bufsz);
+	buf = PyMem_Malloc(bufsz);
 
 	if (buf == NULL) {
 		/* We can't exit, so print a warning and limp along */
diff --git a/Modules/linuxaudiodev.c b/Modules/linuxaudiodev.c
index 393df05..aac742a 100644
--- a/Modules/linuxaudiodev.c
+++ b/Modules/linuxaudiodev.c
@@ -104,7 +104,7 @@
   }
 
   /* Create and initialize the object */
-  if ((xp = PyObject_NEW(lad_t, &Ladtype)) == NULL) {
+  if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
     close(fd);
     return NULL;
   }
@@ -118,7 +118,7 @@
 lad_dealloc(lad_t *xp)
 {
   close(xp->x_fd);
-  PyMem_DEL(xp);
+  PyObject_Del(xp);
 }
 
 static PyObject *
diff --git a/Modules/md5module.c b/Modules/md5module.c
index 3c001ae..6856c68 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -56,7 +56,7 @@
 {
 	md5object *md5p;
 
-	md5p = PyObject_NEW(md5object, &MD5type);
+	md5p = PyObject_New(md5object, &MD5type);
 	if (md5p == NULL)
 		return NULL;
 
@@ -71,7 +71,7 @@
 md5_dealloc(md5p)
 	md5object *md5p;
 {
-	PyMem_DEL(md5p);
+	PyObject_Del(md5p);
 }
 
 
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index a79812d..fcbb484 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -69,7 +69,7 @@
 	}
 #endif /* UNIX */
 
-	PyMem_DEL(m_obj);
+	PyObject_Del(m_obj);
 }
 
 static PyObject *
@@ -706,7 +706,7 @@
 		)
 		return NULL;
   
-	m_obj = PyObject_NEW (mmap_object, &mmap_object_type);
+	m_obj = PyObject_New (mmap_object, &mmap_object_type);
 	if (m_obj == NULL) {return NULL;}
 	m_obj->size = (size_t) map_size;
 	m_obj->pos = (size_t) 0;
@@ -757,7 +757,7 @@
 		fseek(&_iob[fileno], 0, SEEK_SET);
 	}
 
-	m_obj = PyObject_NEW (mmap_object, &mmap_object_type);
+	m_obj = PyObject_New (mmap_object, &mmap_object_type);
     
 	if (fh) {
 		m_obj->file_handle = fh;
diff --git a/Modules/mpzmodule.c b/Modules/mpzmodule.c
index e701d85..1aaf787 100644
--- a/Modules/mpzmodule.c
+++ b/Modules/mpzmodule.c
@@ -123,7 +123,7 @@
 #ifdef MPZ_DEBUG
 	fputs( "mpz_object() called...\n", stderr );
 #endif /* def MPZ_DEBUG */
-	mpzp = PyObject_NEW(mpzobject, &MPZtype);
+	mpzp = PyObject_New(mpzobject, &MPZtype);
 	if (mpzp == NULL)
 		return NULL;
 
@@ -285,7 +285,7 @@
 	fputs( "mpz_dealloc() called...\n", stderr );
 #endif /* def MPZ_DEBUG */
 	mpz_clear(&mpzp->mpz);
-	PyMem_DEL(mpzp);
+	PyObject_Del(mpzp);
 } /* mpz_dealloc() */
 
 
diff --git a/Modules/newmodule.c b/Modules/newmodule.c
index 2758f14..fa0dc8b 100644
--- a/Modules/newmodule.c
+++ b/Modules/newmodule.c
@@ -49,7 +49,7 @@
 			      &PyClass_Type, &klass,
 			      &PyDict_Type, &dict))
 		return NULL;
-	inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
+	inst = PyObject_New(PyInstanceObject, &PyInstance_Type);
 	if (inst == NULL)
 		return NULL;
 	Py_INCREF(klass);
diff --git a/Modules/nismodule.c b/Modules/nismodule.c
index 04f27f2..8943729 100644
--- a/Modules/nismodule.c
+++ b/Modules/nismodule.c
@@ -353,11 +353,11 @@
 	if (list->stat != NIS_TRUE)
 		goto finally;
 
-	PyMem_DEL(server);
+	free(server);
 	return list->maps;
 
   finally:
-	PyMem_DEL(server);
+	free(server);
 	return NULL;
 }
 
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 6a8d38c..9b9baf0 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -295,7 +295,7 @@
 static PyObject*
 parser_newastobject(node *ast, int type)
 {
-    PyAST_Object* o = PyObject_NEW(PyAST_Object, &PyAST_Type);
+    PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
 
     if (o != 0) {
         o->ast_node = ast;
@@ -317,7 +317,7 @@
 parser_free(PyAST_Object *ast)
 {
     PyNode_Free(ast->ast_node);
-    PyMem_DEL(ast);
+    PyObject_Del(ast);
 }
 
 
@@ -790,10 +790,10 @@
                 PyObject *temp = PySequence_GetItem(elem, 1);
 
                 /* check_terminal_tuple() already verified it's a string */
-                strn = (char *)malloc(PyString_GET_SIZE(temp) + 1);
+                strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
                 if (strn != NULL)
                     (void) strcpy(strn, PyString_AS_STRING(temp));
-                Py_XDECREF(temp);
+                Py_DECREF(temp);
 
                 if (PyObject_Length(elem) == 3) {
                     PyObject* temp = PySequence_GetItem(elem, 2);
diff --git a/Modules/pcremodule.c b/Modules/pcremodule.c
index 4d2aa72..6b9f960 100644
--- a/Modules/pcremodule.c
+++ b/Modules/pcremodule.c
@@ -79,7 +79,7 @@
 	PyObject *arg;
 {
 	PcreObject *self;
-	self = PyObject_NEW(PcreObject, &Pcre_Type);
+	self = PyObject_New(PcreObject, &Pcre_Type);
 	if (self == NULL)
 		return NULL;
 	self->regex = NULL;
@@ -95,7 +95,7 @@
 {
 	if (self->regex) (pcre_free)(self->regex);
 	if (self->regex_extra) (pcre_free)(self->regex_extra);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index e98f393..f8b3bce 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -471,7 +471,7 @@
         int i;
         xmlparseobject *self;
         
-        self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
+        self = PyObject_New(xmlparseobject, &Xmlparsetype);
         if (self == NULL)
                 return NULL;
 
@@ -512,7 +512,7 @@
         for( i=0; handler_info[i].name!=NULL; i++ ){
                 Py_XDECREF( self->handlers[i] );
         }
-        PyMem_DEL(self);
+        PyObject_Del(self);
 }
 
 static int handlername2int( const char *name ){
diff --git a/Modules/readline.c b/Modules/readline.c
index d4ba3f8..37baf8c 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -377,7 +377,7 @@
 	char *prompt;
 {
 	int n;
-	char *p;
+	char *p, *q;
 	RETSIGTYPE (*old_inthandler)();
 	old_inthandler = signal(SIGINT, onintr);
 	if (setjmp(jbuf)) {
@@ -391,8 +391,10 @@
 	rl_event_hook = PyOS_InputHook;
 	p = readline(prompt);
 	signal(SIGINT, old_inthandler);
+
+	/* We must return a buffer allocated with PyMem_Malloc. */
 	if (p == NULL) {
-		p = malloc(1);
+		p = PyMem_Malloc(1);
 		if (p != NULL)
 			*p = '\0';
 		return p;
@@ -400,10 +402,16 @@
 	n = strlen(p);
 	if (n > 0)
 		add_history(p);
-	if ((p = realloc(p, n+2)) != NULL) {
+	/* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
+	   release the original. */
+	q = p;
+	p = PyMem_Malloc(n+2);
+	if (p != NULL) {
+		strncpy(p, q, n);
 		p[n] = '\n';
 		p[n+1] = '\0';
 	}
+	free(q);
 	return p;
 }
 
diff --git a/Modules/regexmodule.c b/Modules/regexmodule.c
index 4fbf5c8..92d3b71 100644
--- a/Modules/regexmodule.c
+++ b/Modules/regexmodule.c
@@ -64,13 +64,14 @@
 reg_dealloc(re)
 	regexobject *re;
 {
-	PyMem_XDEL(re->re_patbuf.buffer);
+	if (re->re_patbuf.buffer)
+		PyMem_DEL(re->re_patbuf.buffer);
 	Py_XDECREF(re->re_translate);
 	Py_XDECREF(re->re_lastok);
 	Py_XDECREF(re->re_groupindex);
 	Py_XDECREF(re->re_givenpat);
 	Py_XDECREF(re->re_realpat);
-	PyMem_DEL(re);
+	PyObject_Del(re);
 }
 
 static PyObject *
@@ -418,7 +419,7 @@
 				"translation table must be 256 bytes");
 		return NULL;
 	}
-	re = PyObject_NEW(regexobject, &Regextype);
+	re = PyObject_New(regexobject, &Regextype);
 	if (re != NULL) {
 		char *error;
 		re->re_patbuf.buffer = NULL;
diff --git a/Modules/rotormodule.c b/Modules/rotormodule.c
index b1fef39..3d570f7 100644
--- a/Modules/rotormodule.c
+++ b/Modules/rotormodule.c
@@ -178,7 +178,7 @@
 {
 	Rotorobj *xp;
 
-	xp = PyObject_NEW(Rotorobj, &Rotor_Type);
+	xp = PyObject_New(Rotorobj, &Rotor_Type);
 	if (xp == NULL)
 		return NULL;
 	set_key(xp, key);
@@ -204,10 +204,14 @@
 	return xp;
 
   finally:
-	PyMem_XDEL(xp->e_rotor);
-	PyMem_XDEL(xp->d_rotor);
-	PyMem_XDEL(xp->positions);
-	PyMem_XDEL(xp->advances);
+	if (xp->e_rotor)
+		PyMem_DEL(xp->e_rotor);
+	if (xp->d_rotor)
+		PyMem_DEL(xp->d_rotor);
+	if (xp->positions)
+		PyMem_DEL(xp->positions);
+	if (xp->advances)
+		PyMem_DEL(xp->advances);
 	Py_DECREF(xp);
 	return (Rotorobj*)PyErr_NoMemory();
 }
@@ -473,11 +477,15 @@
 rotor_dealloc(xp)
 	Rotorobj *xp;
 {
-	PyMem_XDEL(xp->e_rotor);
-	PyMem_XDEL(xp->d_rotor);
-	PyMem_XDEL(xp->positions);
-	PyMem_XDEL(xp->advances);
-	PyMem_DEL(xp);
+	if (xp->e_rotor)
+		PyMem_DEL(xp->e_rotor);
+	if (xp->d_rotor)
+		PyMem_DEL(xp->d_rotor);
+	if (xp->positions)
+		PyMem_DEL(xp->positions);
+	if (xp->advances)
+		PyMem_DEL(xp->advances);
+	PyObject_Del(xp);
 }
 
 static PyObject * 
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index b48eb54..fb46fc3 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -278,9 +278,9 @@
 	wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
 	efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
 	if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
-		PyMem_XDEL(rfd2obj);
-		PyMem_XDEL(wfd2obj);
-		PyMem_XDEL(efd2obj);
+		if (rfd2obj) PyMem_DEL(rfd2obj);
+		if (wfd2obj) PyMem_DEL(wfd2obj);
+		if (efd2obj) PyMem_DEL(efd2obj);
 		return NULL;
 	}
 #endif
diff --git a/Modules/shamodule.c b/Modules/shamodule.c
index 0504fad..d56e90e 100644
--- a/Modules/shamodule.c
+++ b/Modules/shamodule.c
@@ -380,7 +380,7 @@
 static SHAobject *
 newSHAobject()
 {
-	return (SHAobject *)PyObject_NEW(SHAobject, &SHAtype);
+	return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
 }
 
 /* Internal methods for a hashing object */
@@ -389,7 +389,7 @@
 SHA_dealloc(ptr)
 	PyObject *ptr;
 {
-	PyMem_DEL(ptr);
+	PyObject_Del(ptr);
 }
 
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 939a495..56f1498 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -389,7 +389,7 @@
 {
 	PySocketSockObject *s;
 	PySocketSock_Type.ob_type = &PyType_Type;
-	s = PyObject_NEW(PySocketSockObject, &PySocketSock_Type);
+	s = PyObject_New(PySocketSockObject, &PySocketSock_Type);
 	if (s != NULL) {
 		s->sock_fd = fd;
 		s->sock_family = family;
@@ -1368,7 +1368,7 @@
 {
 	if (s->sock_fd != -1)
 		(void) SOCKETCLOSE(s->sock_fd);
-	PyMem_DEL(s);
+	PyObject_Del(s);
 }
 
 
@@ -1948,7 +1948,7 @@
 	meth=SSLv2_client_method();
 #endif
 
-	self = PyObject_NEW(SSLObject, &SSL_Type); /* Create new object */
+	self = PyObject_New(SSLObject, &SSL_Type); /* Create new object */
 	if (self == NULL){
 		PyErr_SetObject(SSLErrorObject,
 				PyString_FromString("newSSLObject error"));
@@ -1962,7 +1962,7 @@
 	if (self->ctx == NULL) {
 		PyErr_SetObject(SSLErrorObject,
 				PyString_FromString("SSL_CTX_new error"));
-		PyMem_DEL(self);
+		PyObject_Del(self);
 		return NULL;
 	}
 
@@ -1971,7 +1971,7 @@
 		PyErr_SetObject(SSLErrorObject,
 		      PyString_FromString(
 			"Both the key & certificate files must be specified"));
-		PyMem_DEL(self);
+		PyObject_Del(self);
 		return NULL;
 	}
 
@@ -1983,7 +1983,7 @@
 			PyErr_SetObject(SSLErrorObject,
 				PyString_FromString(
 				  "SSL_CTX_use_PrivateKey_file error"));
-			PyMem_DEL(self);
+			PyObject_Del(self);
 			return NULL;
 		}
 
@@ -1993,7 +1993,7 @@
 			PyErr_SetObject(SSLErrorObject,
 				PyString_FromString(
 				  "SSL_CTX_use_certificate_chain_file error"));
-			PyMem_DEL(self);
+			PyObject_Del(self);
 			return NULL;
 		}
 	}
@@ -2008,7 +2008,7 @@
 		/* Actually negotiate SSL connection */
 		PyErr_SetObject(SSLErrorObject,
 				PyString_FromString("SSL_connect error"));
-		PyMem_DEL(self);
+		PyObject_Del(self);
 		return NULL;
 	}
 	self->ssl->debug = 1;
@@ -2079,7 +2079,7 @@
 	SSL_free(self->ssl);
 	Py_XDECREF(self->x_attr);
 	Py_XDECREF(self->Socket);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *SSL_getattr(SSLObject *self, char *name)
diff --git a/Modules/stropmodule.c b/Modules/stropmodule.c
index 4c9ee76..2e243a4 100644
--- a/Modules/stropmodule.c
+++ b/Modules/stropmodule.c
@@ -1152,7 +1152,7 @@
 		goto return_same;
 	new_len = len + nfound*(sub_len - pat_len);
 
-	new_s = (char *)malloc(new_len);
+	new_s = (char *)PyMem_MALLOC(new_len);
 	if (new_s == NULL) return NULL;
 
 	*out_len = new_len;
@@ -1225,7 +1225,7 @@
 	}
 	else {
 		new = PyString_FromStringAndSize(new_s, out_len);
-		free(new_s);
+		PyMem_FREE(new_s);
 	}
 	return new;
 }
diff --git a/Modules/sunaudiodev.c b/Modules/sunaudiodev.c
index 71c152e..f7b9426 100644
--- a/Modules/sunaudiodev.c
+++ b/Modules/sunaudiodev.c
@@ -139,7 +139,7 @@
 	PyMem_DEL(ctldev);
 
 	/* Create and initialize the object */
-	xp = PyObject_NEW(sadobject, &Sadtype);
+	xp = PyObject_New(sadobject, &Sadtype);
 	if (xp == NULL) {
 		close(fd);
 		return NULL;
@@ -158,7 +158,7 @@
 	sadobject *xp;
 {
         close(xp->x_fd);
-	PyMem_DEL(xp);
+	PyObject_Del(xp);
 }
 
 static PyObject *
@@ -412,7 +412,7 @@
 
 static sadstatusobject *
 sads_alloc() {
-	return PyObject_NEW(sadstatusobject, &Sadstatustype);
+	return PyObject_New(sadstatusobject, &Sadstatustype);
 }
 
 static void
diff --git a/Modules/svmodule.c b/Modules/svmodule.c
index 75f2023..99c6fa1 100644
--- a/Modules/svmodule.c
+++ b/Modules/svmodule.c
@@ -340,7 +340,7 @@
 		Py_DECREF(self->ob_svideo);
 		self->ob_svideo = NULL;
 	}
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -374,7 +374,7 @@
 {
 	captureobject *p;
 
-	p = PyObject_NEW(captureobject, &Capturetype);
+	p = PyObject_New(captureobject, &Capturetype);
 	if (p == NULL)
 		return NULL;
 	p->ob_svideo = self;
@@ -994,7 +994,7 @@
 {
 	if (self->ob_svideo != NULL)
 		(void) svCloseVideo(self->ob_svideo);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
@@ -1026,7 +1026,7 @@
 {
 	svobject *p;
 
-	p = PyObject_NEW(svobject, &Svtype);
+	p = PyObject_New(svobject, &Svtype);
 	if (p == NULL)
 		return NULL;
 	p->ob_svideo = svp;
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index cb463d9..195a164 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -58,12 +58,12 @@
 newlockobject()
 {
 	lockobject *self;
-	self = PyObject_NEW(lockobject, &Locktype);
+	self = PyObject_New(lockobject, &Locktype);
 	if (self == NULL)
 		return NULL;
 	self->lock_lock = PyThread_allocate_lock();
 	if (self->lock_lock == NULL) {
-		PyMem_DEL(self);
+		PyObject_Del(self);
 		self = NULL;
 		PyErr_SetString(ThreadError, "can't allocate lock");
 	}
@@ -79,7 +79,7 @@
 	PyThread_release_lock(self->lock_lock);
 	
 	PyThread_free_lock(self->lock_lock);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c
index c0d05f4..90a5029 100644
--- a/Modules/xxmodule.c
+++ b/Modules/xxmodule.c
@@ -62,7 +62,7 @@
 	PyObject *arg;
 {
 	XxoObject *self;
-	self = PyObject_NEW(XxoObject, &Xxo_Type);
+	self = PyObject_New(XxoObject, &Xxo_Type);
 	if (self == NULL)
 		return NULL;
 	self->x_attr = NULL;
@@ -76,7 +76,7 @@
 	XxoObject *self;
 {
 	Py_XDECREF(self->x_attr);
-	PyMem_DEL(self);
+	PyObject_Del(self);
 }
 
 static PyObject *
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 810ffef..cff7293 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -46,7 +46,7 @@
      PyTypeObject *type;
 {
         compobject *self;
-        self = PyObject_NEW(compobject, type);
+        self = PyObject_New(compobject, type);
         if (self == NULL)
                 return NULL;
 	self->is_initialised = 0;
@@ -369,7 +369,7 @@
     if (self->is_initialised)
       deflateEnd(&self->zst);
     Py_XDECREF(self->unused_data);
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static void
@@ -378,7 +378,7 @@
 {
     inflateEnd(&self->zst);
     Py_XDECREF(self->unused_data);
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static char comp_compress__doc__[] =