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