Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 1 | |
| 2 | /* Lowest-level memory allocation interface */ |
| 3 | |
| 4 | #ifndef Py_PYMEM_H |
| 5 | #define Py_PYMEM_H |
| 6 | |
| 7 | #include "pyport.h" |
| 8 | |
| 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 13 | /* BEWARE: |
| 14 | |
| 15 | Each interface exports both functions and macros. Extension modules |
| 16 | should normally use the functions for ensuring binary compatibility |
| 17 | of the user's code across Python versions. Subsequently, if the |
| 18 | Python runtime switches to its own malloc (different from standard |
| 19 | malloc), no recompilation is required for the extensions. |
| 20 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 21 | The macro versions are free to trade compatibility for speed, although |
| 22 | there's no guarantee they're ever faster. Extensions shouldn't use the |
| 23 | macro versions, as they don't gurantee binary compatibility across |
| 24 | releases. |
| 25 | |
| 26 | Do not mix calls to PyMem_xyz with calls to platform |
| 27 | malloc/realloc/calloc/free. */ |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Raw memory interface |
| 31 | * ==================== |
| 32 | */ |
| 33 | |
| 34 | /* Functions */ |
| 35 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 36 | /* Functions supplying platform-independent semantics for malloc/realloc/ |
| 37 | free; useful if you need to be sure you're using the same memory |
| 38 | allocator as Python (this can be especially important on Windows, if |
| 39 | you need to make sure you're using the same MS malloc/free, and out of |
| 40 | the same heap, as the main Python DLL uses). |
| 41 | These functions make sure that allocating 0 bytes returns a distinct |
| 42 | non-NULL pointer (whenever possible -- if we're flat out of memory, NULL |
| 43 | may be returned), even if the platform malloc and realloc don't. |
| 44 | Returned pointers must be checked for NULL explicitly. No action is |
| 45 | performed on failure (no exception is set, no warning is printed, etc).` */ |
| 46 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 47 | extern DL_IMPORT(void *) PyMem_Malloc(size_t); |
| 48 | extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t); |
| 49 | extern DL_IMPORT(void) PyMem_Free(void *); |
| 50 | |
| 51 | /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are |
| 52 | no longer supported. They used to call PyErr_NoMemory() on failure. */ |
| 53 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 54 | /* Macros. */ |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 55 | #ifdef PYMALLOC_DEBUG |
| 56 | /* Redirect all memory operations to Python's debugging allocator. */ |
| 57 | #define PyMem_MALLOC PyObject_MALLOC |
| 58 | #define PyMem_REALLOC PyObject_REALLOC |
| 59 | #define PyMem_FREE PyObject_FREE |
| 60 | |
| 61 | #else /* ! PYMALLOC_DEBUG */ |
| 62 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 63 | #ifdef MALLOC_ZERO_RETURNS_NULL |
| 64 | #define PyMem_MALLOC(n) malloc((n) ? (n) : 1) |
| 65 | #else |
| 66 | #define PyMem_MALLOC malloc |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 67 | #endif |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 68 | /* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to |
| 69 | do with whether platform realloc(non-NULL, 0) normally frees the memory |
| 70 | or returns NULL. Rather than introduce yet another config variation, |
| 71 | just make a realloc to 0 bytes act as if to 1 instead. */ |
| 72 | #define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1) |
| 73 | |
| 74 | #define PyMem_FREE free |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 75 | #endif /* PYMALLOC_DEBUG */ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 76 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Type-oriented memory interface |
| 79 | * ============================== |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 80 | * |
| 81 | * These are carried along for historical reasons. There's rarely a good |
| 82 | * reason to use them anymore. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 83 | */ |
| 84 | |
| 85 | /* Functions */ |
| 86 | #define PyMem_New(type, n) \ |
| 87 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) |
| 88 | #define PyMem_Resize(p, type, n) \ |
Vladimir Marangozov | dcb45c3 | 2000-08-13 11:59:08 +0000 | [diff] [blame] | 89 | ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 90 | |
| 91 | /* In order to avoid breaking old code mixing PyObject_{New, NEW} with |
| 92 | PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter |
| 93 | have to be redirected to the object allocator. */ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 94 | #define PyMem_Del PyObject_Free |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 95 | |
| 96 | /* Macros */ |
| 97 | #define PyMem_NEW(type, n) \ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 98 | ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) |
| 99 | #define PyMem_RESIZE(p, type, n) \ |
| 100 | ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 101 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 102 | #define PyMem_DEL PyObject_FREE |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 103 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 108 | #endif /* !Py_PYMEM_H */ |