]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Vladimir Marangozov's long-awaited malloc restructuring.
authorGuido van Rossum <guido@python.org>
Wed, 3 May 2000 23:44:39 +0000 (23:44 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 3 May 2000 23:44:39 +0000 (23:44 +0000)
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.)

73 files changed:
Include/mymalloc.h
Include/objimpl.h
Modules/_cursesmodule.c
Modules/_sre.c
Modules/_tkinter.c
Modules/almodule.c
Modules/arraymodule.c
Modules/bsddbmodule.c
Modules/cPickle.c
Modules/cStringIO.c
Modules/cdmodule.c
Modules/clmodule.c
Modules/dbmmodule.c
Modules/dlmodule.c
Modules/flmodule.c
Modules/fmmodule.c
Modules/gdbmmodule.c
Modules/getpath.c
Modules/linuxaudiodev.c
Modules/md5module.c
Modules/mmapmodule.c
Modules/mpzmodule.c
Modules/newmodule.c
Modules/nismodule.c
Modules/parsermodule.c
Modules/pcremodule.c
Modules/pyexpat.c
Modules/readline.c
Modules/regexmodule.c
Modules/rotormodule.c
Modules/selectmodule.c
Modules/shamodule.c
Modules/socketmodule.c
Modules/stropmodule.c
Modules/sunaudiodev.c
Modules/svmodule.c
Modules/threadmodule.c
Modules/xxmodule.c
Modules/zlibmodule.c
Objects/bufferobject.c
Objects/classobject.c
Objects/cobject.c
Objects/complexobject.c
Objects/dictobject.c
Objects/fileobject.c
Objects/floatobject.c
Objects/frameobject.c
Objects/funcobject.c
Objects/intobject.c
Objects/listobject.c
Objects/longobject.c
Objects/methodobject.c
Objects/moduleobject.c
Objects/object.c
Objects/rangeobject.c
Objects/sliceobject.c
Objects/stringobject.c
Objects/tupleobject.c
Objects/unicodeobject.c
Objects/xxobject.c
PC/_winreg.c
PC/winreg.c
Parser/myreadline.c
Parser/parsetok.c
Parser/pgenmain.c
Parser/tokenizer.c
Python/bltinmodule.c
Python/ceval.c
Python/compile.c
Python/import.c
Python/marshal.c
Python/pythonrun.c
Python/traceback.c

index 558af9d29b17cedff582b135c994c39027059446..df3113ba66c05a244f685a2224b43c664e4f580d 100644 (file)
@@ -57,6 +57,8 @@ PERFORMANCE OF THIS SOFTWARE.
 #include <stdlib.h>
 #endif
 
+#include "myproto.h"
+
 #ifdef __cplusplus
 /* Move this down here since some C++ #include's don't like to be included
    inside an extern "C" */
@@ -67,12 +69,8 @@ extern "C" {
 #pragma lib_export on
 #endif
 
-/* The following should never be necessary */
-#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
-extern ANY *malloc Py_PROTO((size_t));
-extern ANY *calloc Py_PROTO((size_t, size_t));
-extern ANY *realloc Py_PROTO((ANY *, size_t));
-extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
+#ifndef DL_IMPORT       /* declarations for DLL import */
+#define DL_IMPORT(RTYPE) RTYPE
 #endif
 
 #ifndef NULL
@@ -87,34 +85,117 @@ extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
 #define _PyMem_EXTRA 0
 #endif
 
-#define PyMem_NEW(type, n) \
-       ( (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)) )
-#define PyMem_RESIZE(p, type, n) \
-       if ((p) == NULL) \
-               (p) =  (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)); \
-       else \
-               (p) = (type *) realloc((ANY *)(p), \
-                                      _PyMem_EXTRA + (n) * sizeof(type))
-#define PyMem_DEL(p) free((ANY *)p)
-#define PyMem_XDEL(p) if ((p) == NULL) ; else PyMem_DEL(p)
+/*
+ * Core memory allocator
+ * =====================
+ */
 
+/* To make sure the interpreter is user-malloc friendly, all memory
+   APIs are implemented on top of this one.
 
-/* Two sets of function wrappers around malloc and friends; useful if
-   you need to be sure that you are using the same memory allocator as
-   Python.  Note that the wrappers make sure that allocating 0 bytes
-   returns a non-NULL pointer, even if the underlying malloc doesn't.
-   The Python interpreter continues to use PyMem_NEW etc. */
+   The PyCore_* macros can be defined to make the interpreter use a
+   custom allocator. Note that they are for internal use only. Both
+   the core and extension modules should use the PyMem_* API. */
+
+#ifndef PyCore_MALLOC_FUNC
+#undef PyCore_REALLOC_FUNC
+#undef PyCore_FREE_FUNC
+#define PyCore_MALLOC_FUNC      malloc
+#define PyCore_REALLOC_FUNC     realloc
+#define PyCore_FREE_FUNC        free
+#endif
+
+#ifndef PyCore_MALLOC_PROTO
+#undef PyCore_REALLOC_PROTO
+#undef PyCore_FREE_PROTO
+#define PyCore_MALLOC_PROTO     Py_PROTO((size_t))
+#define PyCore_REALLOC_PROTO    Py_PROTO((ANY *, size_t))
+#define PyCore_FREE_PROTO       Py_PROTO((ANY *))
+#endif
+
+#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
+extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
+extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
+extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
+#endif
+
+#ifndef PyCore_MALLOC
+#undef PyCore_REALLOC
+#undef PyCore_FREE
+#define PyCore_MALLOC(n)        PyCore_MALLOC_FUNC(n)
+#define PyCore_REALLOC(p, n)    PyCore_REALLOC_FUNC((p), (n))
+#define PyCore_FREE(p)          PyCore_FREE_FUNC(p)
+#endif
 
-/* These wrappers around malloc call PyErr_NoMemory() on failure */
-extern DL_IMPORT(ANY *) Py_Malloc Py_PROTO((size_t));
-extern DL_IMPORT(ANY *) Py_Realloc Py_PROTO((ANY *, size_t));
-extern DL_IMPORT(void) Py_Free Py_PROTO((ANY *));
+/* BEWARE:
 
-/* These wrappers around malloc *don't* call anything on failure */
+   Each interface exports both functions and macros. Extension modules
+   should normally use the functions for ensuring binary compatibility
+   of the user's code across Python versions. Subsequently, if the
+   Python runtime switches to its own malloc (different from standard
+   malloc), no recompilation is required for the extensions.
+
+   The macro versions trade compatibility for speed. They can be used
+   whenever there is a performance problem, but their use implies
+   recompilation of the code for each new Python release. The Python
+   core uses the macros because it *is* compiled on every upgrade.
+   This might not be the case with 3rd party extensions in a custom
+   setup (for example, a customer does not always have access to the
+   source of 3rd party deliverables). You have been warned! */
+
+/*
+ * Raw memory interface
+ * ====================
+ */
+
+/* Functions */
+
+/* Function wrappers around PyCore_MALLOC and friends; useful if you
+   need to be sure that you are using the same memory allocator as
+   Python.  Note that the wrappers make sure that allocating 0 bytes
+   returns a non-NULL pointer, even if the underlying malloc
+   doesn't. Returned pointers must be checked for NULL explicitly.
+   No action is performed on failure. */
 extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
 extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
 extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
 
+/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
+   no longer supported. They used to call PyErr_NoMemory() on failure. */
+
+/* Macros */
+#define PyMem_MALLOC(n)         PyCore_MALLOC(n)
+#define PyMem_REALLOC(p, n)     PyCore_REALLOC((ANY *)(p), (n))
+#define PyMem_FREE(p)           PyCore_FREE((ANY *)(p))
+
+/*
+ * Type-oriented memory interface
+ * ==============================
+ */
+
+/* Functions */
+#define PyMem_New(type, n) \
+       ( (type *) PyMem_Malloc((n) * sizeof(type)) )
+#define PyMem_Resize(p, type, n) \
+       ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
+#define PyMem_Del(p) PyMem_Free(p)
+
+/* Macros */
+#define PyMem_NEW(type, n) \
+       ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
+#define PyMem_RESIZE(p, type, n) \
+       if ((p) == NULL) \
+               (p) = (type *)(PyMem_MALLOC( \
+                                   _PyMem_EXTRA + (n) * sizeof(type))); \
+       else \
+               (p) = (type *)(PyMem_REALLOC((p), \
+                                   _PyMem_EXTRA + (n) * sizeof(type)))
+#define PyMem_DEL(p) PyMem_FREE(p)
+
+/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
+   it is recommended to write the test explicitly in the code.
+   Note that according to ANSI C, free(NULL) has no effect. */
+
 #ifdef __cplusplus
 }
 #endif
index c093466626827094bb5ee92468f3556afc741ab4..71dbb937871f790531977430345740d40e13020d 100644 (file)
@@ -35,42 +35,204 @@ PERFORMANCE OF THIS SOFTWARE.
 
 ******************************************************************/
 
+#include "mymalloc.h"
+
 /*
-Additional macros for modules that implement new object types.
+Functions and macros for modules that implement new object types.
 You must first include "object.h".
 
-PyObject_NEW(type, typeobj) allocates memory for a new object of the given
-type; here 'type' must be the C structure type used to represent the
-object and 'typeobj' the address of the corresponding type object.
-Reference count and type pointer are filled in; the rest of the bytes of
-the object are *undefined*!  The resulting expression type is 'type *'.
-The size of the object is actually determined by the tp_basicsize field
-of the type object.
+ - PyObject_New(type, typeobj) allocates memory for a new object of
+   the given type; here 'type' must be the C structure type used to
+   represent the object and 'typeobj' the address of the corresponding
+   type object.  Reference count and type pointer are filled in; the
+   rest of the bytes of the object are *undefined*!  The resulting
+   expression type is 'type *'.  The size of the object is actually
+   determined by the tp_basicsize field of the type object.
+
+ - PyObject_NewVar(type, typeobj, n) is similar but allocates a
+   variable-size object with n extra items.  The size is computed as
+   tp_basicsize plus n * tp_itemsize.  This fills in the ob_size field
+   as well.
+
+ - PyObject_Del(op) releases the memory allocated for an object.
+
+ - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
+   similar to PyObject_{New, NewVar} except that they don't allocate
+   the memory needed for an object. Instead of the 'type' parameter,
+   they accept the pointer of a new object (allocated by an arbitrary
+   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.
+
+In case a specific form of memory management is needed, implying that
+the objects would not reside in the Python heap (for example standard
+malloc heap(s) are mandatory, use of shared memory, C++ local storage
+or operator new), you must first allocate the object with your custom
+allocator, then pass its pointer to PyObject_{Init, InitVar} for
+filling in its Python-specific fields: reference count, type pointer,
+possibly others. You should be aware that Python has very limited
+control over these objects because they don't cooperate with the
+Python memory manager. Such objects may not be eligible for automatic
+garbage collection and you have to make sure that they are released
+accordingly whenever their destructor gets called (cf. the specific
+form of memory management you're using).
+
+Unless you have specific memory management requirements, it is
+recommended to use PyObject_{New, NewVar, Del}. */
+
+/* 
+ * Core object memory allocator
+ * ============================
+ */
 
