Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 1 | /* The PyMem_ family: low-level memory allocation interfaces. |
| 2 | See objimpl.h for the PyObject_ memory family. |
| 3 | */ |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef Py_PYMEM_H |
| 6 | #define Py_PYMEM_H |
| 7 | |
| 8 | #include "pyport.h" |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 14 | #ifndef Py_LIMITED_API |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 15 | PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 16 | PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 17 | PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); |
| 18 | PyAPI_FUNC(void) PyMem_RawFree(void *ptr); |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 19 | |
| 20 | /* Configure the Python memory allocators. Pass NULL to use default |
| 21 | allocators. */ |
| 22 | PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt); |
| 23 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 24 | /* Try to get the allocators name set by _PyMem_SetupAllocators(). */ |
| 25 | PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void); |
| 26 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 27 | /* Track an allocated memory block in the tracemalloc module. |
| 28 | Return 0 on success, return -1 on error (failed to allocate memory to store |
| 29 | the trace). |
| 30 | |
| 31 | Return -2 if tracemalloc is disabled. |
| 32 | |
Victor Stinner | ca79ccd | 2016-03-23 09:38:54 +0100 | [diff] [blame] | 33 | If memory block is already tracked, update the existing trace. */ |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 34 | PyAPI_FUNC(int) PyTraceMalloc_Track( |
| 35 | unsigned int domain, |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 36 | uintptr_t ptr, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 37 | size_t size); |
| 38 | |
| 39 | /* Untrack an allocated memory block in the tracemalloc module. |
| 40 | Do nothing if the block was not tracked. |
| 41 | |
| 42 | Return -2 if tracemalloc is disabled, otherwise return 0. */ |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 43 | PyAPI_FUNC(int) PyTraceMalloc_Untrack( |
| 44 | unsigned int domain, |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 45 | uintptr_t ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 46 | |
| 47 | /* Get the traceback where a memory block was allocated. |
| 48 | |
| 49 | Return a tuple of (filename: str, lineno: int) tuples. |
| 50 | |
| 51 | Return None if the tracemalloc module is disabled or if the memory block |
| 52 | is not tracked by tracemalloc. |
| 53 | |
| 54 | Raise an exception and return NULL on error. */ |
| 55 | PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback( |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 56 | unsigned int domain, |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 57 | uintptr_t ptr); |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 58 | #endif /* !Py_LIMITED_API */ |
| 59 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 60 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 61 | /* BEWARE: |
| 62 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 63 | Each interface exports both functions and macros. Extension modules should |
| 64 | use the functions, to ensure binary compatibility across Python versions. |
| 65 | Because the Python implementation is free to change internal details, and |
| 66 | the macros may (or may not) expose details for speed, if you do use the |
| 67 | macros you must recompile your extensions with each Python release. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 68 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 69 | Never mix calls to PyMem_ with calls to the platform malloc/realloc/ |
| 70 | calloc/free. For example, on Windows different DLLs may end up using |
| 71 | different heaps, and if you use PyMem_Malloc you'll get the memory from the |
| 72 | heap used by the Python DLL; it could be a disaster if you free()'ed that |
| 73 | directly in your own extension. Using PyMem_Free instead ensures Python |
| 74 | can return the memory to the proper heap. As another example, in |
| 75 | PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_ |
| 76 | memory functions in special debugging wrappers that add additional |
| 77 | debugging info to dynamic memory blocks. The system routines have no idea |
| 78 | what to do with that stuff, and the Python wrappers have no idea what to do |
| 79 | with raw blocks obtained directly by the system routines then. |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 80 | |
| 81 | The GIL must be held when using these APIs. |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 82 | */ |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Raw memory interface |
| 86 | * ==================== |
| 87 | */ |
| 88 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 89 | /* Functions |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 90 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 91 | Functions supplying platform-independent semantics for malloc/realloc/ |
| 92 | free. These functions make sure that allocating 0 bytes returns a distinct |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 93 | non-NULL pointer (whenever possible -- if we're flat out of memory, NULL |
| 94 | may be returned), even if the platform malloc and realloc don't. |
| 95 | Returned pointers must be checked for NULL explicitly. No action is |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 96 | performed on failure (no exception is set, no warning is printed, etc). |
| 97 | */ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 98 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 99 | PyAPI_FUNC(void *) PyMem_Malloc(size_t size); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 100 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 101 | PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 102 | #endif |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 103 | PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size); |
| 104 | PyAPI_FUNC(void) PyMem_Free(void *ptr); |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 105 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 106 | #ifndef Py_LIMITED_API |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 107 | /* strdup() using PyMem_RawMalloc() */ |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 108 | PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str); |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 109 | |
| 110 | /* strdup() using PyMem_Malloc() */ |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 111 | PyAPI_FUNC(char *) _PyMem_Strdup(const char *str); |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 112 | |
| 113 | /* wcsdup() using PyMem_RawMalloc() */ |
| 114 | PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str); |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 115 | #endif |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 116 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 117 | /* Macros. */ |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 118 | |
Martin v. Löwis | 39f59b0 | 2002-11-23 09:13:40 +0000 | [diff] [blame] | 119 | /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL |
| 120 | for malloc(0), which would be treated as an error. Some platforms |
| 121 | would return a pointer with no memory behind it, which would break |
| 122 | pymalloc. To solve these problems, allocate an extra byte. */ |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 123 | /* Returns NULL to indicate error if a negative size or size larger than |
| 124 | Py_ssize_t can represent is supplied. Helps prevents security holes. */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 125 | #define PyMem_MALLOC(n) PyMem_Malloc(n) |
| 126 | #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n) |
| 127 | #define PyMem_FREE(p) PyMem_Free(p) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 128 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Type-oriented memory interface |
| 131 | * ============================== |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 132 | * |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 133 | * Allocate memory for n objects of the given type. Returns a new pointer |
| 134 | * or NULL if the request was too large or memory allocation failed. Use |
| 135 | * these macros rather than doing the multiplication yourself so that proper |
| 136 | * overflow checking is always done. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 137 | */ |
| 138 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 139 | #define PyMem_New(type, n) \ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 140 | ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 141 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 142 | #define PyMem_NEW(type, n) \ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 143 | ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 144 | ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 145 | |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 146 | /* |
| 147 | * The value of (p) is always clobbered by this macro regardless of success. |
| 148 | * The caller MUST check if (p) is NULL afterwards and deal with the memory |
| 149 | * error if so. This means the original value of (p) MUST be saved for the |
| 150 | * caller's memory error handler to not lose track of it. |
| 151 | */ |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 152 | #define PyMem_Resize(p, type, n) \ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 153 | ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 154 | (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 155 | #define PyMem_RESIZE(p, type, n) \ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 156 | ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 157 | (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 158 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 159 | /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used |
| 160 | * anymore. They're just confusing aliases for PyMem_{Free,FREE} now. |
| 161 | */ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 162 | #define PyMem_Del PyMem_Free |
| 163 | #define PyMem_DEL PyMem_FREE |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 164 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 165 | #ifndef Py_LIMITED_API |
| 166 | typedef enum { |
| 167 | /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */ |
| 168 | PYMEM_DOMAIN_RAW, |
| 169 | |
| 170 | /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */ |
| 171 | PYMEM_DOMAIN_MEM, |
| 172 | |
| 173 | /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */ |
| 174 | PYMEM_DOMAIN_OBJ |
| 175 | } PyMemAllocatorDomain; |
| 176 | |
| 177 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 178 | /* user context passed as the first argument to the 4 functions */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 179 | void *ctx; |
| 180 | |
| 181 | /* allocate a memory block */ |
| 182 | void* (*malloc) (void *ctx, size_t size); |
| 183 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 184 | /* allocate a memory block initialized by zeros */ |
| 185 | void* (*calloc) (void *ctx, size_t nelem, size_t elsize); |
| 186 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 187 | /* allocate or resize a memory block */ |
| 188 | void* (*realloc) (void *ctx, void *ptr, size_t new_size); |
| 189 | |
| 190 | /* release a memory block */ |
| 191 | void (*free) (void *ctx, void *ptr); |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 192 | } PyMemAllocatorEx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 193 | |
| 194 | /* Get the memory block allocator of the specified domain. */ |
| 195 | PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain, |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 196 | PyMemAllocatorEx *allocator); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 197 | |
| 198 | /* Set the memory block allocator of the specified domain. |
| 199 | |
| 200 | The new allocator must return a distinct non-NULL pointer when requesting |
| 201 | zero bytes. |
| 202 | |
| 203 | For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL |
| 204 | is not held when the allocator is called. |
| 205 | |
| 206 | If the new allocator is not a hook (don't call the previous allocator), the |
| 207 | PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks |
| 208 | on top on the new allocator. */ |
| 209 | PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain, |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 210 | PyMemAllocatorEx *allocator); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 211 | |
| 212 | /* Setup hooks to detect bugs in the following Python memory allocator |
| 213 | functions: |
| 214 | |
| 215 | - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree() |
| 216 | - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free() |
| 217 | - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() |
| 218 | |
| 219 | Newly allocated memory is filled with the byte 0xCB, freed memory is filled |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 220 | with the byte 0xDB. Additional checks: |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 221 | |
| 222 | - detect API violations, ex: PyObject_Free() called on a buffer allocated |
| 223 | by PyMem_Malloc() |
| 224 | - detect write before the start of the buffer (buffer underflow) |
| 225 | - detect write after the end of the buffer (buffer overflow) |
| 226 | |
| 227 | The function does nothing if Python is not compiled is debug mode. */ |
| 228 | PyAPI_FUNC(void) PyMem_SetupDebugHooks(void); |
| 229 | #endif |
| 230 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 231 | #ifdef Py_BUILD_CORE |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 232 | /* Set the memory allocator of the specified domain to the default. |
| 233 | Save the old allocator into *old_alloc if it's non-NULL. |
| 234 | Return on success, or return -1 if the domain is unknown. */ |
| 235 | PyAPI_FUNC(int) _PyMem_SetDefaultAllocator( |
| 236 | PyMemAllocatorDomain domain, |
| 237 | PyMemAllocatorEx *old_alloc); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 238 | #endif |
| 239 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 240 | #ifdef __cplusplus |
| 241 | } |
| 242 | #endif |
| 243 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 244 | #endif /* !Py_PYMEM_H */ |