blob: 19f0c8a2d9bfbdcf1b575a658eef1467b4442bfa [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
Victor Stinner5d39e042017-11-29 17:20:38 +010024/* Try to get the allocators name set by _PyMem_SetupAllocators(). */
25PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);
26
Victor Stinner82af0b62018-10-23 17:39:40 +020027PyAPI_FUNC(int) _PyMem_IsFreed(void *ptr, size_t size);
28#endif /* !defined(Py_LIMITED_API) */
Victor Stinner34be807c2016-03-14 12:04:26 +010029
Victor Stinner0507bf52013-07-07 02:05:46 +020030
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000031/* BEWARE:
32
Tim Peters8b078f92002-04-28 04:11:46 +000033 Each interface exports both functions and macros. Extension modules should
34 use the functions, to ensure binary compatibility across Python versions.
35 Because the Python implementation is free to change internal details, and
36 the macros may (or may not) expose details for speed, if you do use the
37 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000038
Tim Peters8b078f92002-04-28 04:11:46 +000039 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
40 calloc/free. For example, on Windows different DLLs may end up using
41 different heaps, and if you use PyMem_Malloc you'll get the memory from the
42 heap used by the Python DLL; it could be a disaster if you free()'ed that
43 directly in your own extension. Using PyMem_Free instead ensures Python
44 can return the memory to the proper heap. As another example, in
45 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
46 memory functions in special debugging wrappers that add additional
47 debugging info to dynamic memory blocks. The system routines have no idea
48 what to do with that stuff, and the Python wrappers have no idea what to do
49 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000050
51 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000052*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000053
54/*
55 * Raw memory interface
56 * ====================
57 */
58
Tim Peters8b078f92002-04-28 04:11:46 +000059/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000060
Tim Peters8b078f92002-04-28 04:11:46 +000061 Functions supplying platform-independent semantics for malloc/realloc/
62 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000063 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
64 may be returned), even if the platform malloc and realloc don't.
65 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000066 performed on failure (no exception is set, no warning is printed, etc).
67*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000068
Victor Stinner0507bf52013-07-07 02:05:46 +020069PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020070#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinnerdb067af2014-05-02 22:31:14 +020071PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020072#endif
Victor Stinner0507bf52013-07-07 02:05:46 +020073PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
74PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000075
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010076#ifndef Py_LIMITED_API
Victor Stinner46972b72017-11-24 22:55:40 +010077/* strdup() using PyMem_RawMalloc() */
Victor Stinner49fc8ec2013-07-07 23:30:24 +020078PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
Victor Stinner46972b72017-11-24 22:55:40 +010079
80/* strdup() using PyMem_Malloc() */
Victor Stinner49fc8ec2013-07-07 23:30:24 +020081PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
Victor Stinner46972b72017-11-24 22:55:40 +010082
83/* wcsdup() using PyMem_RawMalloc() */
84PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010085#endif
Victor Stinner49fc8ec2013-07-07 23:30:24 +020086
Tim Petersaf3e8de2002-04-12 07:22:56 +000087/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000088
Martin v. Löwis39f59b02002-11-23 09:13:40 +000089/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
90 for malloc(0), which would be treated as an error. Some platforms
91 would return a pointer with no memory behind it, which would break
92 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +000093/* Returns NULL to indicate error if a negative size or size larger than
94 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner0507bf52013-07-07 02:05:46 +020095#define PyMem_MALLOC(n) PyMem_Malloc(n)
96#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
97#define PyMem_FREE(p) PyMem_Free(p)
Tim Petersaf3e8de2002-04-12 07:22:56 +000098
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000099/*
100 * Type-oriented memory interface
101 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +0000102 *
Georg Brandld492ad82008-07-23 16:13:07 +0000103 * Allocate memory for n objects of the given type. Returns a new pointer
104 * or NULL if the request was too large or memory allocation failed. Use
105 * these macros rather than doing the multiplication yourself so that proper
106 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000107 */
108
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000109#define PyMem_New(type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600110 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
111 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000112#define PyMem_NEW(type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600113 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
114 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +0000115
Georg Brandld492ad82008-07-23 16:13:07 +0000116/*
117 * The value of (p) is always clobbered by this macro regardless of success.
118 * The caller MUST check if (p) is NULL afterwards and deal with the memory
119 * error if so. This means the original value of (p) MUST be saved for the
120 * caller's memory error handler to not lose track of it.
121 */
Tim Peters8b078f92002-04-28 04:11:46 +0000122#define PyMem_Resize(p, type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600123 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
124 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000125#define PyMem_RESIZE(p, type, n) \
luzpaza5293b42017-11-05 07:37:50 -0600126 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
127 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
130 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
131 */
luzpaza5293b42017-11-05 07:37:50 -0600132#define PyMem_Del PyMem_Free
133#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000134
Victor Stinner0507bf52013-07-07 02:05:46 +0200135#ifndef Py_LIMITED_API
136typedef enum {
137 /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
138 PYMEM_DOMAIN_RAW,
139
140 /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
141 PYMEM_DOMAIN_MEM,
142
143 /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
144 PYMEM_DOMAIN_OBJ
145} PyMemAllocatorDomain;
146
147typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200148 /* user context passed as the first argument to the 4 functions */
Victor Stinner0507bf52013-07-07 02:05:46 +0200149 void *ctx;
150
151 /* allocate a memory block */
152 void* (*malloc) (void *ctx, size_t size);
153
Victor Stinnerdb067af2014-05-02 22:31:14 +0200154 /* allocate a memory block initialized by zeros */
155 void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
156
Victor Stinner0507bf52013-07-07 02:05:46 +0200157 /* allocate or resize a memory block */
158 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
159
160 /* release a memory block */
161 void (*free) (void *ctx, void *ptr);
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200162} PyMemAllocatorEx;
Victor Stinner0507bf52013-07-07 02:05:46 +0200163
164/* Get the memory block allocator of the specified domain. */
165PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200166 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200167
168/* Set the memory block allocator of the specified domain.
169
170 The new allocator must return a distinct non-NULL pointer when requesting
171 zero bytes.
172
173 For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
174 is not held when the allocator is called.
175
176 If the new allocator is not a hook (don't call the previous allocator), the
177 PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
178 on top on the new allocator. */
179PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200180 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200181
182/* Setup hooks to detect bugs in the following Python memory allocator
183 functions:
184
185 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
186 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
187 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
188
189 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
luzpaza5293b42017-11-05 07:37:50 -0600190 with the byte 0xDB. Additional checks:
Victor Stinner0507bf52013-07-07 02:05:46 +0200191
192 - detect API violations, ex: PyObject_Free() called on a buffer allocated
193 by PyMem_Malloc()
194 - detect write before the start of the buffer (buffer underflow)
195 - detect write after the end of the buffer (buffer overflow)
196
197 The function does nothing if Python is not compiled is debug mode. */
198PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
Victor Stinner626bff82018-10-25 17:31:10 +0200199#endif /* Py_LIMITED_API */
Victor Stinner0507bf52013-07-07 02:05:46 +0200200
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800201#ifdef Py_BUILD_CORE
Victor Stinner5d39e042017-11-29 17:20:38 +0100202/* Set the memory allocator of the specified domain to the default.
203 Save the old allocator into *old_alloc if it's non-NULL.
204 Return on success, or return -1 if the domain is unknown. */
205PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
206 PyMemAllocatorDomain domain,
207 PyMemAllocatorEx *old_alloc);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800208#endif
209
Victor Stinner9e00e802018-10-25 13:31:16 +0200210
211/* bpo-35053: expose _Py_tracemalloc_config for performance:
212 _Py_NewReference() needs an efficient check to test if tracemalloc is
Victor Stinner6279c1c2018-10-25 15:54:13 +0200213 tracing.
214
215 It has to be defined in pymem.h, before object.h is included. */
Victor Stinner9e00e802018-10-25 13:31:16 +0200216struct _PyTraceMalloc_Config {
217 /* Module initialized?
218 Variable protected by the GIL */
219 enum {
220 TRACEMALLOC_NOT_INITIALIZED,
221 TRACEMALLOC_INITIALIZED,
222 TRACEMALLOC_FINALIZED
223 } initialized;
224
225 /* Is tracemalloc tracing memory allocations?
226 Variable protected by the GIL */
227 int tracing;
228
229 /* limit of the number of frames in a traceback, 1 by default.
230 Variable protected by the GIL. */
231 int max_nframe;
232
233 /* use domain in trace key?
234 Variable protected by the GIL. */
235 int use_domain;
236};
237
238PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;
239
240#define _PyTraceMalloc_Config_INIT \
241 {.initialized = TRACEMALLOC_NOT_INITIALIZED, \
242 .tracing = 0, \
243 .max_nframe = 1, \
244 .use_domain = 0}
245
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000246#ifdef __cplusplus
247}
248#endif
249
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000250#endif /* !Py_PYMEM_H */