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 | |
| 21 | The macro versions trade compatibility for speed. They can be used |
| 22 | whenever there is a performance problem, but their use implies |
| 23 | recompilation of the code for each new Python release. The Python |
| 24 | core uses the macros because it *is* compiled on every upgrade. |
| 25 | This might not be the case with 3rd party extensions in a custom |
| 26 | setup (for example, a customer does not always have access to the |
| 27 | source of 3rd party deliverables). You have been warned! */ |
| 28 | |
| 29 | /* |
| 30 | * Raw memory interface |
| 31 | * ==================== |
| 32 | */ |
| 33 | |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 34 | /* To make sure the interpreter is user-malloc friendly, all memory |
| 35 | APIs are implemented on top of this one. */ |
| 36 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 37 | /* Functions */ |
| 38 | |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 39 | /* Function wrappers around PyMem_MALLOC and friends; useful if you |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 40 | need to be sure that you are using the same memory allocator as |
| 41 | Python. Note that the wrappers make sure that allocating 0 bytes |
| 42 | returns a non-NULL pointer, even if the underlying malloc |
| 43 | doesn't. Returned pointers must be checked for NULL explicitly. |
| 44 | No action is performed on failure. */ |
| 45 | extern DL_IMPORT(void *) PyMem_Malloc(size_t); |
| 46 | extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t); |
| 47 | extern DL_IMPORT(void) PyMem_Free(void *); |
| 48 | |
| 49 | /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are |
| 50 | no longer supported. They used to call PyErr_NoMemory() on failure. */ |
| 51 | |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 52 | /* Macros (override these if you want to a different malloc */ |
| 53 | #ifndef PyMem_MALLOC |
| 54 | #define PyMem_MALLOC(n) malloc(n) |
| 55 | #define PyMem_REALLOC(p, n) realloc((void *)(p), (n)) |
| 56 | #define PyMem_FREE(p) free((void *)(p)) |
| 57 | #endif |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Type-oriented memory interface |
| 61 | * ============================== |
| 62 | */ |
| 63 | |
| 64 | /* Functions */ |
| 65 | #define PyMem_New(type, n) \ |
| 66 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) |
| 67 | #define PyMem_Resize(p, type, n) \ |
Vladimir Marangozov | dcb45c3 | 2000-08-13 11:59:08 +0000 | [diff] [blame] | 68 | ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 69 | #define PyMem_Del(p) PyMem_Free(p) |
| 70 | |
| 71 | /* Macros */ |
| 72 | #define PyMem_NEW(type, n) \ |
| 73 | ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) ) |
Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 74 | |
| 75 | /* See comment near MALLOC_ZERO_RETURNS_NULL in pyport.h. */ |
| 76 | #define PyMem_RESIZE(p, type, n) \ |
| 77 | do { \ |
| 78 | size_t _sum = (n) * sizeof(type); \ |
| 79 | if (!_sum) \ |
| 80 | _sum = 1; \ |
| 81 | (p) = (type *)((p) ? \ |
| 82 | PyMem_REALLOC(p, _sum) : \ |
| 83 | PyMem_MALLOC(_sum)); \ |
| 84 | } while (0) |
| 85 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 86 | #define PyMem_DEL(p) PyMem_FREE(p) |
| 87 | |
| 88 | /* PyMem_XDEL is deprecated. To avoid the call when p is NULL, |
| 89 | it is recommended to write the test explicitly in the code. |
| 90 | Note that according to ANSI C, free(NULL) has no effect. */ |
| 91 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 92 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 93 | #ifdef __cplusplus |
| 94 | } |
| 95 | #endif |
| 96 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 97 | #endif /* !Py_PYMEM_H */ |