annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/python3.8/pymem.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 /* The PyMem_ family: low-level memory allocation interfaces.
jpayne@69 2 See objimpl.h for the PyObject_ memory family.
jpayne@69 3 */
jpayne@69 4
jpayne@69 5 #ifndef Py_PYMEM_H
jpayne@69 6 #define Py_PYMEM_H
jpayne@69 7
jpayne@69 8 #include "pyport.h"
jpayne@69 9
jpayne@69 10 #ifdef __cplusplus
jpayne@69 11 extern "C" {
jpayne@69 12 #endif
jpayne@69 13
jpayne@69 14 /* BEWARE:
jpayne@69 15
jpayne@69 16 Each interface exports both functions and macros. Extension modules should
jpayne@69 17 use the functions, to ensure binary compatibility across Python versions.
jpayne@69 18 Because the Python implementation is free to change internal details, and
jpayne@69 19 the macros may (or may not) expose details for speed, if you do use the
jpayne@69 20 macros you must recompile your extensions with each Python release.
jpayne@69 21
jpayne@69 22 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
jpayne@69 23 calloc/free. For example, on Windows different DLLs may end up using
jpayne@69 24 different heaps, and if you use PyMem_Malloc you'll get the memory from the
jpayne@69 25 heap used by the Python DLL; it could be a disaster if you free()'ed that
jpayne@69 26 directly in your own extension. Using PyMem_Free instead ensures Python
jpayne@69 27 can return the memory to the proper heap. As another example, in
jpayne@69 28 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
jpayne@69 29 memory functions in special debugging wrappers that add additional
jpayne@69 30 debugging info to dynamic memory blocks. The system routines have no idea
jpayne@69 31 what to do with that stuff, and the Python wrappers have no idea what to do
jpayne@69 32 with raw blocks obtained directly by the system routines then.
jpayne@69 33
jpayne@69 34 The GIL must be held when using these APIs.
jpayne@69 35 */
jpayne@69 36
jpayne@69 37 /*
jpayne@69 38 * Raw memory interface
jpayne@69 39 * ====================
jpayne@69 40 */
jpayne@69 41
jpayne@69 42 /* Functions
jpayne@69 43
jpayne@69 44 Functions supplying platform-independent semantics for malloc/realloc/
jpayne@69 45 free. These functions make sure that allocating 0 bytes returns a distinct
jpayne@69 46 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
jpayne@69 47 may be returned), even if the platform malloc and realloc don't.
jpayne@69 48 Returned pointers must be checked for NULL explicitly. No action is
jpayne@69 49 performed on failure (no exception is set, no warning is printed, etc).
jpayne@69 50 */
jpayne@69 51
jpayne@69 52 PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
jpayne@69 53 PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
jpayne@69 54 PyAPI_FUNC(void) PyMem_Free(void *ptr);
jpayne@69 55
jpayne@69 56 /* Macros. */
jpayne@69 57
jpayne@69 58 /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
jpayne@69 59 for malloc(0), which would be treated as an error. Some platforms
jpayne@69 60 would return a pointer with no memory behind it, which would break
jpayne@69 61 pymalloc. To solve these problems, allocate an extra byte. */
jpayne@69 62 /* Returns NULL to indicate error if a negative size or size larger than
jpayne@69 63 Py_ssize_t can represent is supplied. Helps prevents security holes. */
jpayne@69 64 #define PyMem_MALLOC(n) PyMem_Malloc(n)
jpayne@69 65 #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
jpayne@69 66 #define PyMem_FREE(p) PyMem_Free(p)
jpayne@69 67
jpayne@69 68 /*
jpayne@69 69 * Type-oriented memory interface
jpayne@69 70 * ==============================
jpayne@69 71 *
jpayne@69 72 * Allocate memory for n objects of the given type. Returns a new pointer
jpayne@69 73 * or NULL if the request was too large or memory allocation failed. Use
jpayne@69 74 * these macros rather than doing the multiplication yourself so that proper
jpayne@69 75 * overflow checking is always done.
jpayne@69 76 */
jpayne@69 77
jpayne@69 78 #define PyMem_New(type, n) \
jpayne@69 79 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
jpayne@69 80 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
jpayne@69 81 #define PyMem_NEW(type, n) \
jpayne@69 82 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
jpayne@69 83 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
jpayne@69 84
jpayne@69 85 /*
jpayne@69 86 * The value of (p) is always clobbered by this macro regardless of success.
jpayne@69 87 * The caller MUST check if (p) is NULL afterwards and deal with the memory
jpayne@69 88 * error if so. This means the original value of (p) MUST be saved for the
jpayne@69 89 * caller's memory error handler to not lose track of it.
jpayne@69 90 */
jpayne@69 91 #define PyMem_Resize(p, type, n) \
jpayne@69 92 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
jpayne@69 93 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
jpayne@69 94 #define PyMem_RESIZE(p, type, n) \
jpayne@69 95 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
jpayne@69 96 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
jpayne@69 97
jpayne@69 98 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
jpayne@69 99 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
jpayne@69 100 */
jpayne@69 101 #define PyMem_Del PyMem_Free
jpayne@69 102 #define PyMem_DEL PyMem_FREE
jpayne@69 103
jpayne@69 104 /* bpo-35053: expose _Py_tracemalloc_config for performance:
jpayne@69 105 _Py_NewReference() needs an efficient check to test if tracemalloc is
jpayne@69 106 tracing.
jpayne@69 107
jpayne@69 108 It has to be defined in pymem.h, before object.h is included. */
jpayne@69 109 struct _PyTraceMalloc_Config {
jpayne@69 110 /* Module initialized?
jpayne@69 111 Variable protected by the GIL */
jpayne@69 112 enum {
jpayne@69 113 TRACEMALLOC_NOT_INITIALIZED,
jpayne@69 114 TRACEMALLOC_INITIALIZED,
jpayne@69 115 TRACEMALLOC_FINALIZED
jpayne@69 116 } initialized;
jpayne@69 117
jpayne@69 118 /* Is tracemalloc tracing memory allocations?
jpayne@69 119 Variable protected by the GIL */
jpayne@69 120 int tracing;
jpayne@69 121
jpayne@69 122 /* limit of the number of frames in a traceback, 1 by default.
jpayne@69 123 Variable protected by the GIL. */
jpayne@69 124 int max_nframe;
jpayne@69 125
jpayne@69 126 /* use domain in trace key?
jpayne@69 127 Variable protected by the GIL. */
jpayne@69 128 int use_domain;
jpayne@69 129 };
jpayne@69 130
jpayne@69 131 PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;
jpayne@69 132
jpayne@69 133 #define _PyTraceMalloc_Config_INIT \
jpayne@69 134 {.initialized = TRACEMALLOC_NOT_INITIALIZED, \
jpayne@69 135 .tracing = 0, \
jpayne@69 136 .max_nframe = 1, \
jpayne@69 137 .use_domain = 0}
jpayne@69 138
jpayne@69 139
jpayne@69 140 #ifndef Py_LIMITED_API
jpayne@69 141 # define Py_CPYTHON_PYMEM_H
jpayne@69 142 # include "cpython/pymem.h"
jpayne@69 143 # undef Py_CPYTHON_PYMEM_H
jpayne@69 144 #endif
jpayne@69 145
jpayne@69 146 #ifdef __cplusplus
jpayne@69 147 }
jpayne@69 148 #endif
jpayne@69 149
jpayne@69 150 #endif /* !Py_PYMEM_H */