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