-PyObject_NEW_VAR(type, typeobj, n) is similar but allocates a variable-size
-object with n extra items.  The size is computed as tp_basicsize plus
-n * tp_itemsize.  This fills in the ob_size field as well.
-*/
+/* The purpose of the object allocator is to make make the distinction
+   between "object memory" and the rest within the Python heap.
+   
+   Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
+   the one that holds the object's representation defined by its C
+   type structure, *excluding* any object-specific memory buffers that
+   might be referenced by the structure (for type structures that have
+   pointer fields).  By default, the object memory allocator is
+   implemented on top of the raw memory allocator.
 
-#ifndef MS_COREDLL
+   The PyCore_* macros can be defined to make the interpreter use a
+   custom object memory allocator. They are reserved for internal
+   memory management purposes exclusively. Both the core and extension
+   modules should use the PyObject_* API. */
+
+#ifndef PyCore_OBJECT_MALLOC_FUNC
+#undef PyCore_OBJECT_REALLOC_FUNC
+#undef PyCore_OBJECT_FREE_FUNC
+#define PyCore_OBJECT_MALLOC_FUNC    PyCore_MALLOC_FUNC
+#define PyCore_OBJECT_REALLOC_FUNC   PyCore_REALLOC_FUNC
+#define PyCore_OBJECT_FREE_FUNC      PyCore_FREE_FUNC
+#endif
+
+#ifndef PyCore_OBJECT_MALLOC_PROTO
+#undef PyCore_OBJECT_REALLOC_PROTO
+#undef PyCore_OBJECT_FREE_PROTO
+#define PyCore_OBJECT_MALLOC_PROTO   PyCore_MALLOC_PROTO
+#define PyCore_OBJECT_REALLOC_PROTO  PyCore_REALLOC_PROTO
+#define PyCore_OBJECT_FREE_PROTO     PyCore_FREE_PROTO
+#endif
+
+#ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
+extern ANY *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
+extern ANY *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
+extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
+#endif
+
+#ifndef PyCore_OBJECT_MALLOC
+#undef PyCore_OBJECT_REALLOC
+#undef PyCore_OBJECT_FREE
+#define PyCore_OBJECT_MALLOC(n)      PyCore_OBJECT_MALLOC_FUNC(n)
+#define PyCore_OBJECT_REALLOC(p, n)  PyCore_OBJECT_REALLOC_FUNC((p), (n))
+#define PyCore_OBJECT_FREE(p)        PyCore_OBJECT_FREE_FUNC(p)
+#endif
+
+/*
+ * Raw object memory interface
+ * ===========================
+ */
+
+/* The use of this API should be avoided, unless a builtin object
+   constructor inlines PyObject_{New, NewVar}, either because the
+   latter functions cannot allocate the exact amount of needed memory,
+   either for speed. This situation is exceptional, but occurs for
+   some object constructors (PyBuffer_New, PyList_New...).  Inlining
+   PyObject_{New, NewVar} for objects that are supposed to belong to
+   the Python heap is discouraged. If you really have to, make sure
+   the object is initialized with PyObject_{Init, InitVar}. Do *not*
+   inline PyObject_{Init, InitVar} for user-extension types or you
+   might seriously interfere with Python's memory management. */
+
+/* Functions */
+
+/* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
+   need to be sure that you are using the same object memory allocator
+   as Python. These wrappers *do not* make sure that allocating 0
+   bytes returns a non-NULL pointer. Returned pointers must be checked
+   for NULL explicitly; no action is performed on failure. */
+extern DL_IMPORT(ANY *) PyObject_Malloc Py_PROTO((size_t));
+extern DL_IMPORT(ANY *) PyObject_Realloc Py_PROTO((ANY *, size_t));
+extern DL_IMPORT(void) PyObject_Free Py_PROTO((ANY *));
+
+/* Macros */
+#define PyObject_MALLOC(n)           PyCore_OBJECT_MALLOC(n)
+#define PyObject_REALLOC(op, n)      PyCore_OBJECT_REALLOC((ANY *)(op), (n))
+#define PyObject_FREE(op)            PyCore_OBJECT_FREE((ANY *)(op))
+
+/*
+ * Generic object allocator interface
+ * ==================================
+ */
+
+/* Functions */
+extern DL_IMPORT(PyObject *) PyObject_Init Py_PROTO((PyObject *, PyTypeObject *));
+extern DL_IMPORT(PyVarObject *) PyObject_InitVar Py_PROTO((PyVarObject *, PyTypeObject *, int));
 extern DL_IMPORT(PyObject *) _PyObject_New Py_PROTO((PyTypeObject *));
 extern DL_IMPORT(PyVarObject *) _PyObject_NewVar Py_PROTO((PyTypeObject *, int));
+extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
+
+#define PyObject_New(type, typeobj) \
+               ( (type *) _PyObject_New(typeobj) )
+#define PyObject_NewVar(type, typeobj, n) \
+               ( (type *) _PyObject_NewVar((typeobj), (n)) )
+#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
+
+/* Macros trading binary compatibility for speed. See also mymalloc.h.
+   Note that these macros expect non-NULL object pointers.*/
+#define PyObject_INIT(op, typeobj) \
+       ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
+#define PyObject_INIT_VAR(op, typeobj, size) \
+       ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
+
+#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
+#define _PyObject_VAR_SIZE(typeobj, n) \
+       ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize )
+
+#define PyObject_NEW(type, typeobj) \
+( (type *) PyObject_Init( \
+       (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
+#define PyObject_NEW_VAR(type, typeobj, n) \
+( (type *) PyObject_InitVar( \
+       (PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
+       (typeobj), (n)) )
+#define PyObject_DEL(op) PyObject_FREE(op)
+
+/* This example code implements an object constructor with a custom
+   allocator, where PyObject_New is inlined, and shows the important
+   distinction between two steps (at least):
+       1) the actual allocation of the object storage;
+       2) the initialization of the Python specific fields
+          in this storage with PyObject_{Init, InitVar}.
+
+   PyObject *
+   YourObject_New(...)
+   {
+       PyObject *op;
 
-#define PyObject_NEW(type, typeobj) ((type *) _PyObject_New(typeobj))
-#define PyObject_NEW_VAR(type, typeobj, n) ((type *) _PyObject_NewVar(typeobj, n))
+       op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
+       if (op == NULL)
+           return PyErr_NoMemory();
 
-#else
-/* For an MS-Windows DLL, we change the way an object is created, so that the
-   extension module's malloc is used, rather than the core DLL malloc, as there is
-   no guarantee they will use the same heap
-*/
-extern DL_IMPORT(PyObject *) _PyObject_New Py_PROTO((PyTypeObject *, PyObject *));
-extern DL_IMPORT(PyVarObject *) _PyObject_NewVar Py_PROTO((PyTypeObject *, int, PyVarObject *));
+       op = PyObject_Init(op, &YourTypeStruct);
+       if (op == NULL)
+           return NULL;
 
-#define PyObject_NEW(type, typeobj) ((type *) _PyObject_New(typeobj,(PyObject *)malloc((typeobj)->tp_basicsize)))
-#define PyObject_NEW_VAR(type, typeobj, n) ((type *) _PyObject_NewVar(typeobj, n, (PyVarObject *)malloc((typeobj)->tp_basicsize + n * (typeobj)->tp_itemsize)))
+       op->ob_field = value;
+       ...
+       return op;
+   }
 
-#endif /* MS_COREDLL */
+   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. */
 
 #ifdef __cplusplus
 }
index f76bf036fecaceb2099285d131ed2dc10b6ee8b9..d654b294ae3c8b7f1d862ad00a5f318aaef00d6b 100644 (file)
@@ -273,7 +273,7 @@ PyCursesScreen_New(arg)
        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 @@ PyCursesWindow_New(win)
 {
        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 @@ PyCursesWindow_Dealloc(wo)
 {
   if (wo->win != stdscr)
     delwin(wo->win);
-  PyMem_DEL(wo);
+  PyObject_Del(wo);
 }
 
 static PyObject *
@@ -1125,7 +1125,7 @@ PyCursesPad_New(pad)
        WINDOW *pad;
 {
        PyCursesPadObject *po;
-       po = PyObject_NEW(PyCursesPadObject, &PyCursesPad_Type);
+       po = PyObject_New(PyCursesPadObject, &PyCursesPad_Type);
        if (po == NULL)
                return NULL;
        po->pad = pad;
index 9eec0355cfb8ca964d5222a237828668c892bae3..47b80c5d7fafa7062386fb3818125c5903b2d81f 100644 (file)
@@ -848,7 +848,7 @@ _compile(PyObject* self_, PyObject* args)
                           &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 @@ _pattern_new_match(PatternObject* pattern, SRE_STATE* state,
        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 @@ _pattern_dealloc(PatternObject* self)
        Py_XDECREF(self->code);
        Py_XDECREF(self->pattern);
        Py_XDECREF(self->groupindex);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject*
@@ -1163,7 +1163,7 @@ _match_dealloc(MatchObject* self)
 {
        Py_XDECREF(self->string);
        Py_DECREF(self->pattern);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject*
index 008378dff4fc3ff0d0634c29f36e5fc682d23917..15cc7e750a5efd3cc3b6078071c5527964551389 100644 (file)
@@ -469,7 +469,7 @@ Tkapp_New(screenName, baseName, className, interactive)
        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 @@ Tktt_New(func)
 {
        TkttObject *v;
   
-       v = PyObject_NEW(TkttObject, &Tktt_Type);
+       v = PyObject_New(TkttObject, &Tktt_Type);
        if (v == NULL)
                return NULL;
 
@@ -1662,7 +1662,7 @@ Tktt_Dealloc(self)
 
        Py_XDECREF(func);
 
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -1910,7 +1910,7 @@ Tkapp_Dealloc(self)
        ENTER_TCL
        Tcl_DeleteInterp(Tkapp_Interp(self));
        LEAVE_TCL
-       PyMem_DEL(self);
+       PyObject_Del(self);
        DisableEventHook();
 }
 
index 1033c0727abde72114d604152351953000ea4faa..c75fe8921462dd0aa4a2b6534ae1763fcd3a5ee4 100644 (file)
@@ -648,7 +648,7 @@ newalcobject(ALconfig config)
 {
        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 @@ alc_dealloc(self)
 #else
        (void) ALfreeconfig(self->config);      /* ignore errors */
 #endif
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -1421,7 +1421,7 @@ newalpobject(ALport port)
 {
        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 @@ alp_dealloc(self)
                ALcloseport(self->port);
 #endif
        }
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
index dcd931b2823e17b044b100a2e296a8a50d258405..9a09c7d720eebdcab6d294eee59acbcadc859275 100644 (file)
@@ -346,7 +346,7 @@ newarrayobject(size, descr)
        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 @@ newarrayobject(size, descr)
        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 @@ array_dealloc(op)
 {
        if (op->ob_item != NULL)
                PyMem_DEL(op->ob_item);
-       PyMem_DEL(op);
+       PyObject_Del(op);
 }
 
 static int
index 16178ed8831fbd94b95b41541e3334cf9dfb9c89..97a8e8be4341e7195481bf6693e017b61ea5efc8 100644 (file)
@@ -89,7 +89,7 @@ newdbhashobject(file, flags, mode,
        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 @@ newdbbtobject(file, flags, mode,
        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 @@ newdbrnobject(file, flags, mode,
        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 @@ bsddb_dealloc(dp)
                                "Python bsddb: close errno %d in dealloc\n",
                                errno);
        }
-       PyMem_DEL(dp);
+       PyObject_Del(dp);
 }
 
 #ifdef WITH_THREAD
index aa2c7cba08d2a05b72632a8f0cb5ea9d8148e31f..73cb6ba48c7eedb6c59ab077cb198ef37058769b 100644 (file)
@@ -171,7 +171,7 @@ Pdata_dealloc(Pdata *self) {
 
     if (self->data) free(self->data);
 
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static PyTypeObject PdataType = {
@@ -186,7 +186,7 @@ static PyObject *
 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 @@ static Picklerobject *
 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 @@ Pickler_dealloc(Picklerobject *self) {
         free(self->write_buf);
     }
 
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 
@@ -2885,7 +2885,7 @@ Instance_New(PyObject *cls, PyObject *args) {
               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 @@ static Unpicklerobject *
 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 @@ Unpickler_dealloc(Unpicklerobject *self) {
         free(self->buf);
     }
     
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 
index e816178dc9b96582d01cdd93585bc5fcfc0a901c..557545f2092aad0707b464bbd82fa301d61f85ad 100644 (file)
@@ -56,7 +56,7 @@ static char cStringIO_module_documentation[] =
 "\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 @@ static void
 O_dealloc(Oobject *self) {
   if (self->buf != NULL)
     free(self->buf);
-  PyMem_DEL(self);
+  PyObject_Del(self);
 }
 
 static PyObject *
@@ -465,7 +465,7 @@ static PyObject *
 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 struct PyMethodDef I_methods[] = {
 static void
 I_dealloc(Iobject *self) {
   Py_XDECREF(self->pbuf);
-  PyMem_DEL(self);
+  PyObject_Del(self);
 }
 
 static PyObject *
@@ -586,7 +586,7 @@ newIobject(PyObject *s) {
   }
   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;
index 1b414c10c2ea1142a57d50d8513626de1b5e8702..4a04e43f23b8af12fd75e2a0fd404984ad248ef0 100644 (file)
@@ -447,7 +447,7 @@ cdplayer_dealloc(self)
 {
        if (self->ob_cdplayer != NULL)
                CDclose(self->ob_cdplayer);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -483,7 +483,7 @@ newcdplayerobject(cdp)
 {
        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 @@ cdparser_dealloc(self)
                self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
        }
        CDdeleteparser(self->ob_cdparser);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -799,7 +799,7 @@ newcdparserobject(cdp)
        cdparserobject *p;
        int i;
 
-       p = PyObject_NEW(cdparserobject, &CdParsertype);
+       p = PyObject_New(cdparserobject, &CdParsertype);
        if (p == NULL)
                return NULL;
        p->ob_cdparser = cdp;
index 05b00da5dc351a60f2d4aaa4caa4729213c63293..976346a5849d31104926e3a966dfa1c0f589abd7 100644 (file)
@@ -673,7 +673,7 @@ cl_dealloc(PyObject *self)
                else
                        clCloseDecompressor(SELF->ob_compressorHdl);
        }
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -713,7 +713,7 @@ doOpen(PyObject *self, PyObject *args, int (*open_func)(int, CL_Handle *),
        if (!PyArg_Parse(args, "i", &scheme))
                return NULL;
 
-       new = PyObject_NEW(clobject, &Cltype);
+       new = PyObject_New(clobject, &Cltype);
        if (new == NULL)
                return NULL;
 
index 501c3f485096cd0904d4fccbc4db66839813e9af..d4fc1bfec25b06aff39cf68d7a2ac95def8df5be 100644 (file)
@@ -62,7 +62,7 @@ int mode;
 {
         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 @@ dbm_dealloc(dp)
 {
         if ( dp->di_dbm )
                dbm_close(dp->di_dbm);
-       PyMem_DEL(dp);
+       PyObject_Del(dp);
 }
 
 static int
index 76d054017973391e2f6fe51bb3c0d6dabbf657a2..b40e05e6957de84274466d7d977d0013848a4f63 100644 (file)
@@ -54,7 +54,7 @@ newdlobject(handle)
        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 @@ dl_dealloc(xp)
 {
        if (xp->dl_handle != NULL)
                dlclose(xp->dl_handle);
-       PyMem_DEL(xp);
+       PyObject_Del(xp);
 }
 
 static PyObject *
index c921d4bca37f1f6a69fa70a8d540513fb6459053..c6ce439347be2b7584341675ccf14628b116bdfb 100644 (file)
@@ -332,7 +332,7 @@ generic_dealloc(g)
        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 @@ newgenericobject(generic, methods)
        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 @@ form_dealloc(f)
        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 @@ newformobject(form)
        FL_FORM *form;
 {
        formobject *f;
-       f = PyObject_NEW(formobject, &Formtype);
+       f = PyObject_New(formobject, &Formtype);
        if (f == NULL)
                return NULL;
        f->ob_form = form;
index dd5b39787827cb7e7693ce89c5a30e1d72053ce0..21f08d89d7fbcdb1d42e7f513b1787e7d57fdfd9 100644 (file)
@@ -59,7 +59,7 @@ newfhobject(fh)
                                "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 @@ fh_dealloc(fhp)
        fhobject *fhp;
 {
        fmfreefont(fhp->fh_fh);
-       PyMem_DEL(fhp);
+       PyObject_Del(fhp);
 }
 
 static PyTypeObject Fhtype = {
index 96b5866b72f87240f28466781bc880594619b699..066cf3bb311210e19891d7030d7a6940c9eca5a6 100644 (file)
@@ -93,7 +93,7 @@ int mode;
 {
         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 @@ dbm_dealloc(dp)
 {
         if ( dp->di_dbm )
                gdbm_close(dp->di_dbm);
-       PyMem_DEL(dp);
+       PyObject_Del(dp);
 }
 
 static int
index 78b4915fa5ed6a07428b2dc0148b081a3e1bfdb6..7856813743cc3a25e623de16455d000001a9f11c 100644 (file)
@@ -529,7 +529,7 @@ calculate_path()
        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 */
index 393df05bd6349a0b1e86cf6fba7bf865620dc66f..aac742a281d83bd15d20c8767c43e6a7b33c9289 100644 (file)
@@ -104,7 +104,7 @@ newladobject(PyObject *arg)
   }
 
   /* 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 @@ static void
 lad_dealloc(lad_t *xp)
 {
   close(xp->x_fd);
-  PyMem_DEL(xp);
+  PyObject_Del(xp);
 }
 
 static PyObject *
index 3c001ae5e5f77f7055d89a9ef98dcce620811f54..6856c68773f5c6e6e17c62f5a364a911d1d1bafe 100644 (file)
@@ -56,7 +56,7 @@ newmd5object()
 {
        md5object *md5p;
 
-       md5p = PyObject_NEW(md5object, &MD5type);
+       md5p = PyObject_New(md5object, &MD5type);
        if (md5p == NULL)
                return NULL;
 
@@ -71,7 +71,7 @@ static void
 md5_dealloc(md5p)
        md5object *md5p;
 {
-       PyMem_DEL(md5p);
+       PyObject_Del(md5p);
 }
 
 
index a79812d9338968bfee47cfe4b91627e050005d65..fcbb4845fc961d621603d283778c1acca8e5b587 100644 (file)
@@ -69,7 +69,7 @@ mmap_object_dealloc(mmap_object * m_obj)
        }
 #endif /* UNIX */
 
-       PyMem_DEL(m_obj);
+       PyObject_Del(m_obj);
 }
 
 static PyObject *
@@ -706,7 +706,7 @@ new_mmap_object (PyObject * self, PyObject * args, PyObject *kwdict)
                )
                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 @@ new_mmap_object (PyObject * self, PyObject * args)
                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;
index e701d8548db1e5232a813ce694c0a3234f97e28f..1aaf787e6b53ad941f26597779b2bdd97b1648be 100644 (file)
@@ -123,7 +123,7 @@ newmpzobject()
 #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 @@ mpz_dealloc(mpzp)
        fputs( "mpz_dealloc() called...\n", stderr );
 #endif /* def MPZ_DEBUG */
        mpz_clear(&mpzp->mpz);
-       PyMem_DEL(mpzp);
+       PyObject_Del(mpzp);
 } /* mpz_dealloc() */
 
 
index 2758f141f2ea8de63c5e554955e501ffb13b8093..fa0dc8b1d1ae609509b333cc9e3b469dbb75aae0 100644 (file)
@@ -49,7 +49,7 @@ new_instance(unused, args)
                              &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);
index 04f27f2bda424cd7d7edfc42842e916025b2f1a5..89437294c90e4b51ad09225b5767bbeb4e163c72 100644 (file)
@@ -353,11 +353,11 @@ nis_maplist ()
        if (list->stat != NIS_TRUE)
                goto finally;
 
-       PyMem_DEL(server);
+       free(server);
        return list->maps;
 
   finally:
-       PyMem_DEL(server);
+       free(server);
        return NULL;
 }
 
index 6a8d38c0e09b452a6e1128d956e116d61e79e4ee..9b9baf0cea14de2b30f73512f8648a2527a37a2b 100644 (file)
@@ -295,7 +295,7 @@ parser_compare(PyAST_Object *left, PyAST_Object *right)
 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 @@ static void
 parser_free(PyAST_Object *ast)
 {
     PyNode_Free(ast->ast_node);
-    PyMem_DEL(ast);
+    PyObject_Del(ast);
 }
 
 
@@ -790,10 +790,10 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
                 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);
index 4d2aa7298cb40fd60a84d8b31b10acfc09d1fef1..6b9f960a3af1096e103e5dd1cbd1a4e57ebb0312 100644 (file)
@@ -79,7 +79,7 @@ newPcreObject(arg)
        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 @@ PyPcre_dealloc(self)
 {
        if (self->regex) (pcre_free)(self->regex);
        if (self->regex_extra) (pcre_free)(self->regex_extra);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 
index e98f3931f2c08e7a67f0e5e9a188fe6e4e72d895..f8b3bcead45e008fd2d1672f9fd04e62358bfc2f 100644 (file)
@@ -471,7 +471,7 @@ newxmlparseobject( char *encoding, char *namespace_separator){
         int i;
         xmlparseobject *self;
         
-        self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
+        self = PyObject_New(xmlparseobject, &Xmlparsetype);
         if (self == NULL)
                 return NULL;
 
@@ -512,7 +512,7 @@ xmlparse_dealloc( xmlparseobject *self )
         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 ){
index d4ba3f84a45aedafc23b89a1e1686217e8c39ed5..37baf8ca92a40066d7c54cff9cad9246e5f1651f 100644 (file)
@@ -377,7 +377,7 @@ call_readline(prompt)
        char *prompt;
 {
        int n;
-       char *p;
+       char *p, *q;
        RETSIGTYPE (*old_inthandler)();
        old_inthandler = signal(SIGINT, onintr);
        if (setjmp(jbuf)) {
@@ -391,8 +391,10 @@ call_readline(prompt)
        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 @@ call_readline(prompt)
        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;
 }
 
index 4fbf5c87cf539032230f652ed48d35924b137c07..92d3b71cacadfb5fad35d10f433b17742bd7efb8 100644 (file)
@@ -64,13 +64,14 @@ static void
 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 @@ newregexobject(pattern, translate, givenpat, groupindex)
                                "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;
index b1fef3938b7f159b9b595d3d0a1c609148f86362..3d570f7751c61e3e2e91c9d877a1cafa666483a5 100644 (file)
@@ -178,7 +178,7 @@ rotorobj_new(num_rotors, key)
 {
        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 @@ rotorobj_new(num_rotors, key)
        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 @@ static void
 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 * 
index b48eb54ab7c556c590bee4cfa22ab271ee9ef1a3..fb46fc30719b330296c51027548edce8e7441636 100644 (file)
@@ -278,9 +278,9 @@ select_select(self, args)
        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
index 0504fad5d3b3d09284667a95c3bd8200f8f7737a..d56e90e59c50df48c730589fa342b545891a4c76 100644 (file)
@@ -380,7 +380,7 @@ staticforward PyTypeObject SHAtype;
 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 @@ static void
 SHA_dealloc(ptr)
        PyObject *ptr;
 {
-       PyMem_DEL(ptr);
+       PyObject_Del(ptr);
 }
 
 
index 939a49504b0812e339348552824ff371deb84830..56f1498b67975b59d926cb1766e87036e4177c17 100644 (file)
@@ -389,7 +389,7 @@ BUILD_FUNC_DEF_4(PySocketSock_New,int,fd, int,family, int,type, int,proto)
 {
        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 @@ BUILD_FUNC_DEF_1(PySocketSock_dealloc,PySocketSockObject *,s)
 {
        if (s->sock_fd != -1)
                (void) SOCKETCLOSE(s->sock_fd);
-       PyMem_DEL(s);
+       PyObject_Del(s);
 }
 
 
@@ -1948,7 +1948,7 @@ BUILD_FUNC_DEF_3(newSSLObject,
        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 @@ BUILD_FUNC_DEF_3(newSSLObject,
        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 @@ BUILD_FUNC_DEF_3(newSSLObject,
                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 @@ BUILD_FUNC_DEF_3(newSSLObject,
                        PyErr_SetObject(SSLErrorObject,
                                PyString_FromString(
                                  "SSL_CTX_use_PrivateKey_file error"));
-                       PyMem_DEL(self);
+                       PyObject_Del(self);
                        return NULL;
                }
 
@@ -1993,7 +1993,7 @@ BUILD_FUNC_DEF_3(newSSLObject,
                        PyErr_SetObject(SSLErrorObject,
                                PyString_FromString(
                                  "SSL_CTX_use_certificate_chain_file error"));
-                       PyMem_DEL(self);
+                       PyObject_Del(self);
                        return NULL;
                }
        }
@@ -2008,7 +2008,7 @@ BUILD_FUNC_DEF_3(newSSLObject,
                /* 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 @@ static void SSL_dealloc(SSLObject *self)
        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)
index 4c9ee765389f7fb3157ba3eee119803344f8b104..2e243a45cfce4654c84faace92dde728db260464 100644 (file)
@@ -1152,7 +1152,7 @@ static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
                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 @@ strop_replace(self, args)
        }
        else {
                new = PyString_FromStringAndSize(new_s, out_len);
-               free(new_s);
+               PyMem_FREE(new_s);
        }
        return new;
 }
index 71c152e2535ae5d53981570099f6f02c278f7205..f7b942690cbb4f9e0b13261d3cb06b03cf6a59a9 100644 (file)
@@ -139,7 +139,7 @@ newsadobject(arg)
        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 @@ sad_dealloc(xp)
        sadobject *xp;
 {
         close(xp->x_fd);
-       PyMem_DEL(xp);
+       PyObject_Del(xp);
 }
 
 static PyObject *
@@ -412,7 +412,7 @@ sad_getattr(xp, name)
 
 static sadstatusobject *
 sads_alloc() {
-       return PyObject_NEW(sadstatusobject, &Sadstatustype);
+       return PyObject_New(sadstatusobject, &Sadstatustype);
 }
 
 static void
index 75f20238e412499c590a4dd1e6c71459efbb385a..99c6fa129e770ed7767a0a8d1c6b895bb2914b05 100644 (file)
@@ -340,7 +340,7 @@ capture_dealloc(self)
                Py_DECREF(self->ob_svideo);
                self->ob_svideo = NULL;
        }
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -374,7 +374,7 @@ newcaptureobject(self, ptr, mustunlock)
 {
        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 @@ svideo_dealloc(self)
 {
        if (self->ob_svideo != NULL)
                (void) svCloseVideo(self->ob_svideo);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
@@ -1026,7 +1026,7 @@ newsvobject(svp)
 {
        svobject *p;
 
-       p = PyObject_NEW(svobject, &Svtype);
+       p = PyObject_New(svobject, &Svtype);
        if (p == NULL)
                return NULL;
        p->ob_svideo = svp;
index cb463d9dff499b7af28ad587a210e0058f63cbda..195a164100c208bc86be6da1836153c712744176 100644 (file)
@@ -58,12 +58,12 @@ static lockobject *
 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 @@ lock_dealloc(self)
        PyThread_release_lock(self->lock_lock);
        
        PyThread_free_lock(self->lock_lock);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
index c0d05f4fff4b5a1a10da266f2daea61f9de80ded..90a502974418783d3a7123dec03da2631095284e 100644 (file)
@@ -62,7 +62,7 @@ newXxoObject(arg)
        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 @@ Xxo_dealloc(self)
        XxoObject *self;
 {
        Py_XDECREF(self->x_attr);
-       PyMem_DEL(self);
+       PyObject_Del(self);
 }
 
 static PyObject *
index 810ffefcaf92e474cd8cb95fab1718066fd0d675..cff72932a9d2e3086015b6a9de8e7d58cfcdc951 100644 (file)
@@ -46,7 +46,7 @@ newcompobject(type)
      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 @@ Comp_dealloc(self)
     if (self->is_initialised)
       deflateEnd(&self->zst);
     Py_XDECREF(self->unused_data);
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static void
@@ -378,7 +378,7 @@ Decomp_dealloc(self)
 {
     inflateEnd(&self->zst);
     Py_XDECREF(self->unused_data);
-    PyMem_DEL(self);
+    PyObject_Del(self);
 }
 
 static char comp_compress__doc__[] =
index 05b1f11406d4f2dc0391a5c45e334dadaf5fb070..72d424253ccc3794e8d2e3f59e218be20068b08c 100644 (file)
@@ -188,11 +188,11 @@ PyBuffer_New(size)
                                "size must be zero or positive");
                return NULL;
        }
-       b = (PyBufferObject *)malloc(sizeof(*b) + size);
+       /* PyObject_New is inlined */
+       b = (PyBufferObject *) PyObject_MALLOC(sizeof(*b) + size);
        if ( b == NULL )
                return PyErr_NoMemory();
-       b->ob_type = &PyBuffer_Type;
-       _Py_NewReference((PyObject *)b);
+       PyObject_INIT((PyObject *)b, &PyBuffer_Type);
 
        b->b_base = NULL;
        b->b_ptr = (void *)(b + 1);
@@ -212,7 +212,7 @@ buffer_dealloc(self)
        PyBufferObject *self;
 {
        Py_XDECREF(self->b_base);
-       free((void *)self);
+       PyObject_DEL(self);
 }
 
 static int
index 6c7dba5f4edd01564e614f9e6a4cfa0f435420b3..bd95bc039face1ca95059fb7b1f8a4d03e819c35 100644 (file)
@@ -147,7 +147,7 @@ class_dealloc(op)
        Py_XDECREF(op->cl_getattr);
        Py_XDECREF(op->cl_setattr);
        Py_XDECREF(op->cl_delattr);
-       free((ANY *)op);
+       PyObject_DEL(op);
 }
 
 static PyObject *
@@ -561,7 +561,7 @@ instance_dealloc(inst)
 #endif /* Py_TRACE_REFS */
        Py_DECREF(inst->in_class);
        Py_XDECREF(inst->in_dict);
-       free((ANY *)inst);
+       PyObject_DEL(inst);
 }
 
 static PyObject *
@@ -1498,8 +1498,7 @@ PyMethod_New(func, self, class)
        im = free_list;
        if (im != NULL) {
                free_list = (PyMethodObject *)(im->im_self);
-               im->ob_type = &PyMethod_Type;
-               _Py_NewReference((PyObject *)im);
+               PyObject_INIT(im, &PyMethod_Type);
        }
        else {
                im = PyObject_NEW(PyMethodObject, &PyMethod_Type);
@@ -1691,8 +1690,8 @@ void
 PyMethod_Fini()
 {
        while (free_list) {
-               PyMethodObject *v = free_list;
-               free_list = (PyMethodObject *)(v->im_self);
-               PyMem_DEL(v);
+               PyMethodObject *im = free_list;
+               free_list = (PyMethodObject *)(im->im_self);
+               PyObject_DEL(im);
        }
 }
index 40e8672de2f06f9204bc621354d658c76f89854b..267ca95b7611d0b97676ec20f518ca5957dfe4e4 100644 (file)
@@ -151,7 +151,7 @@ PyCObject_dealloc(self)
            else
                  (self->destructor)(self->cobject);
          }
-       PyMem_DEL(self);
+       PyObject_DEL(self);
 }
 
 
index 05649428451eed0f0316c13e8f9c22aad508d593..42709ee09a30df298eea05e2201f22c61e5452bf 100644 (file)
@@ -166,13 +166,14 @@ PyObject *
 PyComplex_FromCComplex(cval)
        Py_complex cval;
 {
-       register PyComplexObject *op =
-               (PyComplexObject *) malloc(sizeof(PyComplexObject));
+       register PyComplexObject *op;
+
+       /* PyObject_New is inlined */
+       op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyComplex_Type;
+       PyObject_INIT(op, &PyComplex_Type);
        op->cval = cval;
-       _Py_NewReference((PyObject *)op);
        return (PyObject *) op;
 }
 
@@ -226,7 +227,7 @@ static void
 complex_dealloc(op)
        PyObject *op;
 {
-       PyMem_DEL(op);
+       PyObject_DEL(op);
 }
 
 
index beab457462eed8bba93209be564efacd1c14596d..6e7fa3d0ef6172bee4a68d68f5204b65d3064ff4 100644 (file)
@@ -277,7 +277,7 @@ dictresize(mp, minused)
                        break;
                }
        }
-       newtable = (dictentry *) malloc(sizeof(dictentry) * newsize);
+       newtable = PyMem_NEW(dictentry, newsize);
        if (newtable == NULL) {
                PyErr_NoMemory();
                return -1;
@@ -301,7 +301,8 @@ dictresize(mp, minused)
                }
        }
 
-       PyMem_XDEL(oldtable);
+       if (oldtable != NULL)
+               PyMem_DEL(oldtable);
        return 0;
 }
 
@@ -488,8 +489,9 @@ dict_dealloc(mp)
                        Py_DECREF(ep->me_value);
                }
        }
-       PyMem_XDEL(mp->ma_table);
-       PyMem_DEL(mp);
+       if (mp->ma_table != NULL)
+               PyMem_DEL(mp->ma_table);
+       PyObject_DEL(mp);
        Py_TRASHCAN_SAFE_END(mp)
 }
 
index c8b083e5f238ff46e7b23e554930cc452fae0939..7e0979f91d0bf49818428ec3da1ca456f55e44a9 100644 (file)
@@ -215,7 +215,7 @@ file_dealloc(f)
        if (f->f_mode != NULL) {
                Py_DECREF(f->f_mode);
        }
-       free((char *)f);
+       PyObject_DEL(f);
 }
 
 static PyObject *
index 77ef8d0917cf38c2130b508550bc15d02bb0c83b..69b66b7470df9dc0ebf44015fcf5fd66aee1cfa0 100644 (file)
@@ -98,9 +98,6 @@ double (*_Py_math_funcs_hack[])() = {
 #define BHEAD_SIZE     8       /* Enough for a 64-bit pointer */
 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
 
-#define PyMem_MALLOC   malloc
-#define PyMem_FREE     free
-
 struct _floatblock {
        struct _floatblock *next;
        PyFloatObject objects[N_FLOATOBJECTS];
@@ -115,9 +112,10 @@ static PyFloatObject *
 fill_free_list()
 {
        PyFloatObject *p, *q;
-       p = (PyFloatObject *)PyMem_MALLOC(sizeof(PyFloatBlock));
+       /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
+       p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
        if (p == NULL)
-               return (PyFloatObject *)PyErr_NoMemory();
+               return (PyFloatObject *) PyErr_NoMemory();
        ((PyFloatBlock *)p)->next = block_list;
        block_list = (PyFloatBlock *)p;
        p = &((PyFloatBlock *)p)->objects[0];
@@ -141,11 +139,11 @@ PyFloat_FromDouble(fval)
                if ((free_list = fill_free_list()) == NULL)
                        return NULL;
        }
+       /* PyObject_New is inlined */
        op = free_list;
        free_list = (PyFloatObject *)op->ob_type;
-       op->ob_type = &PyFloat_Type;
+       PyObject_INIT(op, &PyFloat_Type);
        op->ob_fval = fval;
-       _Py_NewReference((PyObject *)op);
        return (PyObject *) op;
 }
 
@@ -779,7 +777,7 @@ PyFloat_Fini()
                        }
                }
                else {
-                       PyMem_FREE(list);
+                       PyMem_FREE(list); /* XXX PyObject_FREE ??? */
                        bf++;
                }
                fsum += frem;
index 4c716cdcd6cae34539b3a77635ab10164dfb92b7..1e672bb5d799910da23d763bbabe1a004757c21d 100644 (file)
@@ -180,28 +180,27 @@ PyFrame_New(tstate, code, globals, locals)
        if (builtins != NULL && !PyDict_Check(builtins))
                builtins = NULL;
        if (free_list == NULL) {
+               /* PyObject_New is inlined */
                f = (PyFrameObject *)
-                       malloc(sizeof(PyFrameObject) +
-                              extras*sizeof(PyObject *));
+                       PyObject_MALLOC(sizeof(PyFrameObject) +
+                                       extras*sizeof(PyObject *));
                if (f == NULL)
                        return (PyFrameObject *)PyErr_NoMemory();
-               f->ob_type = &PyFrame_Type;
-               _Py_NewReference((PyObject *)f);
+               PyObject_INIT(f, &PyFrame_Type);
        }
        else {
                f = free_list;
                free_list = free_list->f_back;
                if (f->f_nlocals + f->f_stacksize < extras) {
                        f = (PyFrameObject *)
-                               realloc(f, sizeof(PyFrameObject) +
-                                       extras*sizeof(PyObject *));
+                               PyObject_REALLOC(f, sizeof(PyFrameObject) +
+                                                extras*sizeof(PyObject *));
                        if (f == NULL)
                                return (PyFrameObject *)PyErr_NoMemory();
                }
                else
                        extras = f->f_nlocals + f->f_stacksize;
-               f->ob_type = &PyFrame_Type;
-               _Py_NewReference((PyObject *)f);
+               PyObject_INIT(f, &PyFrame_Type);
        }
        if (builtins == NULL) {
                /* No builtins!  Make up a minimal one. */
@@ -376,6 +375,6 @@ PyFrame_Fini()
        while (free_list != NULL) {
                PyFrameObject *f = free_list;
                free_list = free_list->f_back;
-               PyMem_DEL(f);
+               PyObject_DEL(f);
        }
 }
index 562935c6a97104badb75ac73f1c05ff754d85630..a5e15cc9057ace7c1fe19d4ee9eb021a0cb66a69 100644 (file)
@@ -191,7 +191,7 @@ func_dealloc(op)
        Py_DECREF(op->func_name);
        Py_XDECREF(op->func_defaults);
        Py_XDECREF(op->func_doc);
-       PyMem_DEL(op);
+       PyObject_DEL(op);
 }
 
 static PyObject*
index 0c8eefc4fa77f5ab27910ab1c6fb3f3054467d18..79435a990a55c0c1a16e6d8a02e509b389af9991 100644 (file)
@@ -94,9 +94,6 @@ err_ovf(msg)
 #define BHEAD_SIZE     8       /* Enough for a 64-bit pointer */
 #define N_INTOBJECTS   ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
 
-#define PyMem_MALLOC   malloc
-#define PyMem_FREE     free
-
 struct _intblock {
        struct _intblock *next;
        PyIntObject objects[N_INTOBJECTS];
@@ -111,9 +108,10 @@ static PyIntObject *
 fill_free_list()
 {
        PyIntObject *p, *q;
-       p = (PyIntObject *)PyMem_MALLOC(sizeof(PyIntBlock));
+       /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
+       p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
        if (p == NULL)
-               return (PyIntObject *)PyErr_NoMemory();
+               return (PyIntObject *) PyErr_NoMemory();
        ((PyIntBlock *)p)->next = block_list;
        block_list = (PyIntBlock *)p;
        p = &((PyIntBlock *)p)->objects[0];
@@ -164,11 +162,11 @@ PyInt_FromLong(ival)
                if ((free_list = fill_free_list()) == NULL)
                        return NULL;
        }
+       /* PyObject_New is inlined */
        v = free_list;
        free_list = (PyIntObject *)v->ob_type;
-       v->ob_type = &PyInt_Type;
+       PyObject_INIT(v, &PyInt_Type);
        v->ob_ival = ival;
-       _Py_NewReference((PyObject *)v);
 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
        if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
                /* save this one for a following allocation */
@@ -933,7 +931,7 @@ PyInt_Fini()
                        }
                }
                else {
-                       PyMem_FREE(list);
+                       PyMem_FREE(list); /* XXX PyObject_FREE ??? */
                        bf++;
                }
                isum += irem;
index 005d7097cbcf68e4e215246d645148241fa46c7e..f70d19bdf1157bd1415cbbcfd619823cafcf3d8b 100644 (file)
@@ -70,7 +70,8 @@ PyList_New(size)
        if (nbytes / sizeof(PyObject *) != (size_t)size) {
                return PyErr_NoMemory();
        }
-       op = (PyListObject *) malloc(sizeof(PyListObject));
+       /* PyObject_NewVar is inlined */
+       op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject));
        if (op == NULL) {
                return PyErr_NoMemory();
        }
@@ -78,17 +79,15 @@ PyList_New(size)
                op->ob_item = NULL;
        }
        else {
-               op->ob_item = (PyObject **) malloc(nbytes);
+               op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
                if (op->ob_item == NULL) {
-                       free((ANY *)op);
+                       PyObject_FREE(op);
                        return PyErr_NoMemory();
                }
        }
