Chris@87
|
1 /* The PyMem_ family: low-level memory allocation interfaces.
|
Chris@87
|
2 See objimpl.h for the PyObject_ memory family.
|
Chris@87
|
3 */
|
Chris@87
|
4
|
Chris@87
|
5 #ifndef Py_PYMEM_H
|
Chris@87
|
6 #define Py_PYMEM_H
|
Chris@87
|
7
|
Chris@87
|
8 #include "pyport.h"
|
Chris@87
|
9
|
Chris@87
|
10 #ifdef __cplusplus
|
Chris@87
|
11 extern "C" {
|
Chris@87
|
12 #endif
|
Chris@87
|
13
|
Chris@87
|
14 /* BEWARE:
|
Chris@87
|
15
|
Chris@87
|
16 Each interface exports both functions and macros. Extension modules should
|
Chris@87
|
17 use the functions, to ensure binary compatibility across Python versions.
|
Chris@87
|
18 Because the Python implementation is free to change internal details, and
|
Chris@87
|
19 the macros may (or may not) expose details for speed, if you do use the
|
Chris@87
|
20 macros you must recompile your extensions with each Python release.
|
Chris@87
|
21
|
Chris@87
|
22 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
|
Chris@87
|
23 calloc/free. For example, on Windows different DLLs may end up using
|
Chris@87
|
24 different heaps, and if you use PyMem_Malloc you'll get the memory from the
|
Chris@87
|
25 heap used by the Python DLL; it could be a disaster if you free()'ed that
|
Chris@87
|
26 directly in your own extension. Using PyMem_Free instead ensures Python
|
Chris@87
|
27 can return the memory to the proper heap. As another example, in
|
Chris@87
|
28 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
|
Chris@87
|
29 memory functions in special debugging wrappers that add additional
|
Chris@87
|
30 debugging info to dynamic memory blocks. The system routines have no idea
|
Chris@87
|
31 what to do with that stuff, and the Python wrappers have no idea what to do
|
Chris@87
|
32 with raw blocks obtained directly by the system routines then.
|
Chris@87
|
33
|
Chris@87
|
34 The GIL must be held when using these APIs.
|
Chris@87
|
35 */
|
Chris@87
|
36
|
Chris@87
|
37 /*
|
Chris@87
|
38 * Raw memory interface
|
Chris@87
|
39 * ====================
|
Chris@87
|
40 */
|
Chris@87
|
41
|
Chris@87
|
42 /* Functions
|
Chris@87
|
43
|
Chris@87
|
44 Functions supplying platform-independent semantics for malloc/realloc/
|
Chris@87
|
45 free. These functions make sure that allocating 0 bytes returns a distinct
|
Chris@87
|
46 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
|
Chris@87
|
47 may be returned), even if the platform malloc and realloc don't.
|
Chris@87
|
48 Returned pointers must be checked for NULL explicitly. No action is
|
Chris@87
|
49 performed on failure (no exception is set, no warning is printed, etc).
|
Chris@87
|
50 */
|
Chris@87
|
51
|
Chris@87
|
52 PyAPI_FUNC(void *) PyMem_Malloc(size_t);
|
Chris@87
|
53 PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t);
|
Chris@87
|
54 PyAPI_FUNC(void) PyMem_Free(void *);
|
Chris@87
|
55
|
Chris@87
|
56 /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
|
Chris@87
|
57 no longer supported. They used to call PyErr_NoMemory() on failure. */
|
Chris@87
|
58
|
Chris@87
|
59 /* Macros. */
|
Chris@87
|
60 #ifdef PYMALLOC_DEBUG
|
Chris@87
|
61 /* Redirect all memory operations to Python's debugging allocator. */
|
Chris@87
|
62 #define PyMem_MALLOC _PyMem_DebugMalloc
|
Chris@87
|
63 #define PyMem_REALLOC _PyMem_DebugRealloc
|
Chris@87
|
64 #define PyMem_FREE _PyMem_DebugFree
|
Chris@87
|
65
|
Chris@87
|
66 #else /* ! PYMALLOC_DEBUG */
|
Chris@87
|
67
|
Chris@87
|
68 /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
|
Chris@87
|
69 for malloc(0), which would be treated as an error. Some platforms
|
Chris@87
|
70 would return a pointer with no memory behind it, which would break
|
Chris@87
|
71 pymalloc. To solve these problems, allocate an extra byte. */
|
Chris@87
|
72 /* Returns NULL to indicate error if a negative size or size larger than
|
Chris@87
|
73 Py_ssize_t can represent is supplied. Helps prevents security holes. */
|
Chris@87
|
74 #define PyMem_MALLOC(n) ((size_t)(n) > (size_t)PY_SSIZE_T_MAX ? NULL \
|
Chris@87
|
75 : malloc((n) ? (n) : 1))
|
Chris@87
|
76 #define PyMem_REALLOC(p, n) ((size_t)(n) > (size_t)PY_SSIZE_T_MAX ? NULL \
|
Chris@87
|
77 : realloc((p), (n) ? (n) : 1))
|
Chris@87
|
78 #define PyMem_FREE free
|
Chris@87
|
79
|
Chris@87
|
80 #endif /* PYMALLOC_DEBUG */
|
Chris@87
|
81
|
Chris@87
|
82 /*
|
Chris@87
|
83 * Type-oriented memory interface
|
Chris@87
|
84 * ==============================
|
Chris@87
|
85 *
|
Chris@87
|
86 * Allocate memory for n objects of the given type. Returns a new pointer
|
Chris@87
|
87 * or NULL if the request was too large or memory allocation failed. Use
|
Chris@87
|
88 * these macros rather than doing the multiplication yourself so that proper
|
Chris@87
|
89 * overflow checking is always done.
|
Chris@87
|
90 */
|
Chris@87
|
91
|
Chris@87
|
92 #define PyMem_New(type, n) \
|
Chris@87
|
93 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
|
Chris@87
|
94 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
|
Chris@87
|
95 #define PyMem_NEW(type, n) \
|
Chris@87
|
96 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
|
Chris@87
|
97 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
|
Chris@87
|
98
|
Chris@87
|
99 /*
|
Chris@87
|
100 * The value of (p) is always clobbered by this macro regardless of success.
|
Chris@87
|
101 * The caller MUST check if (p) is NULL afterwards and deal with the memory
|
Chris@87
|
102 * error if so. This means the original value of (p) MUST be saved for the
|
Chris@87
|
103 * caller's memory error handler to not lose track of it.
|
Chris@87
|
104 */
|
Chris@87
|
105 #define PyMem_Resize(p, type, n) \
|
Chris@87
|
106 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
|
Chris@87
|
107 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
|
Chris@87
|
108 #define PyMem_RESIZE(p, type, n) \
|
Chris@87
|
109 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
|
Chris@87
|
110 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
|
Chris@87
|
111
|
Chris@87
|
112 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
|
Chris@87
|
113 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
|
Chris@87
|
114 */
|
Chris@87
|
115 #define PyMem_Del PyMem_Free
|
Chris@87
|
116 #define PyMem_DEL PyMem_FREE
|
Chris@87
|
117
|
Chris@87
|
118 #ifdef __cplusplus
|
Chris@87
|
119 }
|
Chris@87
|
120 #endif
|
Chris@87
|
121
|
Chris@87
|
122 #endif /* !Py_PYMEM_H */
|