From b18618dab7b6b85bb05b084693706e59211fa180 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 3 May 2000 23:44:39 +0000 Subject: Vladimir Marangozov's long-awaited malloc restructuring. For more comments, read the patches@python.org archives. For documentation read the comments in mymalloc.h and objimpl.h. (This is not exactly what Vladimir posted to the patches list; I've made a few changes, and Vladimir sent me a fix in private email for a problem that only occurs in debug mode. I'm also holding back on his change to main.c, which seems unnecessary to me.) --- Include/mymalloc.h | 133 ++++++++++++++++++++++++------ Include/objimpl.h | 212 ++++++++++++++++++++++++++++++++++++++++++------ Modules/_cursesmodule.c | 8 +- Modules/_sre.c | 8 +- Modules/_tkinter.c | 8 +- Modules/almodule.c | 8 +- Modules/arraymodule.c | 9 +- Modules/bsddbmodule.c | 8 +- Modules/cPickle.c | 14 ++-- Modules/cStringIO.c | 10 +-- Modules/cdmodule.c | 8 +- Modules/clmodule.c | 4 +- Modules/dbmmodule.c | 4 +- Modules/dlmodule.c | 4 +- Modules/flmodule.c | 8 +- Modules/fmmodule.c | 4 +- Modules/gdbmmodule.c | 4 +- Modules/getpath.c | 2 +- Modules/linuxaudiodev.c | 4 +- Modules/md5module.c | 4 +- Modules/mmapmodule.c | 6 +- Modules/mpzmodule.c | 4 +- Modules/newmodule.c | 2 +- Modules/nismodule.c | 4 +- Modules/parsermodule.c | 8 +- Modules/pcremodule.c | 4 +- Modules/pyexpat.c | 4 +- Modules/readline.c | 14 +++- Modules/regexmodule.c | 7 +- Modules/rotormodule.c | 28 ++++--- Modules/selectmodule.c | 6 +- Modules/shamodule.c | 4 +- Modules/socketmodule.c | 18 ++-- Modules/stropmodule.c | 4 +- Modules/sunaudiodev.c | 6 +- Modules/svmodule.c | 8 +- Modules/threadmodule.c | 6 +- Modules/xxmodule.c | 4 +- Modules/zlibmodule.c | 6 +- Objects/bufferobject.c | 8 +- Objects/classobject.c | 13 ++- Objects/cobject.c | 2 +- Objects/complexobject.c | 11 +-- Objects/dictobject.c | 10 ++- Objects/fileobject.c | 2 +- Objects/floatobject.c | 14 ++-- Objects/frameobject.c | 17 ++-- Objects/funcobject.c | 2 +- Objects/intobject.c | 14 ++-- Objects/listobject.c | 18 ++-- Objects/longobject.c | 2 +- Objects/methodobject.c | 5 +- Objects/moduleobject.c | 2 +- Objects/object.c | 124 ++++++++++++++-------------- Objects/rangeobject.c | 2 +- Objects/sliceobject.c | 5 +- Objects/stringobject.c | 40 +++++---- Objects/tupleobject.c | 20 ++--- Objects/unicodeobject.c | 17 ++-- Objects/xxobject.c | 2 +- PC/_winreg.c | 12 +-- PC/winreg.c | 12 +-- Parser/myreadline.c | 12 +-- Parser/parsetok.c | 2 +- Parser/pgenmain.c | 6 +- Parser/tokenizer.c | 13 +-- Python/bltinmodule.c | 2 +- Python/ceval.c | 3 +- Python/compile.c | 2 +- Python/import.c | 16 ++-- Python/marshal.c | 8 +- Python/pythonrun.c | 4 +- Python/traceback.c | 2 +- 73 files changed, 641 insertions(+), 390 deletions(-) diff --git a/Include/mymalloc.h b/Include/mymalloc.h index 558af9d..df3113b 100644 --- a/Include/mymalloc.h +++ b/Include/mymalloc.h @@ -57,6 +57,8 @@ PERFORMANCE OF THIS SOFTWARE. #include #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 diff --git a/Include/objimpl.h b/Include/objimpl.h index c093466..71dbb93 100644 --- a/Include/objimpl.h +++ b/Include/objimpl.h @@ -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 } diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index f76bf03..d654b29 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -273,7 +273,7 @@ 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; diff --git a/Modules/_sre.c b/Modules/_sre.c index 9eec035..47b80c5 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -848,7 +848,7 @@ _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* diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 008378d..15cc7e7 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -469,7 +469,7 @@ 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(); } diff --git a/Modules/almodule.c b/Modules/almodule.c index 1033c07..c75fe89 100644 --- a/Modules/almodule.c +++ b/Modules/almodule.c @@ -648,7 +648,7 @@ 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 * diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index dcd931b..9a09c7d 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -346,7 +346,7 @@ 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 diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c index 16178ed..97a8e8b 100644 --- a/Modules/bsddbmodule.c +++ b/Modules/bsddbmodule.c @@ -89,7 +89,7 @@ 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 diff --git a/Modules/cPickle.c b/Modules/cPickle.c index aa2c7cb..73cb6ba 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -171,7 +171,7 @@ 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); } diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c index e816178..557545f 100644 --- a/Modules/cStringIO.c +++ b/Modules/cStringIO.c @@ -56,7 +56,7 @@ 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; diff --git a/Modules/cdmodule.c b/Modules/cdmodule.c index 1b414c1..4a04e43 100644 --- a/Modules/cdmodule.c +++ b/Modules/cdmodule.c @@ -447,7 +447,7 @@ 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; diff --git a/Modules/clmodule.c b/Modules/clmodule.c index 05b00da..976346a 100644 --- a/Modules/clmodule.c +++ b/Modules/clmodule.c @@ -673,7 +673,7 @@ 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; diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c index 501c3f4..d4fc1bf 100644 --- a/Modules/dbmmodule.c +++ b/Modules/dbmmodule.c @@ -62,7 +62,7 @@ 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 diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c index 76d0540..b40e05e 100644 --- a/Modules/dlmodule.c +++ b/Modules/dlmodule.c @@ -54,7 +54,7 @@ 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 * diff --git a/Modules/flmodule.c b/Modules/flmodule.c index c921d4b..c6ce439 100644 --- a/Modules/flmodule.c +++ b/Modules/flmodule.c @@ -332,7 +332,7 @@ 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; diff --git a/Modules/fmmodule.c b/Modules/fmmodule.c index dd5b397..21f08d8 100644 --- a/Modules/fmmodule.c +++ b/Modules/fmmodule.c @@ -59,7 +59,7 @@ 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 = { diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c index 96b5866..066cf3b 100644 --- a/Modules/gdbmmodule.c +++ b/Modules/gdbmmodule.c @@ -93,7 +93,7 @@ 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 diff --git a/Modules/getpath.c b/Modules/getpath.c index 78b4915..78568137 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -529,7 +529,7 @@ 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 */ diff --git a/Modules/linuxaudiodev.c b/Modules/linuxaudiodev.c index 393df05..aac742a 100644 --- a/Modules/linuxaudiodev.c +++ b/Modules/linuxaudiodev.c @@ -104,7 +104,7 @@ 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 * diff --git a/Modules/md5module.c b/Modules/md5module.c index 3c001ae..6856c68 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -56,7 +56,7 @@ 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); } diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index a79812d..fcbb484 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -69,7 +69,7 @@ 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; diff --git a/Modules/mpzmodule.c b/Modules/mpzmodule.c index e701d85..1aaf787 100644 --- a/Modules/mpzmodule.c +++ b/Modules/mpzmodule.c @@ -123,7 +123,7 @@ 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() */ diff --git a/Modules/newmodule.c b/Modules/newmodule.c index 2758f14..fa0dc8b 100644 --- a/Modules/newmodule.c +++ b/Modules/newmodule.c @@ -49,7 +49,7 @@ 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); diff --git a/Modules/nismodule.c b/Modules/nismodule.c index 04f27f2..8943729 100644 --- a/Modules/nismodule.c +++ b/Modules/nismodule.c @@ -353,11 +353,11 @@ 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; } diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c index 6a8d38c..9b9baf0 100644 --- a/Modules/parsermodule.c +++ b/Modules/parsermodule.c @@ -295,7 +295,7 @@ 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); diff --git a/Modules/pcremodule.c b/Modules/pcremodule.c index 4d2aa72..6b9f960 100644 --- a/Modules/pcremodule.c +++ b/Modules/pcremodule.c @@ -79,7 +79,7 @@ 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); } diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index e98f393..f8b3bce 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -471,7 +471,7 @@ 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 ){ diff --git a/Modules/readline.c b/Modules/readline.c index d4ba3f8..37baf8c 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -377,7 +377,7 @@ 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; } diff --git a/Modules/regexmodule.c b/Modules/regexmodule.c index 4fbf5c8..92d3b71 100644 --- a/Modules/regexmodule.c +++ b/Modules/regexmodule.c @@ -64,13 +64,14 @@ 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; diff --git a/Modules/rotormodule.c b/Modules/rotormodule.c index b1fef39..3d570f7 100644 --- a/Modules/rotormodule.c +++ b/Modules/rotormodule.c @@ -178,7 +178,7 @@ rotorobj_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 * diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index b48eb54..fb46fc3 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -278,9 +278,9 @@ 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 diff --git a/Modules/shamodule.c b/Modules/shamodule.c index 0504fad..d56e90e 100644 --- a/Modules/shamodule.c +++ b/Modules/shamodule.c @@ -380,7 +380,7 @@ 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); } diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 939a495..56f1498 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -389,7 +389,7 @@ 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) diff --git a/Modules/stropmodule.c b/Modules/stropmodule.c index 4c9ee76..2e243a4 100644 --- a/Modules/stropmodule.c +++ b/Modules/stropmodule.c @@ -1152,7 +1152,7 @@ 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; } diff --git a/Modules/sunaudiodev.c b/Modules/sunaudiodev.c index 71c152e..f7b9426 100644 --- a/Modules/sunaudiodev.c +++ b/Modules/sunaudiodev.c @@ -139,7 +139,7 @@ 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 diff --git a/Modules/svmodule.c b/Modules/svmodule.c index 75f2023..99c6fa1 100644 --- a/Modules/svmodule.c +++ b/Modules/svmodule.c @@ -340,7 +340,7 @@ 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; diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c index cb463d9..195a164 100644 --- a/Modules/threadmodule.c +++ b/Modules/threadmodule.c @@ -58,12 +58,12 @@ 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 * diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c index c0d05f4..90a5029 100644 --- a/Modules/xxmodule.c +++ b/Modules/xxmodule.c @@ -62,7 +62,7 @@ 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 * diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 810ffef..cff7293 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -46,7 +46,7 @@ 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__[] = diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c index 05b1f11..72d4242 100644 --- a/Objects/bufferobject.c +++ b/Objects/bufferobject.c @@ -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 diff --git a/Objects/classobject.c b/Objects/classobject.c index 6c7dba5..bd95bc0 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -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); } } diff --git a/Objects/cobject.c b/Objects/cobject.c index 40e8672..267ca95 100644 --- a/Objects/cobject.c +++ b/Objects/cobject.c @@ -151,7 +151,7 @@ PyCObject_dealloc(self) else (self->destructor)(self->cobject); } - PyMem_DEL(self); + PyObject_DEL(self); } diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 0564942..42709ee 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -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); } diff --git a/Objects/dictobject.c b/Objects/dictobject.c index beab457..6e7fa3d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -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) } diff --git a/Objects/fileobject.c b/Objects/fileobject.c index c8b083e..7e0979f 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -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 * diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 77ef8d0..69b66b7 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -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; diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 4c716cd..1e672bb 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -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); } } diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 562935c..a5e15cc 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -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* diff --git a/Objects/intobject.c b/Objects/intobject.c index 0c8eefc..79435a9 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -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; diff --git a/Objects/listobject.c b/Objects/listobject.c index 005d709..f70d19b 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -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; } diff --git a/Objects/longobject.c b/Objects/longobject.c index a9ce6f3..4bf89d9 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -995,7 +995,7 @@ static void long_dealloc(v) PyObject *v; { - PyMem_DEL(v); + PyObject_DEL(v); } static PyObject * diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 5c69744..8b67a87 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -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); } } diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 7dfca73..808e27a 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -170,7 +170,7 @@ module_dealloc(m) _PyModule_Clear((PyObject *)m); Py_DECREF(m->md_dict); } - free((char *)m); + PyObject_DEL(m); } static PyObject * diff --git a/Objects/object.c b/Objects/object.c index ed276e2..6195479 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -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); } diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index c603ac9..807cf51 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -61,7 +61,7 @@ static void range_dealloc(r) rangeobject *r; { - PyMem_DEL(r); + PyObject_DEL(r); } static PyObject * diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index a232296..eb4972b 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -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 * diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 5b5ed9c..288f26e 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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; } diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index d1627a9..d5d6a07 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -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 diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7a68dd4..601b987 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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); } diff --git a/Objects/xxobject.c b/Objects/xxobject.c index c5b518f..91c9833 100644 --- a/Objects/xxobject.c +++ b/Objects/xxobject.c @@ -71,7 +71,7 @@ xx_dealloc(xp) xxobject *xp; { Py_XDECREF(xp->x_attr); - PyMem_DEL(xp); + PyObject_DEL(xp); } static PyObject * diff --git a/PC/_winreg.c b/PC/_winreg.c index 3dccc3d..bba9bc2 100644 --- a/PC/_winreg.c +++ b/PC/_winreg.c @@ -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"); diff --git a/PC/winreg.c b/PC/winreg.c index 3dccc3d..bba9bc2 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -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"); diff --git a/Parser/myreadline.c b/Parser/myreadline.c index d626139..9bf770e 100644 --- a/Parser/myreadline.c +++ b/Parser/myreadline.c @@ -89,7 +89,7 @@ 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 *)); diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 5b0d990..6d20d93 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -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); diff --git a/Parser/pgenmain.c b/Parser/pgenmain.c index 7292485..34c3b01 100644 --- a/Parser/pgenmain.c +++ b/Parser/pgenmain.c @@ -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 diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index e4b058e..7d9a273 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -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'); diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 698114e..1df95d7 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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) { diff --git a/Python/ceval.c b/Python/ceval.c index d0958bd..1a21ee7 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2558,7 +2558,8 @@ call_function(func, arg, kw) class); Py_DECREF(arg); - PyMem_XDEL(k); + if (k != NULL) + PyMem_DEL(k); return result; } diff --git a/Python/compile.c b/Python/compile.c index 58354c6..a387317 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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 * diff --git a/Python/import.c b/Python/import.c index a65614c..c224752 100644 --- a/Python/import.c +++ b/Python/import.c @@ -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; diff --git a/Python/marshal.c b/Python/marshal.c index 05ac6e9..fe9e000 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -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; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 49e616e..ee6ce07 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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) { diff --git a/Python/traceback.c b/Python/traceback.c index e1148cd..0b8157d 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -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) } -- cgit v0.12