Index: configure.in =================================================================== RCS file: /cvsroot/python/python/dist/src/configure.in,v retrieving revision 1.297 diff -u -r1.297 configure.in --- configure.in 15 Mar 2002 13:48:21 -0000 1.297 +++ configure.in 18 Mar 2002 23:21:34 -0000 @@ -1334,12 +1334,16 @@ # Check for Python-specific malloc support AC_MSG_CHECKING(for --with-pymalloc) AC_ARG_WITH(pymalloc, -[ --with(out)-pymalloc disable/enable specialized mallocs], [ -if test "$withval" != no -then AC_DEFINE(WITH_PYMALLOC) AC_MSG_RESULT(yes) -else AC_MSG_RESULT(no) -fi], -[AC_MSG_RESULT(no)]) +[ --with(out)-pymalloc disable/enable specialized mallocs]) + +if test -z "$with_pymalloc" +then with_pymalloc="yes" +fi +if test "$with_pymalloc" != "no" +then + AC_DEFINE(WITH_PYMALLOC) +fi +AC_MSG_RESULT($with_pymalloc) # Check for --with-wctype-functions AC_MSG_CHECKING(for --with-wctype-functions) Index: Include/objimpl.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/objimpl.h,v retrieving revision 2.46 diff -u -r2.46 objimpl.h --- Include/objimpl.h 18 Mar 2002 21:04:28 -0000 2.46 +++ Include/objimpl.h 18 Mar 2002 23:21:34 -0000 @@ -34,11 +34,10 @@ allocator) and initialize its object header fields. Note that objects created with PyObject_{New, NewVar} are allocated -within the Python heap by an object allocator, the latter being -implemented (by default) on top of the Python raw memory -allocator. This ensures that Python keeps control on the user's -objects regarding their memory management; for instance, they may be -subject to automatic garbage collection. +within the Python heap by the raw memory allocator (usually the system +malloc). If you want to use the specialized Python allocator use +PyMalloc_New and PyMalloc_NewVar to allocate the objects and +PyMalloc_Del to free them. In case a specific form of memory management is needed, implying that the objects would not reside in the Python heap (for example standard @@ -84,9 +83,9 @@ extern DL_IMPORT(void) PyObject_Free(void *); /* Macros */ -#define PyObject_MALLOC(n) _PyMalloc_MALLOC(n) -#define PyObject_REALLOC(op, n) _PyMalloc_REALLOC((void *)(op), (n)) -#define PyObject_FREE(op) _PyMalloc_FREE((void *)(op)) +#define PyObject_MALLOC(n) PyMem_MALLOC(n) +#define PyObject_REALLOC(op, n) PyMem_REALLOC((void *)(op), (n)) +#define PyObject_FREE(op) PyMem_FREE((void *)(op)) /* * Generic object allocator interface @@ -177,6 +176,22 @@ Note that in C++, the use of the new operator usually implies that the 1st step is performed automatically for you, so in a C++ class constructor you would start directly with PyObject_Init/InitVar. */ + +/* + * The PyMalloc Object Allocator + * ============================= + */ + +extern DL_IMPORT(PyObject *) _PyMalloc_New(PyTypeObject *); +extern DL_IMPORT(PyVarObject *) _PyMalloc_NewVar(PyTypeObject *, int); +extern DL_IMPORT(void) _PyMalloc_Del(PyObject *); + +#define PyMalloc_New(type, typeobj) \ + ( (type *) _PyMalloc_New(typeobj) ) +#define PyMalloc_NewVar(type, typeobj, n) \ + ( (type *) _PyMalloc_NewVar((typeobj), (n)) ) +#define PyMalloc_Del(op) _PyMalloc_Del((PyObject *)(op)) + /* * Garbage Collection Support Index: Modules/gcmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Modules/gcmodule.c,v retrieving revision 2.34 diff -u -r2.34 gcmodule.c --- Modules/gcmodule.c 29 Jan 2002 00:53:41 -0000 2.34 +++ Modules/gcmodule.c 18 Mar 2002 23:21:34 -0000 @@ -829,7 +829,7 @@ const size_t basicsize = _PyObject_VAR_SIZE(tp, nitems); #ifdef WITH_CYCLE_GC const size_t nbytes = sizeof(PyGC_Head) + basicsize; - PyGC_Head *g = PyObject_MALLOC(nbytes); + PyGC_Head *g = _PyMalloc_MALLOC(nbytes); if (g == NULL) return (PyObject *)PyErr_NoMemory(); g->gc.gc_next = NULL; @@ -845,7 +845,7 @@ } op = FROM_GC(g); #else - op = PyObject_MALLOC(basicsize); + op = _PyMalloc_MALLOC(basicsize); if (op == NULL) return (PyObject *)PyErr_NoMemory(); @@ -896,9 +896,9 @@ if (allocated > 0) { allocated--; } - PyObject_FREE(g); + _PyMalloc_FREE(g); #else - PyObject_FREE(op); + _PyMalloc_FREE(op); #endif } Index: Objects/dictobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v retrieving revision 2.119 diff -u -r2.119 dictobject.c --- Objects/dictobject.c 11 Dec 2001 18:51:08 -0000 2.119 +++ Objects/dictobject.c 18 Mar 2002 23:21:34 -0000 @@ -1914,7 +1914,7 @@ dictiter_new(dictobject *dict, binaryfunc select) { dictiterobject *di; - di = PyObject_NEW(dictiterobject, &PyDictIter_Type); + di = PyMalloc_New(dictiterobject, &PyDictIter_Type); if (di == NULL) return NULL; Py_INCREF(dict); @@ -1929,7 +1929,7 @@ dictiter_dealloc(dictiterobject *di) { Py_DECREF(di->di_dict); - PyObject_DEL(di); + PyMalloc_Del(di); } static PyObject * Index: Objects/floatobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v retrieving revision 2.111 diff -u -r2.111 floatobject.c --- Objects/floatobject.c 9 Mar 2002 04:58:24 -0000 2.111 +++ Objects/floatobject.c 18 Mar 2002 23:21:34 -0000 @@ -42,7 +42,6 @@ fill_free_list(void) { PyFloatObject *p, *q; - /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */ p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock)); if (p == NULL) return (PyFloatObject *) PyErr_NoMemory(); @@ -849,7 +848,7 @@ } } else { - PyMem_FREE(list); /* XXX PyObject_FREE ??? */ + PyMem_FREE(list); bf++; } fsum += frem; Index: Objects/intobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v retrieving revision 2.80 diff -u -r2.80 intobject.c --- Objects/intobject.c 1 Feb 2002 15:34:10 -0000 2.80 +++ Objects/intobject.c 18 Mar 2002 23:21:34 -0000 @@ -63,7 +63,6 @@ fill_free_list(void) { PyIntObject *p, *q; - /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */ p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); if (p == NULL) return (PyIntObject *) PyErr_NoMemory(); @@ -979,7 +978,7 @@ } } else { - PyMem_FREE(list); /* XXX PyObject_FREE ??? */ + PyMem_FREE(list); bf++; } isum += irem; Index: Objects/object.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v retrieving revision 2.166 diff -u -r2.166 object.c --- Objects/object.c 18 Mar 2002 21:05:57 -0000 2.166 +++ Objects/object.c 18 Mar 2002 23:21:34 -0000 @@ -2109,3 +2109,27 @@ PyMem_FREE(p); } #endif /* !WITH_PYMALLOC */ + +PyObject *_PyMalloc_New(PyTypeObject *tp) +{ + PyObject *op; + op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp)); + if (op == NULL) + return PyErr_NoMemory(); + return PyObject_INIT(op, tp); +} + +PyVarObject *_PyMalloc_NewVar(PyTypeObject *tp, int nitems) +{ + PyVarObject *op; + const size_t size = _PyObject_VAR_SIZE(tp, nitems); + op = (PyVarObject *) _PyMalloc_MALLOC(size); + if (op == NULL) + return (PyVarObject *)PyErr_NoMemory(); + return PyObject_INIT_VAR(op, tp, nitems); +} + +void _PyMalloc_Del(PyObject *op) +{ + _PyMalloc_FREE(op); +} Index: Objects/rangeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/rangeobject.c,v retrieving revision 2.31 diff -u -r2.31 rangeobject.c --- Objects/rangeobject.c 29 Jan 2002 00:53:41 -0000 2.31 +++ Objects/rangeobject.c 18 Mar 2002 23:21:34 -0000 @@ -60,7 +60,7 @@ PyRange_New(long start, long len, long step, int reps) { long totlen = -1; - rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); + rangeobject *obj = PyMalloc_New(rangeobject, &PyRange_Type); if (obj == NULL) return NULL; @@ -104,7 +104,7 @@ static void range_dealloc(rangeobject *r) { - PyObject_DEL(r); + PyMalloc_Del(r); } static PyObject * Index: Objects/sliceobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/sliceobject.c,v retrieving revision 2.10 diff -u -r2.10 sliceobject.c --- Objects/sliceobject.c 30 Oct 2001 02:40:52 -0000 2.10 +++ Objects/sliceobject.c 18 Mar 2002 23:21:34 -0000 @@ -60,7 +60,7 @@ PyObject * PySlice_New(PyObject *start, PyObject *stop, PyObject *step) { - PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type); + PySliceObject *obj = PyMalloc_New(PySliceObject, &PySlice_Type); if (obj == NULL) return NULL; @@ -115,7 +115,7 @@ Py_DECREF(r->step); Py_DECREF(r->start); Py_DECREF(r->stop); - PyObject_DEL(r); + PyMalloc_Del(r); } static PyObject * Index: Objects/stringobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v retrieving revision 2.150 diff -u -r2.150 stringobject.c --- Objects/stringobject.c 28 Feb 2002 11:38:24 -0000 2.150 +++ Objects/stringobject.c 18 Mar 2002 23:21:34 -0000 @@ -68,7 +68,7 @@ /* PyObject_NewVar is inlined */ op = (PyStringObject *) - PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); + _PyMalloc_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) return PyErr_NoMemory(); PyObject_INIT_VAR(op, &PyString_Type, size); @@ -131,7 +131,7 @@ /* PyObject_NewVar is inlined */ op = (PyStringObject *) - PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); + _PyMalloc_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) return PyErr_NoMemory(); PyObject_INIT_VAR(op, &PyString_Type, size); @@ -733,7 +733,7 @@ size = a->ob_size + b->ob_size; /* PyObject_NewVar is inlined */ op = (PyStringObject *) - PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); + _PyMalloc_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); if (op == NULL) return PyErr_NoMemory(); PyObject_INIT_VAR(op, &PyString_Type, size); @@ -780,7 +780,7 @@ return NULL; } op = (PyStringObject *) - PyObject_MALLOC(sizeof(PyStringObject) + nbytes); + _PyMalloc_MALLOC(sizeof(PyStringObject) + nbytes); if (op == NULL) return PyErr_NoMemory(); PyObject_INIT_VAR(op, &PyString_Type, size); @@ -2789,7 +2789,7 @@ 0, /* tp_init */ 0, /* tp_alloc */ string_new, /* tp_new */ - _PyObject_Del, /* tp_free */ + _PyMalloc_Del, /* tp_free */ }; void @@ -2841,10 +2841,10 @@ #endif _Py_ForgetReference(v); *pv = (PyObject *) - PyObject_REALLOC((char *)v, + _PyMalloc_REALLOC((char *)v, sizeof(PyStringObject) + newsize * sizeof(char)); if (*pv == NULL) { - PyObject_DEL(v); + PyMalloc_Del(v); PyErr_NoMemory(); return -1; } Index: Objects/structseq.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/structseq.c,v retrieving revision 1.6 diff -u -r1.6 structseq.c --- Objects/structseq.c 7 Mar 2002 15:13:40 -0000 1.6 +++ Objects/structseq.c 18 Mar 2002 23:21:34 -0000 @@ -22,7 +22,7 @@ { PyStructSequence *obj; - obj = PyObject_New(PyStructSequence, type); + obj = PyMalloc_New(PyStructSequence, type); obj->ob_size = VISIBLE_SIZE_TP(type); return (PyObject*) obj; @@ -37,7 +37,7 @@ for (i = 0; i < size; ++i) { Py_XDECREF(obj->ob_item[i]); } - PyObject_FREE(obj); + PyMalloc_Del(obj); } static int Index: Objects/typeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v retrieving revision 2.130 diff -u -r2.130 typeobject.c --- Objects/typeobject.c 17 Mar 2002 18:56:20 -0000 2.130 +++ Objects/typeobject.c 18 Mar 2002 23:21:35 -0000 @@ -1103,7 +1103,7 @@ PyObject *doc = PyDict_GetItemString(dict, "__doc__"); if (doc != NULL && PyString_Check(doc)) { const size_t n = (size_t)PyString_GET_SIZE(doc); - type->tp_doc = (char *)PyObject_MALLOC(n+1); + type->tp_doc = (char *)PyMem_MALLOC(n+1); if (type->tp_doc == NULL) { Py_DECREF(type); return NULL; @@ -1417,7 +1417,7 @@ CLEAR(et->slots); if (type->tp_doc != NULL) { - PyObject_FREE(type->tp_doc); + PyMem_FREE(type->tp_doc); type->tp_doc = NULL; } Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.130 diff -u -r2.130 unicodeobject.c --- Objects/unicodeobject.c 28 Feb 2002 11:38:24 -0000 2.130 +++ Objects/unicodeobject.c 18 Mar 2002 23:21:35 -0000 @@ -201,7 +201,7 @@ PyObject_INIT(unicode, &PyUnicode_Type); } else { - unicode = PyObject_NEW(PyUnicodeObject, &PyUnicode_Type); + unicode = PyMalloc_New(PyUnicodeObject, &PyUnicode_Type); if (unicode == NULL) return NULL; unicode->str = PyMem_NEW(Py_UNICODE, length + 1); @@ -219,7 +219,7 @@ onError: _Py_ForgetReference((PyObject *)unicode); - PyObject_DEL(unicode); + PyMalloc_Del(unicode); return NULL; } @@ -5701,7 +5701,7 @@ pnew->str = PyMem_NEW(Py_UNICODE, n+1); if (pnew->str == NULL) { _Py_ForgetReference((PyObject *)pnew); - PyObject_DEL(pnew); + PyMalloc_Del(pnew); return NULL; } Py_UNICODE_COPY(pnew->str, tmp->str, n+1); @@ -5759,7 +5759,7 @@ 0, /* tp_init */ 0, /* tp_alloc */ unicode_new, /* tp_new */ - _PyObject_Del, /* tp_free */ + _PyMalloc_Del, /* tp_free */ }; /* Initialize the Unicode implementation */ @@ -5801,7 +5801,7 @@ if (v->str) PyMem_DEL(v->str); Py_XDECREF(v->defenc); - PyObject_DEL(v); + PyMalloc_Del(v); } unicode_freelist = NULL; unicode_freelist_size = 0;