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. */ |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 55 | #ifndef PyMem_MALLOC |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 56 | #ifdef MALLOC_ZERO_RETURNS_NULL |
| 57 | #define PyMem_MALLOC(n) malloc((n) ? (n) : 1) |
| 58 | #else |
| 59 | #define PyMem_MALLOC malloc |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 60 | #endif |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 61 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 62 | /* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to |
| 63 | do with whether platform realloc(non-NULL, 0) normally frees the memory |
| 64 | or returns NULL. Rather than introduce yet another config variation, |
| 65 | just make a realloc to 0 bytes act as if to 1 instead. */ |
| 66 | #define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1) |
| 67 | |
| 68 | #define PyMem_FREE free |
| 69 | #endif /* PyMem_MALLOC */ |
| 70 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Type-oriented memory interface |
| 73 | * ============================== |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 74 | * |
| 75 | * These are carried along for historical reasons. There's rarely a good |
| 76 | * reason to use them anymore. |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 77 | */ |
| 78 | |
| 79 | /* Functions */ |
| 80 | #define PyMem_New(type, n) \ |
| 81 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) |
| 82 | #define PyMem_Resize(p, type, n) \ |
Vladimir Marangozov | dcb45c3 | 2000-08-13 11:59:08 +0000 | [diff] [blame] | 83 | ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 84 | |
| 85 | /* In order to avoid breaking old code mixing PyObject_{New, NEW} with |
| 86 | PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter |
| 87 | have to be redirected to the object allocator. */ |
| 88 | /* XXX The parser module needs rework before this can be enabled. */ |
| 89 | #if 0 |
| 90 | #define PyMem_Del PyObject_Free |
| 91 | #else |
| 92 | #define PyMem_Del PyMem_Free |
| 93 | #endif |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 94 | |
| 95 | /* Macros */ |
| 96 | #define PyMem_NEW(type, n) \ |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 97 | ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) |
| 98 | #define PyMem_RESIZE(p, type, n) \ |
| 99 | ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 100 | |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame^] | 101 | /* XXX The parser module needs rework before this can be enabled. */ |
| 102 | #if 0 |
| 103 | #define PyMem_DEL PyObject_FREE |
| 104 | #else |
| 105 | #define PyMem_DEL PyMem_FREE |
| 106 | #endif |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 107 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 108 | #ifdef __cplusplus |
| 109 | } |
| 110 | #endif |
| 111 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 112 | #endif /* !Py_PYMEM_H */ |