|
| 1 | +/*********************************************************** |
| 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. |
| 6 | +
|
| 7 | +See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | +redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 9 | +******************************************************************/ |
| 10 | + |
| 11 | +/* Lowest-level memory allocation interface */ |
| 12 | + |
| 13 | +#ifndef Py_PYMEM_H |
| 14 | +#define Py_PYMEM_H |
| 15 | + |
| 16 | +#include "pyport.h" |
| 17 | + |
| 18 | +#ifdef __cplusplus |
| 19 | +extern "C" { |
| 20 | +#endif |
| 21 | + |
| 22 | +/* |
| 23 | + * Core memory allocator |
| 24 | + * ===================== |
| 25 | + */ |
| 26 | + |
| 27 | +/* To make sure the interpreter is user-malloc friendly, all memory |
| 28 | + APIs are implemented on top of this one. |
| 29 | +
|
| 30 | + The PyCore_* macros can be defined to make the interpreter use a |
| 31 | + custom allocator. Note that they are for internal use only. Both |
| 32 | + the core and extension modules should use the PyMem_* API. |
| 33 | +
|
| 34 | + See the comment block at the end of this file for two scenarios |
| 35 | + showing how to use this to use a different allocator. */ |
| 36 | + |
| 37 | +#ifndef PyCore_MALLOC_FUNC |
| 38 | +#undef PyCore_REALLOC_FUNC |
| 39 | +#undef PyCore_FREE_FUNC |
| 40 | +#define PyCore_MALLOC_FUNC malloc |
| 41 | +#define PyCore_REALLOC_FUNC realloc |
| 42 | +#define PyCore_FREE_FUNC free |
| 43 | +#endif |
| 44 | + |
| 45 | +#ifndef PyCore_MALLOC_PROTO |
| 46 | +#undef PyCore_REALLOC_PROTO |
| 47 | +#undef PyCore_FREE_PROTO |
| 48 | +#define PyCore_MALLOC_PROTO (size_t) |
| 49 | +#define PyCore_REALLOC_PROTO (void *, size_t) |
| 50 | +#define PyCore_FREE_PROTO (void *) |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 54 | +extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO; |
| 55 | +extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO; |
| 56 | +extern void PyCore_FREE_FUNC PyCore_FREE_PROTO; |
| 57 | +#endif |
| 58 | + |
| 59 | +#ifndef PyCore_MALLOC |
| 60 | +#undef PyCore_REALLOC |
| 61 | +#undef PyCore_FREE |
| 62 | +#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n) |
| 63 | +#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n)) |
| 64 | +#define PyCore_FREE(p) PyCore_FREE_FUNC(p) |
| 65 | +#endif |
| 66 | + |
| 67 | +/* BEWARE: |
| 68 | +
|
| 69 | + Each interface exports both functions and macros. Extension modules |
| 70 | + should normally use the functions for ensuring binary compatibility |
| 71 | + of the user's code across Python versions. Subsequently, if the |
| 72 | + Python runtime switches to its own malloc (different from standard |
| 73 | + malloc), no recompilation is required for the extensions. |
| 74 | +
|
| 75 | + The macro versions trade compatibility for speed. They can be used |
| 76 | + whenever there is a performance problem, but their use implies |
| 77 | + recompilation of the code for each new Python release. The Python |
| 78 | + core uses the macros because it *is* compiled on every upgrade. |
| 79 | + This might not be the case with 3rd party extensions in a custom |
| 80 | + setup (for example, a customer does not always have access to the |
| 81 | + source of 3rd party deliverables). You have been warned! */ |
| 82 | + |
| 83 | +/* |
| 84 | + * Raw memory interface |
| 85 | + * ==================== |
| 86 | + */ |
| 87 | + |
| 88 | +/* Functions */ |
| 89 | + |
| 90 | +/* Function wrappers around PyCore_MALLOC and friends; useful if you |
| 91 | + need to be sure that you are using the same memory allocator as |
| 92 | + Python. Note that the wrappers make sure that allocating 0 bytes |
| 93 | + returns a non-NULL pointer, even if the underlying malloc |
| 94 | + doesn't. Returned pointers must be checked for NULL explicitly. |
| 95 | + No action is performed on failure. */ |
| 96 | +extern DL_IMPORT(void *) PyMem_Malloc(size_t); |
| 97 | +extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t); |
| 98 | +extern DL_IMPORT(void) PyMem_Free(void *); |
| 99 | + |
| 100 | +/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are |
| 101 | + no longer supported. They used to call PyErr_NoMemory() on failure. */ |
| 102 | + |
| 103 | +/* Macros */ |
| 104 | +#define PyMem_MALLOC(n) PyCore_MALLOC(n) |
| 105 | +#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n)) |
| 106 | +#define PyMem_FREE(p) PyCore_FREE((void *)(p)) |
| 107 | + |
| 108 | +/* |
| 109 | + * Type-oriented memory interface |
| 110 | + * ============================== |
| 111 | + */ |
| 112 | + |
| 113 | +/* Functions */ |
| 114 | +#define PyMem_New(type, n) \ |
| 115 | + ( (type *) PyMem_Malloc((n) * sizeof(type)) ) |
| 116 | +#define PyMem_Resize(p, type, n) \ |
| 117 | + ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) ) |
| 118 | +#define PyMem_Del(p) PyMem_Free(p) |
| 119 | + |
| 120 | +/* Macros */ |
| 121 | +#define PyMem_NEW(type, n) \ |
| 122 | + ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) ) |
| 123 | +#define PyMem_RESIZE(p, type, n) \ |
| 124 | + if ((p) == NULL) \ |
| 125 | + (p) = (type *)(PyMem_MALLOC( \ |
| 126 | + _PyMem_EXTRA + (n) * sizeof(type))); \ |
| 127 | + else \ |
| 128 | + (p) = (type *)(PyMem_REALLOC((p), \ |
| 129 | + _PyMem_EXTRA + (n) * sizeof(type))) |
| 130 | +#define PyMem_DEL(p) PyMem_FREE(p) |
| 131 | + |
| 132 | +/* PyMem_XDEL is deprecated. To avoid the call when p is NULL, |
| 133 | + it is recommended to write the test explicitly in the code. |
| 134 | + Note that according to ANSI C, free(NULL) has no effect. */ |
| 135 | + |
| 136 | +#ifdef __cplusplus |
| 137 | +} |
| 138 | +#endif |
| 139 | + |
| 140 | +/* SCENARIOS |
| 141 | +
|
| 142 | + Here are two scenarios by Vladimir Marangozov (the author of the |
| 143 | + memory allocation redesign). |
| 144 | +
|
| 145 | + 1) Scenario A |
| 146 | +
|
| 147 | + Suppose you want to use a debugging malloc library that collects info on |
| 148 | + where the malloc calls originate from. Assume the interface is: |
| 149 | +
|
| 150 | + d_malloc(size_t n, char* src_file, unsigned long src_line) c.s. |
| 151 | +
|
| 152 | + In this case, you would define (for example in config.h) : |
| 153 | +
|
| 154 | + #define PyCore_MALLOC_FUNC d_malloc |
| 155 | + ... |
| 156 | + #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long) |
| 157 | + ... |
| 158 | + #define NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 159 | +
|
| 160 | + #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__) |
| 161 | + ... |
| 162 | +
|
| 163 | + 2) Scenario B |
| 164 | +
|
| 165 | + Suppose you want to use malloc hooks (defined & initialized in a 3rd party |
| 166 | + malloc library) instead of malloc functions. In this case, you would |
| 167 | + define: |
| 168 | +
|
| 169 | + #define PyCore_MALLOC_FUNC (*malloc_hook) |
| 170 | + ... |
| 171 | + #define NEED_TO_DECLARE_MALLOC_AND_FRIEND |
| 172 | +
|
| 173 | + and ignore the previous definitions about PyCore_MALLOC_FUNC, etc. |
| 174 | +
|
| 175 | +
|
| 176 | +*/ |
| 177 | + |
| 178 | + |
| 179 | +#endif /* !Py_PYMEM_H */ |
0 commit comments