Skip to content

Commit ec2cbdd

Browse files
authored
bpo-20064: Document PyObject_Malloc() (#4199)
Document the following functions: * PyObject_Malloc() * PyObject_Calloc() * PyObject_Realloc() * PyObject_Free() Fix also PyMem_RawFree() documentation.
1 parent 2298fad commit ec2cbdd

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

Doc/c-api/memory.rst

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ The default raw memory block allocator uses the following functions:
150150
151151
Frees the memory block pointed to by *p*, which must have been returned by a
152152
previous call to :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc` or
153-
:c:func:`PyMem_RawCalloc`. Otherwise, or if ``PyMem_Free(p)`` has been
153+
:c:func:`PyMem_RawCalloc`. Otherwise, or if ``PyMem_RawFree(p)`` has been
154154
called before, undefined behavior occurs.
155155
156156
If *p* is *NULL*, no operation is performed.
@@ -263,6 +263,69 @@ versions and is therefore deprecated in extension modules.
263263
* ``PyMem_DEL(ptr)``
264264
265265
266+
Object allocators
267+
=================
268+
269+
The following function sets, modeled after the ANSI C standard, but specifying
270+
behavior when requesting zero bytes, are available for allocating and releasing
271+
memory from the Python heap.
272+
273+
By default, these functions use :ref:`pymalloc memory allocator <pymalloc>`.
274+
275+
.. warning::
276+
277+
The :term:`GIL <global interpreter lock>` must be held when using these
278+
functions.
279+
280+
.. c:function:: void* PyObject_Malloc(size_t n)
281+
282+
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
283+
allocated memory, or *NULL* if the request fails.
284+
285+
Requesting zero bytes returns a distinct non-*NULL* pointer if possible, as
286+
if ``PyObject_Malloc(1)`` had been called instead. The memory will not have
287+
been initialized in any way.
288+
289+
290+
.. c:function:: void* PyObject_Calloc(size_t nelem, size_t elsize)
291+
292+
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
293+
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
294+
request fails. The memory is initialized to zeros.
295+
296+
Requesting zero elements or elements of size zero bytes returns a distinct
297+
non-*NULL* pointer if possible, as if ``PyObject_Calloc(1, 1)`` had been called
298+
instead.
299+
300+
.. versionadded:: 3.5
301+
302+
303+
.. c:function:: void* PyObject_Realloc(void *p, size_t n)
304+
305+
Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
306+
unchanged to the minimum of the old and the new sizes.
307+
308+
If *p* is *NULL*, the call is equivalent to ``PyObject_Malloc(n)``; else if *n*
309+
is equal to zero, the memory block is resized but is not freed, and the
310+
returned pointer is non-*NULL*.
311+
312+
Unless *p* is *NULL*, it must have been returned by a previous call to
313+
:c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc` or :c:func:`PyObject_Calloc`.
314+
315+
If the request fails, :c:func:`PyObject_Realloc` returns *NULL* and *p* remains
316+
a valid pointer to the previous memory area.
317+
318+
319+
.. c:function:: void PyObject_Free(void *p)
320+
321+
Frees the memory block pointed to by *p*, which must have been returned by a
322+
previous call to :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc` or
323+
:c:func:`PyObject_Calloc`. Otherwise, or if ``PyObject_Free(p)`` has been called
324+
before, undefined behavior occurs.
325+
326+
If *p* is *NULL*, no operation is performed.
327+
328+
266329
Customize Memory Allocators
267330
===========================
268331

0 commit comments

Comments
 (0)