-       op->ob_type = &PyList_Type;
-       op->ob_size = size;
+       PyObject_INIT_VAR(op, &PyList_Type, size);
        for (i = 0; i < size; i++)
                op->ob_item[i] = NULL;
-       _Py_NewReference((PyObject *)op);
        return (PyObject *) op;
 }
 
@@ -225,9 +224,9 @@ list_dealloc(op)
                while (--i >= 0) {
                        Py_XDECREF(op->ob_item[i]);
                }
-               free((ANY *)op->ob_item);
+               PyMem_FREE(op->ob_item);
        }
-       free((ANY *)op);
+       PyObject_DEL(op);
        Py_TRASHCAN_SAFE_END(op)
 }
 
@@ -501,7 +500,8 @@ list_ass_slice(a, ilow, ihigh, v)
        else { /* Insert d items; recycle ihigh-ilow items */
                NRESIZE(item, PyObject *, a->ob_size + d);
                if (item == NULL) {
-                       PyMem_XDEL(recycle);
+                       if (recycle != NULL)
+                               PyMem_DEL(recycle);
                        PyErr_NoMemory();
                        return -1;
                }
index a9ce6f3c3b864f50de2c8fd6fa49fc8277b7e4ea..4bf89d9aaf20312825100f4449ab41fa40e9413d 100644 (file)
@@ -995,7 +995,7 @@ static void
 long_dealloc(v)
        PyObject *v;
 {
-       PyMem_DEL(v);
+       PyObject_DEL(v);
 }
 
 static PyObject *
index 5c69744b7bf21096008be7f70c2b08f0b82183ed..8b67a87e79abf376a9ecd86a8e5a4781f72c3bda 100644 (file)
@@ -46,8 +46,7 @@ PyCFunction_New(ml, self)
        op = free_list;
        if (op != NULL) {
                free_list = (PyCFunctionObject *)(op->m_self);
-               op->ob_type = &PyCFunction_Type;
-               _Py_NewReference((PyObject *)op);
+               PyObject_INIT(op, &PyCFunction_Type);
        }
        else {
                op = PyObject_NEW(PyCFunctionObject, &PyCFunction_Type);
@@ -288,6 +287,6 @@ PyCFunction_Fini()
        while (free_list) {
                PyCFunctionObject *v = free_list;
                free_list = (PyCFunctionObject *)(v->m_self);
-               PyMem_DEL(v);
+               PyObject_DEL(v);
        }
 }
index 7dfca731e51981706a686d200a26874e2a92dd90..808e27ad68936f6a408c84d6ab7014bb49afffbe 100644 (file)
@@ -170,7 +170,7 @@ module_dealloc(m)
                _PyModule_Clear((PyObject *)m);
                Py_DECREF(m->md_dict);
        }
