blob: 57a34cf90783b6c725cf6e15debaa08f417ae7b7 [file] [log] [blame]
Tim Peters8b078f92002-04-28 04:11:46 +00001/* The PyMem_ family: low-level memory allocation interfaces.
2 See objimpl.h for the PyObject_ memory family.
3*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00004
5#ifndef Py_PYMEM_H
6#define Py_PYMEM_H
7
8#include "pyport.h"
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010014#ifndef Py_LIMITED_API
Victor Stinner0507bf52013-07-07 02:05:46 +020015PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +020016PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +020017PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
18PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
Victor Stinner34be807c2016-03-14 12:04:26 +010019
20/* Configure the Python memory allocators. Pass NULL to use default
21 allocators. */
22PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);
23
24#ifdef WITH_PYMALLOC
25PyAPI_FUNC(int) _PyMem_PymallocEnabled(void);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010026#endif
Victor Stinner0507bf52013-07-07 02:05:46 +020027
Victor Stinner10b73e12016-03-22 13:39:05 +010028/* Track an allocated memory block in the tracemalloc module.
29 Return 0 on success, return -1 on error (failed to allocate memory to store
30 the trace).
31
32 Return -2 if tracemalloc is disabled.
33
Victor Stinnerca79ccd2016-03-23 09:38:54 +010034 If memory block is already tracked, update the existing trace. */
Victor Stinner5ea4c062017-06-20 17:46:36 +020035PyAPI_FUNC(int) PyTraceMalloc_Track(
36 unsigned int domain,
Benjamin Petersonca470632016-09-06 13:47:26 -070037 uintptr_t ptr,
Victor Stinner10b73e12016-03-22 13:39:05 +010038 size_t size);
39
40/* Untrack an allocated memory block in the tracemalloc module.
41 Do nothing if the block was not tracked.
42
43 Return -2 if tracemalloc is disabled, otherwise return 0. */
Victor Stinner5ea4c062017-06-20 17:46:36 +020044PyAPI_FUNC(int) PyTraceMalloc_Untrack(
45 unsigned int domain,
Benjamin Petersonca470632016-09-06 13:47:26 -070046 uintptr_t ptr);
Victor Stinner10b73e12016-03-22 13:39:05 +010047
48/* Get the traceback where a memory block was allocated.
49
50 Return a tuple of (filename: str, lineno: int) tuples.
51
52 Return None if the tracemalloc module is disabled or if the memory block
53 is not tracked by tracemalloc.
54
55 Raise an exception and return NULL on error. */
56PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
Victor Stinner5ea4c062017-06-20 17:46:36 +020057 unsigned int domain,
Benjamin Petersonca470632016-09-06 13:47:26 -070058 uintptr_t ptr);
Victor Stinner34be807c2016-03-14 12:04:26 +010059#endif /* !Py_LIMITED_API */
60
Victor Stinner0507bf52013-07-07 02:05:46 +020061
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000062/* BEWARE:
63
Tim Peters8b078f92002-04-28 04:11:46 +000064 Each interface exports both functions and macros. Extension modules should
65 use the functions, to ensure binary compatibility across Python versions.
66 Because the Python implementation is free to change internal details, and
67 the macros may (or may not) expose details for speed, if you do use the
68 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000069
Tim Peters8b078f92002-04-28 04:11:46 +000070 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
71 calloc/free. For example, on Windows different DLLs may end up using
72 different heaps, and if you use PyMem_Malloc you'll get the memory from the
73 heap used by the Python DLL; it could be a disaster if you free()'ed that
74 directly in your own extension. Using PyMem_Free instead ensures Python
75 can return the memory to the proper heap. As another example, in
76 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
77 memory functions in special debugging wrappers that add additional
78 debugging info to dynamic memory blocks. The system routines have no idea
79 what to do with that stuff, and the Python wrappers have no idea what to do
80 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000081
82 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000083*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000084
85/*
86 * Raw memory interface
87 * ====================
88 */
89
Tim Peters8b078f92002-04-28 04:11:46 +000090/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000091
Tim Peters8b078f92002-04-28 04:11:46 +000092 Functions supplying platform-independent semantics for malloc/realloc/
93 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000094 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
95 may be returned), even if the platform malloc and realloc don't.
96 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000097 performed on failure (no exception is set, no warning is printed, etc).
98*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000099
Victor Stinner0507bf52013-07-07 02:05:46 +0200100PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200101#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinnerdb067af2014-05-02 22:31:14 +0200102PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200103#endif
Victor Stinner0507bf52013-07-07 02:05:46 +0200104PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
105PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000106
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100107#ifndef Py_LIMITED_API
Victor Stinner46972b72017-11-24 22:55:40 +0100108/* strdup() using PyMem_RawMalloc() */
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200109PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
Victor Stinner46972b72017-11-24 22:55:40 +0100110
111/* strdup() using PyMem_Malloc() */
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200112PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
Victor Stinner46972b72017-11-24 22:55:40 +0100113
114/* wcsdup() using PyMem_RawMalloc() */
115PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100116#endif
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200117
Tim Petersaf3e8de2002-04-12 07:22:56 +0000118/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +0000119
Martin v. Löwis39f59b02002-11-23 09:13:40 +0000120/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
121 for malloc(0), which would be treated as an error. Some platforms
122 would return a pointer with no memory behind it, which would break
123 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +0000124/* Returns NULL to indicate error if a negative size or size larger than
125 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner0507bf52013-07-07 02:05:46 +0200126#define PyMem_MALLOC(n) PyMem_Malloc(n)
127#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
128#define PyMem_FREE(p) PyMem_Free(p)
Tim Petersaf3e8de2002-04-12 07:22:56 +0000129
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000130/*
131 * Type-oriented memory interface
132 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +0000133 *
Georg Brandld492ad82008-07-23 16:13:07 +0000134 * Allocate memory for n objects of the given type. Returns a new pointer
135 * or NULL if the request was too large or memory allocation failed. Use
136 * these macros rather than doing the multiplication yourself so that proper
137 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000138 */
139
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000140#define PyMem_New(type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600141 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
142 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000143#define PyMem_NEW(type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600144 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
145 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +0000146
Georg Brandld492ad82008-07-23 16:13:07 +0000147/*
148 * The value of (p) is always clobbered by this macro regardless of success.
149 * The caller MUST check if (p) is NULL afterwards and deal with the memory
150 * error if so. This means the original value of (p) MUST be saved for the
151 * caller's memory error handler to not lose track of it.
152 */
Tim Peters8b078f92002-04-28 04:11:46 +0000153#define PyMem_Resize(p, type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600154 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
155 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000156#define PyMem_RESIZE(p, type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600157 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
158 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000159
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
161 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
162 */
luzpaza5293b42017-11-05 07:37:50 -0600163#define PyMem_Del PyMem_Free
164#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000165
Victor Stinner0507bf52013-07-07 02:05:46 +0200166#ifndef Py_LIMITED_API
167typedef enum {
168 /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
169 PYMEM_DOMAIN_RAW,
170
171 /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
172 PYMEM_DOMAIN_MEM,
173
174 /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
175 PYMEM_DOMAIN_OBJ
176} PyMemAllocatorDomain;
177
178typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200179 /* user context passed as the first argument to the 4 functions */
Victor Stinner0507bf52013-07-07 02:05:46 +0200180 void *ctx;
181
182 /* allocate a memory block */
183 void* (*malloc) (void *ctx, size_t size);
184
Victor Stinnerdb067af2014-05-02 22:31:14 +0200185 /* allocate a memory block initialized by zeros */
186 void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
187
Victor Stinner0507bf52013-07-07 02:05:46 +0200188 /* allocate or resize a memory block */
189 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
190
191 /* release a memory block */
192 void (*free) (void *ctx, void *ptr);
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200193} PyMemAllocatorEx;
Victor Stinner0507bf52013-07-07 02:05:46 +0200194
195/* Get the memory block allocator of the specified domain. */
196PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200197 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200198
199/* Set the memory block allocator of the specified domain.
200
201 The new allocator must return a distinct non-NULL pointer when requesting
202 zero bytes.
203
204 For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
205 is not held when the allocator is called.
206
207 If the new allocator is not a hook (don't call the previous allocator), the
208 PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
209 on top on the new allocator. */
210PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200211 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200212
213/* Setup hooks to detect bugs in the following Python memory allocator
214 functions:
215
216 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
217 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
218 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
219
220 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
luzpaza5293b42017-11-05 07:37:50 -0600221 with the byte 0xDB. Additional checks:
Victor Stinner0507bf52013-07-07 02:05:46 +0200222
223 - detect API violations, ex: PyObject_Free() called on a buffer allocated
224 by PyMem_Malloc()
225 - detect write before the start of the buffer (buffer underflow)
226 - detect write after the end of the buffer (buffer overflow)
227
228 The function does nothing if Python is not compiled is debug mode. */
229PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
230#endif
231
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800232#ifdef Py_BUILD_CORE
233PyAPI_FUNC(void) _PyMem_GetDefaultRawAllocator(PyMemAllocatorEx *alloc);
234#endif
235
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000236#ifdef __cplusplus
237}
238#endif
239
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000240#endif /* !Py_PYMEM_H */