blob: 431e5b6d0f41381be7d55852ef74057518d06370 [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 Stinner34be8072016-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/* Identifier of an address space (domain) in tracemalloc */
29typedef unsigned int _PyTraceMalloc_domain_t;
30
31/* Track an allocated memory block in the tracemalloc module.
32 Return 0 on success, return -1 on error (failed to allocate memory to store
33 the trace).
34
35 Return -2 if tracemalloc is disabled.
36
Victor Stinnerca79ccd2016-03-23 09:38:54 +010037 If memory block is already tracked, update the existing trace. */
Victor Stinner10b73e12016-03-22 13:39:05 +010038PyAPI_FUNC(int) _PyTraceMalloc_Track(
39 _PyTraceMalloc_domain_t domain,
40 Py_uintptr_t ptr,
41 size_t size);
42
43/* Untrack an allocated memory block in the tracemalloc module.
44 Do nothing if the block was not tracked.
45
46 Return -2 if tracemalloc is disabled, otherwise return 0. */
47PyAPI_FUNC(int) _PyTraceMalloc_Untrack(
48 _PyTraceMalloc_domain_t domain,
49 Py_uintptr_t ptr);
50
51/* Get the traceback where a memory block was allocated.
52
53 Return a tuple of (filename: str, lineno: int) tuples.
54
55 Return None if the tracemalloc module is disabled or if the memory block
56 is not tracked by tracemalloc.
57
58 Raise an exception and return NULL on error. */
59PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
60 _PyTraceMalloc_domain_t domain,
61 Py_uintptr_t ptr);
Victor Stinner34be8072016-03-14 12:04:26 +010062#endif /* !Py_LIMITED_API */
63
Victor Stinner0507bf52013-07-07 02:05:46 +020064
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000065/* BEWARE:
66
Tim Peters8b078f92002-04-28 04:11:46 +000067 Each interface exports both functions and macros. Extension modules should
68 use the functions, to ensure binary compatibility across Python versions.
69 Because the Python implementation is free to change internal details, and
70 the macros may (or may not) expose details for speed, if you do use the
71 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000072
Tim Peters8b078f92002-04-28 04:11:46 +000073 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
74 calloc/free. For example, on Windows different DLLs may end up using
75 different heaps, and if you use PyMem_Malloc you'll get the memory from the
76 heap used by the Python DLL; it could be a disaster if you free()'ed that
77 directly in your own extension. Using PyMem_Free instead ensures Python
78 can return the memory to the proper heap. As another example, in
79 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
80 memory functions in special debugging wrappers that add additional
81 debugging info to dynamic memory blocks. The system routines have no idea
82 what to do with that stuff, and the Python wrappers have no idea what to do
83 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000084
85 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000086*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000087
88/*
89 * Raw memory interface
90 * ====================
91 */
92
Tim Peters8b078f92002-04-28 04:11:46 +000093/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000094
Tim Peters8b078f92002-04-28 04:11:46 +000095 Functions supplying platform-independent semantics for malloc/realloc/
96 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000097 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
98 may be returned), even if the platform malloc and realloc don't.
99 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +0000100 performed on failure (no exception is set, no warning is printed, etc).
101*/
Tim Petersaf3e8de2002-04-12 07:22:56 +0000102
Victor Stinner0507bf52013-07-07 02:05:46 +0200103PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +0200104PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +0200105PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
106PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000107
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100108#ifndef Py_LIMITED_API
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200109PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
110PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100111#endif
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200112
Tim Petersaf3e8de2002-04-12 07:22:56 +0000113/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +0000114
Martin v. Löwis39f59b02002-11-23 09:13:40 +0000115/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
116 for malloc(0), which would be treated as an error. Some platforms
117 would return a pointer with no memory behind it, which would break
118 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +0000119/* Returns NULL to indicate error if a negative size or size larger than
120 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner0507bf52013-07-07 02:05:46 +0200121#define PyMem_MALLOC(n) PyMem_Malloc(n)
122#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
123#define PyMem_FREE(p) PyMem_Free(p)
Tim Petersaf3e8de2002-04-12 07:22:56 +0000124
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000125/*
126 * Type-oriented memory interface
127 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +0000128 *
Georg Brandld492ad82008-07-23 16:13:07 +0000129 * Allocate memory for n objects of the given type. Returns a new pointer
130 * or NULL if the request was too large or memory allocation failed. Use
131 * these macros rather than doing the multiplication yourself so that proper
132 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000133 */
134
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000135#define PyMem_New(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000136 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000137 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000138#define PyMem_NEW(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000139 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000140 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +0000141
Georg Brandld492ad82008-07-23 16:13:07 +0000142/*
143 * The value of (p) is always clobbered by this macro regardless of success.
144 * The caller MUST check if (p) is NULL afterwards and deal with the memory
145 * error if so. This means the original value of (p) MUST be saved for the
146 * caller's memory error handler to not lose track of it.
147 */
Tim Peters8b078f92002-04-28 04:11:46 +0000148#define PyMem_Resize(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000149 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000150 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000151#define PyMem_RESIZE(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000152 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000153 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000154
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
156 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
157 */
158#define PyMem_Del PyMem_Free
159#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000160
Victor Stinner0507bf52013-07-07 02:05:46 +0200161#ifndef Py_LIMITED_API
162typedef enum {
163 /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
164 PYMEM_DOMAIN_RAW,
165
166 /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
167 PYMEM_DOMAIN_MEM,
168
169 /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
170 PYMEM_DOMAIN_OBJ
171} PyMemAllocatorDomain;
172
173typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200174 /* user context passed as the first argument to the 4 functions */
Victor Stinner0507bf52013-07-07 02:05:46 +0200175 void *ctx;
176
177 /* allocate a memory block */
178 void* (*malloc) (void *ctx, size_t size);
179
Victor Stinnerdb067af2014-05-02 22:31:14 +0200180 /* allocate a memory block initialized by zeros */
181 void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
182
Victor Stinner0507bf52013-07-07 02:05:46 +0200183 /* allocate or resize a memory block */
184 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
185
186 /* release a memory block */
187 void (*free) (void *ctx, void *ptr);
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200188} PyMemAllocatorEx;
Victor Stinner0507bf52013-07-07 02:05:46 +0200189
190/* Get the memory block allocator of the specified domain. */
191PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200192 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200193
194/* Set the memory block allocator of the specified domain.
195
196 The new allocator must return a distinct non-NULL pointer when requesting
197 zero bytes.
198
199 For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
200 is not held when the allocator is called.
201
202 If the new allocator is not a hook (don't call the previous allocator), the
203 PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
204 on top on the new allocator. */
205PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200206 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200207
208/* Setup hooks to detect bugs in the following Python memory allocator
209 functions:
210
211 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
212 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
213 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
214
215 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
216 with the byte 0xDB. Additionnal checks:
217
218 - detect API violations, ex: PyObject_Free() called on a buffer allocated
219 by PyMem_Malloc()
220 - detect write before the start of the buffer (buffer underflow)
221 - detect write after the end of the buffer (buffer overflow)
222
223 The function does nothing if Python is not compiled is debug mode. */
224PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
225#endif
226
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000227#ifdef __cplusplus
228}
229#endif
230
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000231#endif /* !Py_PYMEM_H */