-       free((char *)m);
+       PyObject_DEL(m);
 }
 
 static PyObject *
index ed276e27ed400fed234f815ef0fe36345ee20c37..61954798c76811b2f05979ade0c81cc380bc4326 100644 (file)
@@ -112,50 +112,68 @@ inc_count(tp)
 }
 #endif
 
-#ifndef MS_COREDLL
 PyObject *
-_PyObject_New(tp)
-       PyTypeObject *tp;
-#else
-PyObject *
-_PyObject_New(tp,op)
-       PyTypeObject *tp;
+PyObject_Init(op, tp)
        PyObject *op;
-#endif
+       PyTypeObject *tp;
 {
-#ifndef MS_COREDLL
-       PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
-#endif
-       if (op == NULL)
-               return PyErr_NoMemory();
+       if (op == NULL) {
+               PyErr_SetString(PyExc_SystemError,
+                               "NULL object passed to PyObject_Init");
+               return op;
+       }
+       /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
        op->ob_type = tp;
        _Py_NewReference(op);
        return op;
 }
 
-#ifndef MS_COREDLL
 PyVarObject *
-_PyObject_NewVar(tp, size)
+PyObject_InitVar(op, tp, size)
+       PyVarObject *op;
        PyTypeObject *tp;
        int size;
-#else
+{
+       if (op == NULL) {
+               PyErr_SetString(PyExc_SystemError,
+                               "NULL object passed to PyObject_InitVar");
+               return op;
+       }
+       /* Any changes should be reflected in PyObject_INIT_VAR */
+       op->ob_size = size;
+       op->ob_type = tp;
+       _Py_NewReference((PyObject *)op);
+       return op;
+}
+
+PyObject *
+_PyObject_New(tp)
+       PyTypeObject *tp;
+{
+       PyObject *op;
+       op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
+       if (op == NULL)
+               return PyErr_NoMemory();
+       return PyObject_INIT(op, tp);
+}
+
 PyVarObject *
