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