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