-_PyObject_NewVar(tp, size, op)
+_PyObject_NewVar(tp, size)
        PyTypeObject *tp;
        int size;
-       PyVarObject *op;
-#endif
 {
-#ifndef MS_COREDLL
-       PyVarObject *op = (PyVarObject *)
-               malloc(tp->tp_basicsize + size * tp->tp_itemsize);
-#endif
+       PyVarObject *op;
+       op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
        if (op == NULL)
                return (PyVarObject *)PyErr_NoMemory();
-       op->ob_type = tp;
-       op->ob_size = size;
-       _Py_NewReference((PyObject *)op);
-       return op;
+       return PyObject_INIT_VAR(op, tp, size);
+}
+
+void
+_PyObject_Del(op)
+       PyObject *op;
+{
+       PyObject_FREE(op);
 }
 
 int
@@ -888,30 +906,21 @@ PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
 int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
 
 
-/* Malloc wrappers (see mymalloc.h) */
-
-/* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
+/* Python's malloc wrappers (see mymalloc.h) */
 
 ANY *
-Py_Malloc(nbytes)
+PyMem_Malloc(nbytes)
        size_t nbytes;
 {
-       ANY *p;
 #if _PyMem_EXTRA > 0
        if (nbytes == 0)
                nbytes = _PyMem_EXTRA;
 #endif
-       p = malloc(nbytes);
-       if (p != NULL)
-               return p;
-       else {
-               PyErr_NoMemory();
-               return NULL;
-       }
+       return PyMem_MALLOC(nbytes);
 }
 
 ANY *
-Py_Realloc(p, nbytes)
+PyMem_Realloc(p, nbytes)
        ANY *p;
        size_t nbytes;
 {
@@ -919,52 +928,39 @@ Py_Realloc(p, nbytes)
        if (nbytes == 0)
                nbytes = _PyMem_EXTRA;
 #endif
-       p = realloc(p, nbytes);
-       if (p != NULL)
-               return p;
-       else {
-               PyErr_NoMemory();
-               return NULL;
-       }
+       return PyMem_REALLOC(p, nbytes);
 }
 
 void
-Py_Free(p)
+PyMem_Free(p)
        ANY *p;
 {
-       free(p);
+       PyMem_FREE(p);
 }
 
-/* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
+
+/* Python's object malloc wrappers (see objimpl.h) */
 
 ANY *
-PyMem_Malloc(nbytes)
+PyObject_Malloc(nbytes)
        size_t nbytes;
 {
-#if _PyMem_EXTRA > 0
-       if (nbytes == 0)
-               nbytes = _PyMem_EXTRA;
-#endif
-       return malloc(nbytes);
+       return PyObject_MALLOC(nbytes);
 }
 
 ANY *
-PyMem_Realloc(p, nbytes)
+PyObject_Realloc(p, nbytes)
        ANY *p;
        size_t nbytes;
 {
-#if _PyMem_EXTRA > 0
-       if (nbytes == 0)
-               nbytes = _PyMem_EXTRA;
-#endif
-       return realloc(p, nbytes);
+       return PyObject_REALLOC(p, nbytes);
 }
 
 void
-PyMem_Free(p)
+PyObject_Free(p)
        ANY *p;
 {
-       free(p);
+       PyObject_FREE(p);
 }
 
 
index c603ac96edc748a466f6392c42ca908a0c85088b..807cf51234b9298f6c2aff49feceb22b13304874 100644 (file)
@@ -61,7 +61,7 @@ static void
 range_dealloc(r)
        rangeobject *r;
 {
-       PyMem_DEL(r);
+       PyObject_DEL(r);
 }
 
 static PyObject *
index a232296705788a41e4e11711e0ac3fb9ff2583f9..eb4972b49125eb567a8bfe4061d497e6dc49c61b 100644 (file)
@@ -57,8 +57,7 @@ PySlice_New(start, stop, step)
        PyObject *stop;
        PyObject *step;
 {
-       PySliceObject *obj =
-               (PySliceObject *) PyObject_NEW(PySliceObject, &PySlice_Type);
+       PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type);
 
        if (step == NULL) step = Py_None;
        Py_INCREF(step);
@@ -115,7 +114,7 @@ slice_dealloc(r)
        Py_DECREF(r->step);
        Py_DECREF(r->start);
        Py_DECREF(r->stop);
-       PyMem_DEL(r);
+       PyObject_DEL(r);
 }
 
 static PyObject *
index 5b5ed9c40efa4ee4edb93573bc57a3e0c137ef28..288f26e22081608e0d1e342771a91f8f273edf32 100644 (file)
@@ -92,19 +92,19 @@ PyString_FromStringAndSize(str, size)
                return (PyObject *)op;
        }
 #endif /* DONT_SHARE_SHORT_STRINGS */
