Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_MYMALLOC_H |
| 2 | #define Py_MYMALLOC_H |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 3 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 4 | Copyright (c) 2000, BeOpen.com. |
| 5 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 6 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 7 | All rights reserved. |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 9 | See the file "Misc/COPYRIGHT" for information on usage and |
| 10 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 11 | ******************************************************************/ |
| 12 | |
| 13 | /* Lowest-level memory allocation interface */ |
| 14 | |
| 15 | #ifdef macintosh |
| 16 | #define ANY void |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
| 19 | #ifdef __STDC__ |
| 20 | #define ANY void |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 23 | #ifdef __TURBOC__ |
| 24 | #define ANY void |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #ifdef __GNUC__ |
| 28 | #define ANY void |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 31 | #ifndef ANY |
| 32 | #define ANY char |
| 33 | #endif |
| 34 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 35 | #ifdef HAVE_STDLIB_H |
| 36 | #include <stdlib.h> |
Guido van Rossum | 6c1874f | 1995-01-10 17:43:33 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 39 | #include "myproto.h" |
| 40 | |
Guido van Rossum | 6c1874f | 1995-01-10 17:43:33 +0000 | [diff] [blame] | 41 | #ifdef __cplusplus |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 42 | /* Move this down here since some C++ #include's don't like to be included |
| 43 | inside an extern "C" */ |
Guido van Rossum | 6c1874f | 1995-01-10 17:43:33 +0000 | [diff] [blame] | 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Jack Jansen | f9480ce | 1995-06-27 13:12:09 +0000 | [diff] [blame] | 47 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | 0acd4b6 | 1995-02-18 14:50:12 +0000 | [diff] [blame] | 48 | #pragma lib_export on |
| 49 | #endif |
| 50 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 51 | #ifndef DL_IMPORT /* declarations for DLL import */ |
| 52 | #define DL_IMPORT(RTYPE) RTYPE |
Guido van Rossum | ab589b9 | 1997-08-21 16:13:37 +0000 | [diff] [blame] | 53 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 55 | #ifndef NULL |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 56 | #define NULL ((ANY *)0) |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 59 | #ifdef MALLOC_ZERO_RETURNS_NULL |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 60 | /* XXX Always allocate one extra byte, since some malloc's return NULL |
| 61 | XXX for malloc(0) or realloc(p, 0). */ |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 62 | #define _PyMem_EXTRA 1 |
| 63 | #else |
| 64 | #define _PyMem_EXTRA 0 |
| 65 | #endif |
| 66 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Core memory allocator |
| 69 | * ===================== |
| 70 | */ |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 72 | /* To make sure the interpreter is user-malloc friendly, all memory |
| 73 | APIs are implemented on top of this one. |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 75 | The PyCore_* macros can be defined to make the interpreter use a |
| 76 | custom allocator. Note that they are for internal use only. Both |
Guido van Rossum | 3a03d4c | 2000-05-05 15:36:09 +0000 | [diff] [blame] | 77 | the core and extension modules should use the PyMem_* API. |
| 78 | |
| 79 | See the comment block at the end of this file for two scenarios |
| 80 | showing how to use this to use a different allocator. */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 81 | |
| 82 | #ifndef PyCore_MALLOC_FUNC |
| 83 | #undef PyCore_REALLOC_FUNC |
| 84 | #undef PyCore_FREE_FUNC |
| 85 | #define PyCore_MALLOC_FUNC malloc |
| 86 | #define PyCore_REALLOC_FUNC realloc |
| 87 | #define PyCore_FREE_FUNC free |
| 88 | #endif |
| 89 | |
| 90 | #ifndef PyCore_MALLOC_PROTO |
| 91 | #undef PyCore_REALLOC_PROTO |
| 92 | #undef PyCore_FREE_PROTO |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame^] | 93 | #define PyCore_MALLOC_PROTO (size_t) |
| 94 | #define PyCore_REALLOC_PROTO (ANY *, size_t) |
| 95 | #define PyCore_FREE_PROTO (ANY *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | #ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 99 | extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO; |
| 100 | extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO; |
| 101 | extern void PyCore_FREE_FUNC PyCore_FREE_PROTO; |
| 102 | #endif |
| 103 | |
| 104 | #ifndef PyCore_MALLOC |
| 105 | #undef PyCore_REALLOC |
| 106 | #undef PyCore_FREE |
| 107 | #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n) |
| 108 | #define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n)) |
| 109 | #define PyCore_FREE(p) PyCore_FREE_FUNC(p) |
| 110 | #endif |
| 111 | |
| 112 | /* BEWARE: |
| 113 | |
| 114 | Each interface exports both functions and macros. Extension modules |
| 115 | should normally use the functions for ensuring binary compatibility |
| 116 | of the user's code across Python versions. Subsequently, if the |
| 117 | Python runtime switches to its own malloc (different from standard |
| 118 | malloc), no recompilation is required for the extensions. |
| 119 | |
| 120 | The macro versions trade compatibility for speed. They can be used |
| 121 | whenever there is a performance problem, but their use implies |
| 122 | recompilation of the code for each new Python release. The Python |
| 123 | core uses the macros because it *is* compiled on every upgrade. |
| 124 | This might not be the case with 3rd party extensions in a custom |
| 125 | setup (for example, a customer does not always have access to the |
| 126 | source of 3rd party deliverables). You have been warned! */ |
| 127 | |
| 128 | /* |
| 129 | * Raw memory interface |
| 130 | * ==================== |
| 131 | */ |
| 132 | |
| 133 | /* Functions */ |
| 134 | |
| 135 | /* Function wrappers around PyCore_MALLOC and friends; useful if you |
| 136 | need to be sure that you are using the same memory allocator as |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 137 | Python. Note that the wrappers make sure that allocating 0 bytes |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 138 | returns a non-NULL pointer, even if the underlying malloc |
| 139 | doesn't. Returned pointers must be checked for NULL explicitly. |
| 140 | No action is performed on failure. */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame^] | 141 | extern DL_IMPORT(ANY *) PyMem_Malloc(size_t); |
| 142 | extern DL_IMPORT(ANY *) PyMem_Realloc(ANY *, size_t); |
| 143 | extern DL_IMPORT(void) PyMem_Free(ANY *); |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 145 | /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are |
| 146 | no longer supported. They used to call PyErr_NoMemory() on failure. */ |
| 147 | |
| 148 | /* Macros */ |
| 149 | #define PyMem_MALLOC(n) PyCore_MALLOC(n) |
| 150 | #define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n)) |
| 151 | #define PyMem_FREE(p) PyCore_FREE((ANY *)(p)) |
| 152 | |
| 153 | /* |
| 154 | * Type-oriented memory interface |
| 155 | * ============================== |
| 156 | */ |
| 157 | |
| 158 | /* Functions */ |
| 159 | #define PyMem_New(type, n) \ |
| 160 | ( (type *) PyMem_Malloc((n) * sizeof(type)) ) |
| 161 | #define PyMem_Resize(p, type, n) \ |
| 162 | ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) ) |
| 163 | #define PyMem_Del(p) PyMem_Free(p) |
| 164 | |
| 165 | /* Macros */ |
| 166 | #define PyMem_NEW(type, n) \ |
| 167 | ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) ) |
| 168 | #define PyMem_RESIZE(p, type, n) \ |
| 169 | if ((p) == NULL) \ |
| 170 | (p) = (type *)(PyMem_MALLOC( \ |
| 171 | _PyMem_EXTRA + (n) * sizeof(type))); \ |
| 172 | else \ |
| 173 | (p) = (type *)(PyMem_REALLOC((p), \ |
| 174 | _PyMem_EXTRA + (n) * sizeof(type))) |
| 175 | #define PyMem_DEL(p) PyMem_FREE(p) |
| 176 | |
| 177 | /* PyMem_XDEL is deprecated. To avoid the call when p is NULL, |
| 178 | it is recommended to write the test explicitly in the code. |
| 179 | Note that according to ANSI C, free(NULL) has no effect. */ |
| 180 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 181 | #ifdef __cplusplus |
| 182 | } |
| 183 | #endif |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 3a03d4c | 2000-05-05 15:36:09 +0000 | [diff] [blame] | 185 | /* SCENARIOS |
| 186 | |
| 187 | Here are two scenarios by Vladimir Marangozov (the author of the |
| 188 | memory allocation redesign). |
| 189 | |
| 190 | 1) Scenario A |
| 191 | |
| 192 | Suppose you want to use a debugging malloc library that collects info on |
| 193 | where the malloc calls originate from. Assume the interface is: |
| 194 | |
| 195 | d_malloc(size_t n, char* src_file, unsigned long src_line) c.s. |
| 196 | |
| 197 | In this case, you would define (for example in config.h) : |
| 198 | |
| 199 | #define PyCore_MALLOC_FUNC d_malloc |
| 200 | ... |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame^] | 201 | #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long) |
Guido van Rossum | 3a03d4c | 2000-05-05 15:36:09 +0000 | [diff] [blame] | 202 | ... |
| 203 | #define NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 204 | |
| 205 | #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__) |
| 206 | ... |
| 207 | |
| 208 | 2) Scenario B |
| 209 | |
| 210 | Suppose you want to use malloc hooks (defined & initialized in a 3rd party |
| 211 | malloc library) instead of malloc functions. In this case, you would |
| 212 | define: |
| 213 | |
| 214 | #define PyCore_MALLOC_FUNC (*malloc_hook) |
| 215 | ... |
| 216 | #define NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 217 | |
| 218 | and ignore the previous definitions about PyCore_MALLOC_FUNC, etc. |
| 219 | |
| 220 | |
| 221 | */ |
| 222 | |
| 223 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 224 | #endif /* !Py_MYMALLOC_H */ |