GNU Info

Info Node: (python2.1-api.info)Memory Interface

(python2.1-api.info)Memory Interface


Next: Examples Prev: Overview Up: Memory Management
Enter node , (file) or (file)node

Memory Interface
================

The following function sets, modeled after the ANSI C standard, are
available for allocating and releasing memory from the Python heap:

`void* PyMem_Malloc(size_t n)'
     Allocates N bytes and returns a pointer of type `void*' to the
     allocated memory, or `NULL' if the request fails.  Requesting zero
     bytes returns a non-`NULL' pointer.  The memory will not have been
     initialized in any way.

`void* PyMem_Realloc(void *p, size_t n)'
     Resizes the memory block pointed to by P to N bytes. The contents
     will be unchanged to the minimum of the old and the new sizes. If
     P is `NULL', the call is equivalent to `PyMem_Malloc(N)'; if N is
     equal to zero, the memory block is resized but is not freed, and
     the returned pointer is non-`NULL'.  Unless P is `NULL', it must
     have been returned by a previous call to `PyMem_Malloc()' or
     `PyMem_Realloc()'.

`void PyMem_Free(void *p)'
     Frees the memory block pointed to by P, which must have been
     returned by a previous call to `PyMem_Malloc()' or
     `PyMem_Realloc()'.  Otherwise, or if `PyMem_Free(p)' has been
     called before, undefined behaviour occurs. If P is `NULL', no
     operation is performed.

The following type-oriented macros are provided for convenience.  Note
that TYPE refers to any C type.

`TYPE* PyMem_New(TYPE, size_t n)'
     Same as `PyMem_Malloc()', but allocates `(N * sizeof(TYPE))' bytes
     of memory.  Returns a pointer cast to `TYPE*'.  The memory will
     not have been initialized in any way.

`TYPE* PyMem_Resize(void *p, TYPE, size_t n)'
     Same as `PyMem_Realloc()', but the memory block is resized to `(N
     * sizeof(TYPE))' bytes.  Returns a pointer cast to `TYPE*'.

`void PyMem_Del(void *p)'
     Same as `PyMem_Free()'.

In addition, the following macro sets are provided for calling the
Python memory allocator directly, without involving the C API functions
listed above. However, note that their use does not preserve binary
compatibility accross Python versions and is therefore deprecated in
extension modules.

`PyMem_MALLOC()', `PyMem_REALLOC()', `PyMem_FREE()'.

`PyMem_NEW()', `PyMem_RESIZE()', `PyMem_DEL()'.


automatically generated by info2www version 1.2.2.9