+
+       /* PyObject_NewVar is inlined */
        op = (PyStringObject *)
-               malloc(sizeof(PyStringObject) + size * sizeof(char));
+               PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyString_Type;
-       op->ob_size = size;
+       PyObject_INIT_VAR(op, &PyString_Type, size);
 #ifdef CACHE_HASH
        op->ob_shash = -1;
 #endif
 #ifdef INTERN_STRINGS
        op->ob_sinterned = NULL;
 #endif
-       _Py_NewReference((PyObject *)op);
        if (str != NULL)
                memcpy(op->ob_sval, str, size);
        op->ob_sval[size] = '\0';
@@ -142,19 +142,19 @@ PyString_FromString(str)
                return (PyObject *)op;
        }
 #endif /* DONT_SHARE_SHORT_STRINGS */
+
+       /* PyObject_NewVar is inlined */
        op = (PyStringObject *)
-               malloc(sizeof(PyStringObject) + size * sizeof(char));
+               PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyString_Type;
-       op->ob_size = size;
+       PyObject_INIT_VAR(op, &PyString_Type, size);
 #ifdef CACHE_HASH
        op->ob_shash = -1;
 #endif
 #ifdef INTERN_STRINGS
        op->ob_sinterned = NULL;
 #endif
-       _Py_NewReference((PyObject *)op);
        strcpy(op->ob_sval, str);
 #ifndef DONT_SHARE_SHORT_STRINGS
        if (size == 0) {
@@ -172,7 +172,7 @@ static void
 string_dealloc(op)
        PyObject *op;
 {
-       PyMem_DEL(op);
+       PyObject_DEL(op);
 }
 
 int
@@ -307,19 +307,18 @@ string_concat(a, bb)
                return (PyObject *)a;
        }
        size = a->ob_size + b->ob_size;
+       /* PyObject_NewVar is inlined */
        op = (PyStringObject *)
-               malloc(sizeof(PyStringObject) + size * sizeof(char));
+               PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyString_Type;
-       op->ob_size = size;
+       PyObject_INIT_VAR(op, &PyString_Type, size);
 #ifdef CACHE_HASH
        op->ob_shash = -1;
 #endif
 #ifdef INTERN_STRINGS
        op->ob_sinterned = NULL;
 #endif
-       _Py_NewReference((PyObject *)op);
        memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
        memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
        op->ob_sval[size] = '\0';
@@ -342,19 +341,18 @@ string_repeat(a, n)
                Py_INCREF(a);
                return (PyObject *)a;
        }
+       /* PyObject_NewVar is inlined */
        op = (PyStringObject *)
-               malloc(sizeof(PyStringObject) + size * sizeof(char));
+               PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyString_Type;
-       op->ob_size = size;
+       PyObject_INIT_VAR(op, &PyString_Type, size);
 #ifdef CACHE_HASH
        op->ob_shash = -1;
 #endif
 #ifdef INTERN_STRINGS
        op->ob_sinterned = NULL;
 #endif
-       _Py_NewReference((PyObject *)op);
        for (i = 0; i < size; i += a->ob_size)
                memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
        op->ob_sval[size] = '\0';
@@ -1498,7 +1496,7 @@ mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
                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;
@@ -1593,7 +1591,7 @@ string_replace(self, args)
        }
        else {
                new = PyString_FromStringAndSize(new_s, out_len);
-               free(new_s);
+               PyMem_FREE(new_s);
        }
        return new;
 }
@@ -2273,10 +2271,10 @@ _PyString_Resize(pv, newsize)
 #endif
        _Py_ForgetReference(v);
        *pv = (PyObject *)
-               realloc((char *)v,
+               PyObject_REALLOC((char *)v,
                        sizeof(PyStringObject) + newsize * sizeof(char));
        if (*pv == NULL) {
-               PyMem_DEL(v);
+               PyObject_DEL(v);
                PyErr_NoMemory();
                return -1;
        }
index d1627a9a2c02c5dbbc0fff3ac46551654525023f..d5d6a079eff39e2fed2ecad2154245fc653bca9a 100644 (file)
@@ -80,10 +80,12 @@ PyTuple_New(size)
 #ifdef COUNT_ALLOCS
                fast_tuple_allocs++;
 #endif
+               /* PyObject_InitVar is inlined */
 #ifdef Py_TRACE_REFS
-               op->ob_type = &PyTuple_Type;
                op->ob_size = size;
+               op->ob_type = &PyTuple_Type;
 #endif
+               _Py_NewReference((PyObject *)op);
        }
        else
 #endif
@@ -96,17 +98,15 @@ PyTuple_New(size)
                {
                        return PyErr_NoMemory();
                }
-               ;
-               op = (PyTupleObject *) malloc(nbytes);
+               /* PyObject_NewVar is inlined */
+               op = (PyTupleObject *) PyObject_MALLOC(nbytes);
                if (op == NULL)
                        return PyErr_NoMemory();
 
-               op->ob_type = &PyTuple_Type;
-               op->ob_size = size;
+               PyObject_INIT_VAR(op, &PyTuple_Type, size);
        }
        for (i = 0; i < size; i++)
                op->ob_item[i] = NULL;
-       _Py_NewReference((PyObject *)op);
 #if MAXSAVESIZE > 0
        if (size == 0) {
                free_tuples[0] = op;
@@ -193,7 +193,7 @@ tupledealloc(op)
                }
 #endif
        }
-       free((ANY *)op);
+       PyObject_DEL(op);
 done:
        Py_TRASHCAN_SAFE_END(op)
 }
@@ -530,11 +530,11 @@ _PyTuple_Resize(pv, newsize, last_is_sticky)
 #endif         
        {
                sv = (PyTupleObject *)
-                       realloc((char *)v,
+                       PyObject_REALLOC((char *)v,
                                sizeof(PyTupleObject) + newsize * sizeof(PyObject *));
                *pv = (PyObject *) sv;
                if (sv == NULL) {
-                       PyMem_DEL(v);
+                       PyObject_DEL(v);
                        PyErr_NoMemory();
                        return -1;
                }
@@ -569,7 +569,7 @@ PyTuple_Fini()
                while (p) {
                        q = p;
                        p = (PyTupleObject *)(p->ob_item[0]);
-                       PyMem_DEL(q);
+                       PyObject_DEL(q);
                }
        }
 #endif
index 7a68dd40104d51b31583b5f30004ec3d9afa33ba..601b98716c4ff5a158a67e6c408499d2022f20f6 100644 (file)
@@ -200,14 +200,13 @@ PyUnicodeObject *_PyUnicode_New(int length)
         unicode = unicode_freelist;
         unicode_freelist = *(PyUnicodeObject **)unicode_freelist;
         unicode_freelist_size--;
-        unicode->ob_type = &PyUnicode_Type;
-        _Py_NewReference((PyObject *)unicode);
+       PyObject_INIT(unicode, &PyUnicode_Type);
        if (unicode->str) {
            /* Keep-Alive optimization: we only upsize the buffer,
               never downsize it. */
            if ((unicode->length < length) &&
                _PyUnicode_Resize(unicode, length)) {
-               free(unicode->str);
+               PyMem_DEL(unicode->str);
                goto onError;
            }
        }
@@ -233,7 +232,7 @@ PyUnicodeObject *_PyUnicode_New(int length)
 
  onError:
     _Py_ForgetReference((PyObject *)unicode);
-    PyMem_DEL(unicode);
+    PyObject_DEL(unicode);
     return NULL;
 }
 
@@ -243,7 +242,7 @@ void _PyUnicode_Free(register PyUnicodeObject *unicode)
     if (unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
         /* Keep-Alive optimization */
        if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
-           free(unicode->str);
+           PyMem_DEL(unicode->str);
            unicode->str = NULL;
            unicode->length = 0;
        }
@@ -257,9 +256,9 @@ void _PyUnicode_Free(register PyUnicodeObject *unicode)
         unicode_freelist_size++;
     }
     else {
-       free(unicode->str);
+       PyMem_DEL(unicode->str);
        Py_XDECREF(unicode->utf8str);
-        PyMem_DEL(unicode);
+       PyObject_DEL(unicode);
     }
 }
 
@@ -4662,9 +4661,9 @@ _PyUnicode_Fini()
        PyUnicodeObject *v = u;
        u = *(PyUnicodeObject **)u;
        if (v->str)
-           free(v->str);
+           PyMem_DEL(v->str);
        Py_XDECREF(v->utf8str);
-       free(v);
+       PyObject_DEL(v);
     }
     Py_XDECREF(unicode_empty);
 }
index c5b518f7bdde7aca619848a7c8a2554edef3781e..91c98339c75ebea663e8bafa475bdb988482d9f3 100644 (file)
@@ -71,7 +71,7 @@ xx_dealloc(xp)
        xxobject *xp;
 {
        Py_XDECREF(xp->x_attr);
-       PyMem_DEL(xp);
+       PyObject_DEL(xp);
 }
 
 static PyObject *
index 3dccc3db319f295f896627589f30b7b85d76befb..bba9bc29835ec21c9d61cab8d40c9d7bf6948655 100644 (file)
@@ -370,7 +370,7 @@ PyHKEY_deallocFunc(PyObject *ob)
        PyHKEYObject *obkey = (PyHKEYObject *)ob;
        if (obkey->hkey)
                RegCloseKey((HKEY)obkey->hkey);
-       PyMem_DEL(ob);
+       PyObject_DEL(ob);
 }
 
 static int
@@ -604,12 +604,14 @@ PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
 PyObject *
 PyHKEY_FromHKEY(HKEY h)
 {
-       PyHKEYObject *op = (PyHKEYObject *) malloc(sizeof(PyHKEYObject));
+       PyHKEYObject *op;
+
+       /* PyObject_New is inlined */
+       op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyHKEY_Type;
+       PyObject_INIT(op, &PyHKEY_Type);
        op->hkey = h;
-       _Py_NewReference((PyObject *)op);
        return (PyObject *)op;
 }
 
@@ -1348,7 +1350,7 @@ PySetValueEx(PyObject *self, PyObject *args)
        Py_BEGIN_ALLOW_THREADS
        rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
        Py_END_ALLOW_THREADS
-       PyMem_Free(data);
+       PyMem_DEL(data);
        if (rc != ERROR_SUCCESS)
                return PyErr_SetFromWindowsErrWithFunction(rc,
                                                           "RegSetValueEx");
