1
cdef extern from "Python.h":
3
#####################################################################
5
#####################################################################
6
# You are definitely *supposed* to use these: "In most situations,
7
# however, it is recommended to allocate memory from the Python
8
# heap specifically because the latter is under control of the
9
# Python memory manager. For example, this is required when the
10
# interpreter is extended with new object types written in
11
# C. Another reason for using the Python heap is the desire to
12
# inform the Python memory manager about the memory needs of the
13
# extension module. Even when the requested memory is used
14
# exclusively for internal, highly-specific purposes, delegating
15
# all memory requests to the Python memory manager causes the
16
# interpreter to have a more accurate image of its memory
17
# footprint as a whole. Consequently, under certain circumstances,
18
# the Python memory manager may or may not trigger appropriate
19
# actions, like garbage collection, memory compaction or other
20
# preventive procedures. Note that by using the C library
21
# allocator as shown in the previous example, the allocated memory
22
# for the I/O buffer escapes completely the Python memory
25
# The following function sets, modeled after the ANSI C standard,
26
# but specifying behavior when requesting zero bytes, are
27
# available for allocating and releasing memory from the Python
30
void* PyMem_RawMalloc(size_t n) nogil
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.
38
void* PyMem_RawCalloc(size_t nelem, size_t elsize) nogil
39
void* PyMem_Calloc(size_t nelem, size_t elsize)
40
# Allocates nelem elements each whose size in bytes is elsize and
41
# returns a pointer of type void* to the allocated memory, or NULL if
42
# the request fails. The memory is initialized to zeros. Requesting
43
# zero elements or elements of size zero bytes returns a distinct
44
# non-NULL pointer if possible, as if PyMem_Calloc(1, 1) had been
47
void* PyMem_RawRealloc(void *p, size_t n) nogil
48
void* PyMem_Realloc(void *p, size_t n)
49
# Resizes the memory block pointed to by p to n bytes. The
50
# contents will be unchanged to the minimum of the old and the new
51
# sizes. If p is NULL, the call is equivalent to PyMem_Malloc(n);
52
# else if n is equal to zero, the memory block is resized but is
53
# not freed, and the returned pointer is non-NULL. Unless p is
54
# NULL, it must have been returned by a previous call to
55
# PyMem_Malloc(), PyMem_Realloc(), or PyMem_Calloc().
57
void PyMem_RawFree(void *p) nogil
58
void PyMem_Free(void *p)
59
# Frees the memory block pointed to by p, which must have been
60
# returned by a previous call to PyMem_Malloc(), PyMem_Realloc(), or
61
# PyMem_Calloc(). Otherwise, or if PyMem_Free(p) has been called
62
# before, undefined behavior occurs. If p is NULL, no operation is
65
# The following type-oriented macros are provided for
66
# convenience. Note that TYPE refers to any C type.
68
# TYPE* PyMem_New(TYPE, size_t n)
69
# Same as PyMem_Malloc(), but allocates (n * sizeof(TYPE)) bytes
70
# of memory. Returns a pointer cast to TYPE*. The memory will not
71
# have been initialized in any way.
73
# TYPE* PyMem_Resize(void *p, TYPE, size_t n)
74
# Same as PyMem_Realloc(), but the memory block is resized to (n *
75
# sizeof(TYPE)) bytes. Returns a pointer cast to TYPE*.
77
void PyMem_Del(void *p)
78
# Same as PyMem_Free().
80
# In addition, the following macro sets are provided for calling
81
# the Python memory allocator directly, without involving the C
82
# API functions listed above. However, note that their use does
83
# not preserve binary compatibility across Python versions and is
84
# therefore deprecated in extension modules.
86
# PyMem_MALLOC(), PyMem_REALLOC(), PyMem_FREE().
87
# PyMem_NEW(), PyMem_RESIZE(), PyMem_DEL().
90
#####################################################################
91
# Raw object memory interface
92
#####################################################################
94
# Functions to call the same malloc/realloc/free as used by Python's
95
# object allocator. If WITH_PYMALLOC is enabled, these may differ from
96
# the platform malloc/realloc/free. The Python object allocator is
97
# designed for fast, cache-conscious allocation of many "small" objects,
98
# and with low hidden memory overhead.
100
# PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
102
# PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
103
# PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
106
# Returned pointers must be checked for NULL explicitly; no action is
107
# performed on failure other than to return NULL (no warning it printed, no
108
# exception is set, etc).
110
# For allocating objects, use PyObject_{New, NewVar} instead whenever
111
# possible. The PyObject_{Malloc, Realloc, Free} family is exposed
112
# so that you can exploit Python's small-block allocator for non-object
113
# uses. If you must use these routines to allocate object memory, make sure
114
# the object gets initialized via PyObject_{Init, InitVar} after obtaining
117
void* PyObject_Malloc(size_t size)
118
void* PyObject_Calloc(size_t nelem, size_t elsize)
119
void* PyObject_Realloc(void *ptr, size_t new_size)
120
void PyObject_Free(void *ptr)