@@ -4351,39 +4351,35 @@ \section{Memory Interface \label{memoryInterface}}
43514351occurs. If \var {p} is \NULL {}, no operation is performed.
43524352\end {cfuncdesc }
43534353
4354- \begin {cfuncdesc }{void*}{Py_Malloc}{size_t n}
4355- Same as \cfunction {PyMem_Malloc()}, but calls
4356- \cfunction {PyErr_NoMemory()} on failure.
4357- \end {cfuncdesc }
4358-
4359- \begin {cfuncdesc }{void*}{Py_Realloc}{void *p, size_t n}
4360- Same as \cfunction {PyMem_Realloc()}, but calls
4361- \cfunction {PyErr_NoMemory()} on failure.
4362- \end {cfuncdesc }
4363-
4364- \begin {cfuncdesc }{void}{Py_Free}{void *p}
4365- Same as \cfunction {PyMem_Free()}.
4366- \end {cfuncdesc }
4367-
43684354The following type-oriented macros are provided for convenience. Note
43694355that \var {TYPE} refers to any C type.
43704356
4371- \begin {cfuncdesc }{\var {TYPE}*}{PyMem_NEW }{TYPE, size_t n}
4357+ \begin {cfuncdesc }{\var {TYPE}*}{PyMem_New }{TYPE, size_t n}
43724358Same as \cfunction {PyMem_Malloc()}, but allocates \code {(\var {n} *
43734359sizeof(\var {TYPE}))} bytes of memory. Returns a pointer cast to
43744360\ctype {\var {TYPE}*}.
43754361\end {cfuncdesc }
43764362
4377- \begin {cfuncdesc }{\var {TYPE}*}{PyMem_RESIZE }{void *p, TYPE, size_t n}
4363+ \begin {cfuncdesc }{\var {TYPE}*}{PyMem_Resize }{void *p, TYPE, size_t n}
43784364Same as \cfunction {PyMem_Realloc()}, but the memory block is resized
43794365to \code {(\var {n} * sizeof(\var {TYPE}))} bytes. Returns a pointer
43804366cast to \ctype {\var {TYPE}*}.
43814367\end {cfuncdesc }
43824368
4383- \begin {cfuncdesc }{void}{PyMem_DEL }{void *p}
4369+ \begin {cfuncdesc }{void}{PyMem_Del }{void *p}
43844370Same as \cfunction {PyMem_Free()}.
43854371\end {cfuncdesc }
43864372
4373+ In addition, the following macro sets are provided for calling the
4374+ Python memory allocator directly, without involving the C API functions
4375+ listed above. However, note that their use does not preserve binary
4376+ compatibility accross Python versions and is therefore deprecated in
4377+ extension modules.
4378+
4379+ \cfunction {PyMem_MALLOC()}, \cfunction {PyMem_REALLOC()}, \cfunction {PyMem_FREE()}.
4380+
4381+ \cfunction {PyMem_NEW()}, \cfunction {PyMem_RESIZE()}, \cfunction {PyMem_DEL()}.
4382+
43874383
43884384\section {Examples \label {memoryExamples } }
43894385
@@ -4403,64 +4399,43 @@ \section{Examples \label{memoryExamples}}
44034399 return res;
44044400\end {verbatim }
44054401
4406- With the second function set, the need to call
4407- \cfunction {PyErr_NoMemory()} is obviated:
4408-
4409- \begin {verbatim }
4410- PyObject *res;
4411- char *buf = (char *) Py_Malloc(BUFSIZ); /* for I/O */
4412-
4413- if (buf == NULL)
4414- return NULL;
4415- /* ...Do some I/O operation involving buf... */
4416- res = PyString_FromString(buf);
4417- Py_Free(buf); /* allocated with Py_Malloc */
4418- return res;
4419- \end {verbatim }
4420-
4421- The same code using the macro set:
4402+ The same code using the type-oriented function set:
44224403
44234404\begin {verbatim }
44244405 PyObject *res;
4425- char *buf = PyMem_NEW (char, BUFSIZ); /* for I/O */
4406+ char *buf = PyMem_New (char, BUFSIZ); /* for I/O */
44264407
44274408 if (buf == NULL)
44284409 return PyErr_NoMemory();
44294410 /* ...Do some I/O operation involving buf... */
44304411 res = PyString_FromString(buf);
4431- PyMem_DEL (buf); /* allocated with PyMem_NEW */
4412+ PyMem_Del (buf); /* allocated with PyMem_New */
44324413 return res;
44334414\end {verbatim }
44344415
4435- Note that in the three examples above, the buffer is always
4436- manipulated via functions/macros belonging to the same set. Indeed, it
4416+ Note that in the two examples above, the buffer is always
4417+ manipulated via functions belonging to the same set. Indeed, it
44374418is required to use the same memory API family for a given
44384419memory block, so that the risk of mixing different allocators is
44394420reduced to a minimum. The following code sequence contains two errors,
44404421one of which is labeled as \emph {fatal } because it mixes two different
44414422allocators operating on different heaps.
44424423
44434424\begin {verbatim }
4444- char *buf1 = PyMem_NEW (char, BUFSIZ);
4425+ char *buf1 = PyMem_New (char, BUFSIZ);
44454426char *buf2 = (char *) malloc(BUFSIZ);
44464427char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
44474428...
4448- PyMem_DEL (buf3); /* Wrong -- should be PyMem_Free() */
4429+ PyMem_Del (buf3); /* Wrong -- should be PyMem_Free() */
44494430free(buf2); /* Right -- allocated via malloc() */
4450- free(buf1); /* Fatal -- should be PyMem_DEL () */
4431+ free(buf1); /* Fatal -- should be PyMem_Del () */
44514432\end {verbatim }
44524433
44534434In addition to the functions aimed at handling raw memory blocks from
44544435the Python heap, objects in Python are allocated and released with
4455- \cfunction {_PyObject_New()}\ttindex {_PyObject_New()} and
4456- \cfunction {_PyObject_NewVar()}\ttindex {_PyObject_NewVar()}, or with
4457- their corresponding macros
4458- \cfunction {PyObject_NEW()}\ttindex {PyObject_NEW()} and
4459- \cfunction {PyObject_NEW_VAR()}\ttindex {PyObject_NEW_VAR()}.
4460-
4461- \cfunction {_PyObject_New()}, \cfunction {_PyObject_NewVar()},
4462- \cfunction {_PyObject_Del()}, or with their corresponding macros
4463- \cfunction {PyObject_NEW()}, \cfunction {PyObject_NEW_VAR()},
4436+ \cfunction {PyObject_New()}, \cfunction {PyObject_NewVar()} and
4437+ \cfunction {PyObject_Del()}, or with their corresponding macros
4438+ \cfunction {PyObject_NEW()}, \cfunction {PyObject_NEW_VAR()} and
44644439\cfunction {PyObject_DEL()}.
44654440
44664441These will be explained in the next chapter on defining and
@@ -4472,14 +4447,38 @@ \chapter{Defining New Object Types \label{newTypes}}
44724447\begin {cfuncdesc }{PyObject*}{_PyObject_New}{PyTypeObject *type}
44734448\end {cfuncdesc }
44744449
4475- \begin {cfuncdesc }{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
4450+ \begin {cfuncdesc }{PyVarObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
4451+ \end {cfuncdesc }
4452+
4453+ \begin {cfuncdesc }{void}{_PyObject_Del}{PyObject *op}
4454+ \end {cfuncdesc }
4455+
4456+ \begin {cfuncdesc }{PyObject*}{PyObject_Init}{PyObject *op,
4457+ PyTypeObject *type}
4458+ \end {cfuncdesc }
4459+
4460+ \begin {cfuncdesc }{PyVarObject*}{PyObject_InitVar}{PyVarObject *op,
4461+ PyTypeObject *type, int size}
4462+ \end {cfuncdesc }
4463+
4464+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_New}{TYPE, PyTypeObject *type}
4465+ \end {cfuncdesc }
4466+
4467+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_NewVar}{TYPE, PyTypeObject *type,
4468+ int size}
4469+ \end {cfuncdesc }
4470+
4471+ \begin {cfuncdesc }{void}{PyObject_Del}{PyObject *op}
4472+ \end {cfuncdesc }
4473+
4474+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_NEW}{TYPE, PyTypeObject *type}
44764475\end {cfuncdesc }
44774476
4478- \begin {cfuncdesc }{\var {TYPE}}{_PyObject_NEW}{TYPE, PyTypeObject *type}
4477+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_NEW_VAR}{TYPE, PyTypeObject *type,
4478+ int size}
44794479\end {cfuncdesc }
44804480
4481- \begin {cfuncdesc }{\var {TYPE}}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *type,
4482- int size}
4481+ \begin {cfuncdesc }{void}{PyObject_DEL}{PyObject *op}
44834482\end {cfuncdesc }
44844483
44854484Py_InitModule (!!!)
0 commit comments