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