| OLD | NEW |
| 1 /* The PyMem_ family: low-level memory allocation interfaces. | 1 /* The PyMem_ family: low-level memory allocation interfaces. |
| 2 See objimpl.h for the PyObject_ memory family. | 2 See objimpl.h for the PyObject_ memory family. |
| 3 */ | 3 */ |
| 4 | 4 |
| 5 #ifndef Py_PYMEM_H | 5 #ifndef Py_PYMEM_H |
| 6 #define Py_PYMEM_H | 6 #define Py_PYMEM_H |
| 7 | 7 |
| 8 #include "pyport.h" | 8 #include "pyport.h" |
| 9 | 9 |
| 10 #ifdef __cplusplus | 10 #ifdef __cplusplus |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 #define PyMem_MALLOC PyObject_MALLOC | 62 #define PyMem_MALLOC PyObject_MALLOC |
| 63 #define PyMem_REALLOC PyObject_REALLOC | 63 #define PyMem_REALLOC PyObject_REALLOC |
| 64 #define PyMem_FREE PyObject_FREE | 64 #define PyMem_FREE PyObject_FREE |
| 65 | 65 |
| 66 #else /* ! PYMALLOC_DEBUG */ | 66 #else /* ! PYMALLOC_DEBUG */ |
| 67 | 67 |
| 68 /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL | 68 /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL |
| 69 for malloc(0), which would be treated as an error. Some platforms | 69 for malloc(0), which would be treated as an error. Some platforms |
| 70 would return a pointer with no memory behind it, which would break | 70 would return a pointer with no memory behind it, which would break |
| 71 pymalloc. To solve these problems, allocate an extra byte. */ | 71 pymalloc. To solve these problems, allocate an extra byte. */ |
| 72 #define PyMem_MALLOC(n) malloc((n) ? (n) : 1) | 72 /* Returns NULL to indicate error if a negative size or size larger than |
| 73 #define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1) | 73 Py_ssize_t can represent is supplied. Helps prevents security holes. */ |
| 74 #define PyMem_MALLOC(n)»» (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \ |
| 75 » » » » : malloc((n) ? (n) : 1)) |
| 76 #define PyMem_REALLOC(p, n)» (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \ |
| 77 » » » » : realloc((p), (n) ? (n) : 1)) |
| 74 #define PyMem_FREE free | 78 #define PyMem_FREE free |
| 75 | 79 |
| 76 #endif /* PYMALLOC_DEBUG */ | 80 #endif /* PYMALLOC_DEBUG */ |
| 77 | 81 |
| 78 /* | 82 /* |
| 79 * Type-oriented memory interface | 83 * Type-oriented memory interface |
| 80 * ============================== | 84 * ============================== |
| 81 * | 85 * |
| 82 * These are carried along for historical reasons. There's rarely a good | 86 * Allocate memory for n objects of the given type. Returns a new pointer |
| 83 * reason to use them anymore (you can just as easily do the multiply and | 87 * or NULL if the request was too large or memory allocation failed. Use |
| 84 * cast yourself). | 88 * these macros rather than doing the multiplication yourself so that proper |
| 89 * overflow checking is always done. |
| 85 */ | 90 */ |
| 86 | 91 |
| 87 #define PyMem_New(type, n) \ | 92 #define PyMem_New(type, n) \ |
| 88 ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 93 ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 89 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) | 94 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
| 90 #define PyMem_NEW(type, n) \ | 95 #define PyMem_NEW(type, n) \ |
| 91 ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 96 ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 92 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) | 97 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
| 93 | 98 |
| 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 */ |
| 94 #define PyMem_Resize(p, type, n) \ | 105 #define PyMem_Resize(p, type, n) \ |
| 95 ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 106 ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 96 » ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) ) | 107 » (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
| 97 #define PyMem_RESIZE(p, type, n) \ | 108 #define PyMem_RESIZE(p, type, n) \ |
| 98 ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 109 ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
| 99 » ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) ) | 110 » (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
| 100 | 111 |
| 101 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used | 112 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used |
| 102 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now. | 113 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now. |
| 103 */ | 114 */ |
| 104 #define PyMem_Del PyMem_Free | 115 #define PyMem_Del PyMem_Free |
| 105 #define PyMem_DEL PyMem_FREE | 116 #define PyMem_DEL PyMem_FREE |
| 106 | 117 |
| 107 #ifdef __cplusplus | 118 #ifdef __cplusplus |
| 108 } | 119 } |
| 109 #endif | 120 #endif |
| 110 | 121 |
| 111 #endif /* !Py_PYMEM_H */ | 122 #endif /* !Py_PYMEM_H */ |
| OLD | NEW |