| 1 | cdef extern from "Python.h": |
|---|
| 2 | ctypedef unsigned long size_t |
|---|
| 3 | |
|---|
| 4 | ##################################################################### |
|---|
| 5 | # 9.2 Memory Interface |
|---|
| 6 | ##################################################################### |
|---|
| 7 | # You are definitely *supposed* to use these: "In most situations, |
|---|
| 8 | # however, it is recommended to allocate memory from the Python |
|---|
| 9 | # heap specifically because the latter is under control of the |
|---|
| 10 | # Python memory manager. For example, this is required when the |
|---|
| 11 | # interpreter is extended with new object types written in |
|---|
| 12 | # C. Another reason for using the Python heap is the desire to |
|---|
| 13 | # inform the Python memory manager about the memory needs of the |
|---|
| 14 | # extension module. Even when the requested memory is used |
|---|
| 15 | # exclusively for internal, highly-specific purposes, delegating |
|---|
| 16 | # all memory requests to the Python memory manager causes the |
|---|
| 17 | # interpreter to have a more accurate image of its memory |
|---|
| 18 | # footprint as a whole. Consequently, under certain circumstances, |
|---|
| 19 | # the Python memory manager may or may not trigger appropriate |
|---|
| 20 | # actions, like garbage collection, memory compaction or other |
|---|
| 21 | # preventive procedures. Note that by using the C library |
|---|
| 22 | # allocator as shown in the previous example, the allocated memory |
|---|
| 23 | # for the I/O buffer escapes completely the Python memory |
|---|
| 24 | # manager." |
|---|
| 25 | |
|---|
| 26 | # The following function sets, modeled after the ANSI C standard, |
|---|
| 27 | # but specifying behavior when requesting zero bytes, are |
|---|
| 28 | # available for allocating and releasing memory from the Python |
|---|
| 29 | # heap: |
|---|
| 30 | |
|---|
| 31 | void* PyMem_Malloc(size_t n) |
|---|
| 32 | # Allocates n bytes and returns a pointer of type void* to the |
|---|
| 33 | # allocated memory, or NULL if the request fails. Requesting zero |
|---|
| 34 | # bytes returns a distinct non-NULL pointer if possible, as if |
|---|
| 35 | # PyMem_Malloc(1) had been called instead. The memory will not |
|---|
| 36 | # have been initialized in any way. |
|---|
| 37 | |
|---|
| 38 | void* PyMem_Realloc(void *p, size_t n) |
|---|
| 39 | # Resizes the memory block pointed to by p to n bytes. The |
|---|
| 40 | # contents will be unchanged to the minimum of the old and the new |
|---|
| 41 | # sizes. If p is NULL, the call is equivalent to PyMem_Malloc(n); |
|---|
| 42 | # else if n is equal to zero, the memory block is resized but is |
|---|
| 43 | # not freed, and the returned pointer is non-NULL. Unless p is |
|---|
| 44 | # NULL, it must have been returned by a previous call to |
|---|
| 45 | # PyMem_Malloc() or PyMem_Realloc(). |
|---|
| 46 | |
|---|
| 47 | void PyMem_Free(void *p) |
|---|
| 48 | # Frees the memory block pointed to by p, which must have been |
|---|
| 49 | # returned by a previous call to PyMem_Malloc() or |
|---|
| 50 | # PyMem_Realloc(). Otherwise, or if PyMem_Free(p) has been called |
|---|
| 51 | # before, undefined behavior occurs. If p is NULL, no operation is |
|---|
| 52 | # performed. |
|---|
| 53 | |
|---|
| 54 | # The following type-oriented macros are provided for |
|---|
| 55 | # convenience. Note that TYPE refers to any C type. |
|---|
| 56 | |
|---|
| 57 | # TYPE* PyMem_New(TYPE, size_t n) |
|---|
| 58 | # Same as PyMem_Malloc(), but allocates (n * sizeof(TYPE)) bytes |
|---|
| 59 | # of memory. Returns a pointer cast to TYPE*. The memory will not |
|---|
| 60 | # have been initialized in any way. |
|---|
| 61 | |
|---|
| 62 | # TYPE* PyMem_Resize(void *p, TYPE, size_t n) |
|---|
| 63 | # Same as PyMem_Realloc(), but the memory block is resized to (n * |
|---|
| 64 | # sizeof(TYPE)) bytes. Returns a pointer cast to TYPE*. |
|---|
| 65 | |
|---|
| 66 | void PyMem_Del(void *p) |
|---|
| 67 | # Same as PyMem_Free(). |
|---|
| 68 | |
|---|
| 69 | # In addition, the following macro sets are provided for calling |
|---|
| 70 | # the Python memory allocator directly, without involving the C |
|---|
| 71 | # API functions listed above. However, note that their use does |
|---|
| 72 | # not preserve binary compatibility across Python versions and is |
|---|
| 73 | # therefore deprecated in extension modules. |
|---|
| 74 | |
|---|
| 75 | # PyMem_MALLOC(), PyMem_REALLOC(), PyMem_FREE(). |
|---|
| 76 | # PyMem_NEW(), PyMem_RESIZE(), PyMem_DEL(). |
|---|