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