index 3dccc3db319f295f896627589f30b7b85d76befb..bba9bc29835ec21c9d61cab8d40c9d7bf6948655 100644 (file)
@@ -370,7 +370,7 @@ PyHKEY_deallocFunc(PyObject *ob)
        PyHKEYObject *obkey = (PyHKEYObject *)ob;
        if (obkey->hkey)
                RegCloseKey((HKEY)obkey->hkey);
-       PyMem_DEL(ob);
+       PyObject_DEL(ob);
 }
 
 static int
@@ -604,12 +604,14 @@ PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
 PyObject *
 PyHKEY_FromHKEY(HKEY h)
 {
-       PyHKEYObject *op = (PyHKEYObject *) malloc(sizeof(PyHKEYObject));
+       PyHKEYObject *op;
+
+       /* PyObject_New is inlined */
+       op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
        if (op == NULL)
                return PyErr_NoMemory();
-       op->ob_type = &PyHKEY_Type;
+       PyObject_INIT(op, &PyHKEY_Type);
        op->hkey = h;
-       _Py_NewReference((PyObject *)op);
        return (PyObject *)op;
 }
 
@@ -1348,7 +1350,7 @@ PySetValueEx(PyObject *self, PyObject *args)
        Py_BEGIN_ALLOW_THREADS
        rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
        Py_END_ALLOW_THREADS
-       PyMem_Free(data);
+       PyMem_DEL(data);
        if (rc != ERROR_SUCCESS)
                return PyErr_SetFromWindowsErrWithFunction(rc,
                                                           "RegSetValueEx");
index d6261398cb3503a2a797a83729f9748c272bfb6c..9bf770eec5b0f47d21b6f4208b4b622267527819 100644 (file)
@@ -89,7 +89,7 @@ PyOS_StdioReadline(prompt)
        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 @@ PyOS_StdioReadline(prompt)
        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 @@ PyOS_StdioReadline(prompt)
        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 *));
 
index 5b0d99050fcc709994e98d7e9b91bf0971d76790..6d20d9337a8497c70865966cbd3f375d033db23f 100644 (file)
@@ -192,7 +192,7 @@ parsetok(tok, g, start, err_ret)
                err_ret->offset = tok->cur - tok->buf;
                if (tok->buf != NULL) {
                        int len = tok->inp - tok->buf;
-                       err_ret->text = malloc(len + 1);
+                       err_ret->text = PyMem_NEW(char, len + 1);
                        if (err_ret->text != NULL) {
                                if (len > 0)
                                        strncpy(err_ret->text, tok->buf, len);
index 72924854309bb8bd69a2dd49456b709c6ae3eb13..34c3b01d9a668731f438ddc5ace20f706d0a3460 100644 (file)
@@ -139,7 +139,7 @@ getgrammar(filename)
                                        putc(' ', stderr);
                        }
                        fprintf(stderr, "^\n");
-                       free(err.text);
+                       PyMem_DEL(err.text);
                }
                Py_Exit(1);
        }
@@ -196,7 +196,7 @@ PyOS_Readline(prompt)
        char *prompt;
 {
        int n = 1000;
-       char *p = malloc(n);
+       char *p = PyMem_MALLOC(n);
        char *q;
        if (p == NULL)
                return NULL;
@@ -209,7 +209,7 @@ PyOS_Readline(prompt)
        n = strlen(p);
        if (n > 0 && p[n-1] != '\n')
                p[n-1] = '\n';
-       return realloc(p, n+1);
+       return PyMem_REALLOC(p, n+1);
 }
 
 #ifdef HAVE_STDARG_PROTOTYPES
index e4b058e08577ee78944dce7597848573130b870e..7d9a273039b24ffea3925ee8f50e22aecd1601aa 100644 (file)
@@ -219,26 +219,27 @@ tok_nextc(tok)
                        if (new == NULL)
                                tok->done = E_INTR;
                        else if (*new == '\0') {
-                               free(new);
+                               PyMem_FREE(new);
                                tok->done = E_EOF;
                        }
                        else if (tok->start != NULL) {
                                int start = tok->start - tok->buf;
                                int oldlen = tok->cur - tok->buf;
                                int newlen = oldlen + strlen(new);
-                               char *buf = realloc(tok->buf, newlen+1);
+                               char *buf = tok->buf;
+                               PyMem_RESIZE(buf, char, newlen+1);
                                tok->lineno++;
                                if (buf == NULL) {
-                                       free(tok->buf);
+                                       PyMem_DEL(tok->buf);
                                        tok->buf = NULL;
-                                       free(new);
+                                       PyMem_FREE(new);
                                        tok->done = E_NOMEM;
                                        return EOF;
                                }
                                tok->buf = buf;
                                tok->cur = tok->buf + oldlen;
                                strcpy(tok->buf + oldlen, new);
-                               free(new);
+                               PyMem_FREE(new);
                                tok->inp = tok->buf + newlen;
                                tok->end = tok->inp + 1;
                                tok->start = tok->buf + start;
@@ -246,7 +247,7 @@ tok_nextc(tok)
                        else {
                                tok->lineno++;
                                if (tok->buf != NULL)
-                                       free(tok->buf);
+                                       PyMem_DEL(tok->buf);
                                tok->buf = new;
                                tok->cur = tok->buf;
                                tok->inp = strchr(tok->buf, '\0');
index 698114eff47aaa394c45ce93c4cf40436dcf579f..1df95d7a9527addb71df2888b8cc01bb74d543dc 100644 (file)
@@ -1875,7 +1875,7 @@ builtin_raw_input(self, args)
                else { /* strip trailing '\n' */
                        result = PyString_FromStringAndSize(s, strlen(s)-1);
                }
-               free(s);
+               PyMem_FREE(s);
                return result;
        }
        if (v != NULL) {
index d0958bdfcf319e6d9b4b9c54f804907fc0a8c079..1a21ee7b13dde9d85eb6a793ec8e04457085e192 100644 (file)
@@ -2558,7 +2558,8 @@ call_function(func, arg, kw)
                class);
        
        Py_DECREF(arg);
-       PyMem_XDEL(k);
+       if (k != NULL)
+               PyMem_DEL(k);
        
        return result;
 }
index 58354c6b0a639cfdc83c2cdea8800e960465b546..a387317ef3c9542d5bec3cd56ed741f3e8903792 100644 (file)
@@ -112,7 +112,7 @@ code_dealloc(co)
        Py_XDECREF(co->co_filename);
        Py_XDECREF(co->co_name);
        Py_XDECREF(co->co_lnotab);
-       PyMem_DEL(co);
+       PyObject_DEL(co);
 }
 
 static PyObject *
index a65614c24657a2f181ca1e5f2d7402cf491dccc7..c224752c57611ba0ba89a0b5427108dba93e2659 100644 (file)
@@ -124,7 +124,7 @@ _PyImport_Init()
                ++countD;
        for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
                ++countS;
-       filetab = malloc((countD + countS + 1) * sizeof(struct filedescr));
+       filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
        memcpy(filetab, _PyImport_DynLoadFiletab,
               countD * sizeof(struct filedescr));
        memcpy(filetab + countD, _PyImport_StandardFiletab,
@@ -2398,10 +2398,10 @@ initimp()
 }
 
 
-/* API for embedding applications that want to add their own entries to the
-   table of built-in modules.  This should normally be called *before*
-   Py_Initialize().  When the malloc() or realloc() call fails, -1 is returned
-   and the existing table is unchanged.
+/* API for embedding applications that want to add their own entries
+   to the table of built-in modules.  This should normally be called
+   *before* Py_Initialize().  When the table resize fails, -1 is
+   returned and the existing table is unchanged.
 
    After a similar function by Just van Rossum. */
 
@@ -2422,10 +2422,8 @@ PyImport_ExtendInittab(newtab)
                ;
 
        /* Allocate new memory for the combined table */
-       if (our_copy == NULL)
-               p = malloc((i+n+1) * sizeof(struct _inittab));
-       else
-               p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab));
+       p = our_copy;
+       PyMem_RESIZE(p, struct _inittab, i+n+1);
        if (p == NULL)
                return -1;
 
index 05ac6e9ee5bb4a8d96205f2aa6050b3725c20f81..fe9e000418455338b507c63b24a2836a064083b2 100644 (file)
@@ -514,17 +514,17 @@ r_object(p)
                        PyErr_SetString(PyExc_ValueError, "bad marshal data");
                        return NULL;
                }
-               buffer = (char *)Py_Malloc(n);
+               buffer = PyMem_NEW(char, n);
                if (buffer == NULL)
-                   return NULL;
+                       return PyErr_NoMemory();
                if (r_string(buffer, (int)n, p) != n) {
-                       free(buffer);
+                       PyMem_DEL(buffer);
                        PyErr_SetString(PyExc_EOFError,
                                "EOF read where object expected");
                        return NULL;
                }
                v = PyUnicode_DecodeUTF8(buffer, n, NULL);
-               free(buffer);
+               PyMem_DEL(buffer);
                return v;
            }
            
index 49e616e92f2492279acbd34d5d7ee3ebccd91809..ee6ce07ba19245d1102dc36b3e6684a5e59a7558 100644 (file)
@@ -543,7 +543,7 @@ PyRun_InteractiveOne(fp, filename)
        if (n == NULL) {
                if (err.error == E_EOF) {
                        if (err.text)
-                               free(err.text);
+                               PyMem_DEL(err.text);
                        return E_EOF;
                }
                err_input(&err);
@@ -1009,7 +1009,7 @@ err_input(err)
        v = Py_BuildValue("(ziiz)", err->filename,
                            err->lineno, err->offset, err->text);
        if (err->text != NULL) {
-               free(err->text);
+               PyMem_DEL(err->text);
                err->text = NULL;
        }
        switch (err->error) {
index e1148cd26284adccf162d18a9565e9b706e3c0d1..0b8157d5a51aeaf325b92de40ec7bfd78ceca221 100644 (file)
@@ -71,7 +71,7 @@ tb_dealloc(tb)
        Py_TRASHCAN_SAFE_BEGIN(tb)
        Py_XDECREF(tb->tb_next);
        Py_XDECREF(tb->tb_frame);
-       PyMem_DEL(tb);
+       PyObject_DEL(tb);
        Py_TRASHCAN_SAFE_END(tb)
 }