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); |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 19 | #endif |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 20 | |
| 21 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 22 | /* BEWARE: |
| 23 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 24 | Each interface exports both functions and macros. Extension modules should |
| 25 | use the functions, to ensure binary compatibility across Python versions. |
| 26 | Because the Python implementation is free to change internal details, and |
| 27 | the macros may (or may not) expose details for speed, if you do use the |
| 28 | macros you must recompile your extensions with each Python release. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 29 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 30 | Never mix calls to PyMem_ with calls to the platform malloc/realloc/ |
| 31 | calloc/free. For example, on Windows different DLLs may end up using |
| 32 | different heaps, and if you use PyMem_Malloc you'll get the memory from the |
| 33 | heap used by the Python DLL; it could be a disaster if you free()'ed that |
| 34 | directly in your own extension. Using PyMem_Free instead ensures Python |
| 35 | can return the memory to the proper heap. As another example, in |
| 36 | PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_ |
| 37 | memory functions in special debugging wrappers that add additional |
| 38 | debugging info to dynamic memory blocks. The system routines have no idea |
| 39 | what to do with that stuff, and the Python wrappers have no idea what to do |
| 40 | with raw blocks obtained directly by the system routines then. |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 41 | |
| 42 | The GIL must be held when using these APIs. |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 43 | */ |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Raw memory interface |
| 47 | * ==================== |
| 48 | */ |
| 49 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 50 | /* Functions |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 51 | |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 52 | Functions supplying platform-independent semantics for malloc/realloc/ |
| 53 | free. These functions make sure that allocating 0 bytes returns a distinct |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 54 | non-NULL pointer (whenever possible -- if we're flat out of memory, NULL |
| 55 | may be returned), even if the platform malloc and realloc don't. |
| 56 | Returned pointers must be checked for NULL explicitly. No action is |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 57 | performed on failure (no exception is set, no warning is printed, etc). |
| 58 | */ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 59 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 60 | PyAPI_FUNC(void *) PyMem_Malloc(size_t size); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 61 | PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 62 | PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size); |
| 63 | PyAPI_FUNC(void) PyMem_Free(void *ptr); |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 64 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 65 | #ifndef Py_LIMITED_API |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 66 | PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str); |
| 67 | PyAPI_FUNC(char *) _PyMem_Strdup(const char *str); |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 68 | #endif |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 69 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 70 | /* Macros. */ |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 71 | |
Martin v. Löwis | 39f59b0 | 2002-11-23 09:13:40 +0000 | [diff] [blame] | 72 | /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL |
| 73 | for malloc(0), which would be treated as an error. Some platforms |
| 74 | would return a pointer with no memory behind it, which would break |
| 75 | pymalloc. To solve these problems, allocate an extra byte. */ |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 76 | /* Returns NULL to indicate error if a negative size or size larger than |
| 77 | Py_ssize_t can represent is supplied. Helps prevents security holes. */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 78 | #define PyMem_MALLOC(n) PyMem_Malloc(n) |
| 79 | #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n) |
| 80 | #define PyMem_FREE(p) PyMem_Free(p) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 81 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 82 | /* |
| 83 | * Type-oriented memory interface |
| 84 | * ============================== |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 85 | * |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 86 | * Allocate memory for n objects of the given type. Returns a new pointer |
| 87 | * or NULL if the request was too large or memory allocation failed. Use |
| 88 | * these macros rather than doing the multiplication yourself so that proper |
| 89 | * overflow checking is always done. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 90 | */ |
| 91 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 92 | #define PyMem_New(type, n) \ |
Mark Dickinson | bbe6306 | 2010-02-14 14:08:54 +0000 | [diff] [blame] | 93 | ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 94 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 95 | #define PyMem_NEW(type, n) \ |
Mark Dickinson | bbe6306 | 2010-02-14 14:08:54 +0000 | [diff] [blame] | 96 | ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 97 | ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 98 | |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 99 | /* |
| 100 | * The value of (p) is always clobbered by this macro regardless of success. |
| 101 | * The caller MUST check if (p) is NULL afterwards and deal with the memory |
| 102 | * error if so. This means the original value of (p) MUST be saved for the |
| 103 | * caller's memory error handler to not lose track of it. |
| 104 | */ |
Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 105 | #define PyMem_Resize(p, type, n) \ |
Mark Dickinson | bbe6306 | 2010-02-14 14:08:54 +0000 | [diff] [blame] | 106 | ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 107 | (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 108 | #define PyMem_RESIZE(p, type, n) \ |
Mark Dickinson | bbe6306 | 2010-02-14 14:08:54 +0000 | [diff] [blame] | 109 | ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 110 | (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 112 | /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used |
| 113 | * anymore. They're just confusing aliases for PyMem_{Free,FREE} now. |
| 114 | */ |
| 115 | #define PyMem_Del PyMem_Free |
| 116 | #define PyMem_DEL PyMem_FREE |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 117 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 118 | #ifndef Py_LIMITED_API |
| 119 | typedef enum { |
| 120 | /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */ |
| 121 | PYMEM_DOMAIN_RAW, |
| 122 | |
| 123 | /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */ |
| 124 | PYMEM_DOMAIN_MEM, |
| 125 | |
| 126 | /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */ |
| 127 | PYMEM_DOMAIN_OBJ |
| 128 | } PyMemAllocatorDomain; |
| 129 | |
| 130 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 131 | /* user context passed as the first argument to the 4 functions */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 132 | void *ctx; |
| 133 | |
| 134 | /* allocate a memory block */ |
| 135 | void* (*malloc) (void *ctx, size_t size); |
| 136 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 137 | /* allocate a memory block initialized by zeros */ |
| 138 | void* (*calloc) (void *ctx, size_t nelem, size_t elsize); |
| 139 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 140 | /* allocate or resize a memory block */ |
| 141 | void* (*realloc) (void *ctx, void *ptr, size_t new_size); |
| 142 | |
| 143 | /* release a memory block */ |
| 144 | void (*free) (void *ctx, void *ptr); |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 145 | } PyMemAllocatorEx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 146 | |
| 147 | /* Get the memory block allocator of the specified domain. */ |
| 148 | PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain, |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 149 | PyMemAllocatorEx *allocator); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 150 | |
| 151 | /* Set the memory block allocator of the specified domain. |
| 152 | |
| 153 | The new allocator must return a distinct non-NULL pointer when requesting |
| 154 | zero bytes. |
| 155 | |
| 156 | For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL |
| 157 | is not held when the allocator is called. |
| 158 | |
| 159 | If the new allocator is not a hook (don't call the previous allocator), the |
| 160 | PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks |
| 161 | on top on the new allocator. */ |
| 162 | PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain, |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 163 | PyMemAllocatorEx *allocator); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 164 | |
| 165 | /* Setup hooks to detect bugs in the following Python memory allocator |
| 166 | functions: |
| 167 | |
| 168 | - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree() |
| 169 | - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free() |
| 170 | - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() |
| 171 | |
| 172 | Newly allocated memory is filled with the byte 0xCB, freed memory is filled |
| 173 | with the byte 0xDB. Additionnal checks: |
| 174 | |
| 175 | - detect API violations, ex: PyObject_Free() called on a buffer allocated |
| 176 | by PyMem_Malloc() |
| 177 | - detect write before the start of the buffer (buffer underflow) |
| 178 | - detect write after the end of the buffer (buffer overflow) |
| 179 | |
| 180 | The function does nothing if Python is not compiled is debug mode. */ |
| 181 | PyAPI_FUNC(void) PyMem_SetupDebugHooks(void); |
| 182 | #endif |
| 183 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 184 | #ifdef __cplusplus |
| 185 | } |
| 186 | #endif |
| 187 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 188 | #endif /* !Py_PYMEM_H */ |