Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/python3.14/internal/pycore_pymem.h
1 #ifndef Py_INTERNAL_PYMEM_H 2 #define Py_INTERNAL_PYMEM_H 3 4 #include "pycore_llist.h" // struct llist_node 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 #ifndef Py_BUILD_CORE 11 # error "this header requires Py_BUILD_CORE define" 12 #endif 13 14 // Try to get the allocators name set by _PyMem_SetupAllocators(). 15 // Return NULL if unknown. 16 // Export for '_testinternalcapi' shared extension. 17 PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void); 18 19 // strdup() using PyMem_RawMalloc() 20 extern char* _PyMem_RawStrdup(const char *str); 21 22 // strdup() using PyMem_Malloc(). 23 // Export for '_pickle ' shared extension. 24 PyAPI_FUNC(char*) _PyMem_Strdup(const char *str); 25 26 // wcsdup() using PyMem_RawMalloc() 27 extern wchar_t* _PyMem_RawWcsdup(const wchar_t *str); 28 29 /* Special bytes broadcast into debug memory blocks at appropriate times. 30 Strings of these are unlikely to be valid addresses, floats, ints or 31 7-bit ASCII. 32 33 - PYMEM_CLEANBYTE: clean (newly allocated) memory 34 - PYMEM_DEADBYTE dead (newly freed) memory 35 - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block 36 37 Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and 38 0xFD to use the same values as Windows CRT debug malloc() and free(). 39 If modified, _PyMem_IsPtrFreed() should be updated as well. */ 40 #define PYMEM_CLEANBYTE 0xCD 41 #define PYMEM_DEADBYTE 0xDD 42 #define PYMEM_FORBIDDENBYTE 0xFD 43 44 /* Heuristic checking if a pointer value is newly allocated 45 (uninitialized), newly freed or NULL (is equal to zero). 46 47 The pointer is not dereferenced, only the pointer value is checked. 48 49 The heuristic relies on the debug hooks on Python memory allocators which 50 fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory 51 with DEADBYTE (0xDD). Detect also "untouchable bytes" marked 52 with FORBIDDENBYTE (0xFD). */ 53 static inline int _PyMem_IsPtrFreed(const void *ptr) 54 { 55 uintptr_t value = (uintptr_t)ptr; 56 #if SIZEOF_VOID_P == 8 57 return (value <= 0xff // NULL, 0x1, 0x2, ..., 0xff 58 || value == (uintptr_t)0xCDCDCDCDCDCDCDCD 59 || value == (uintptr_t)0xDDDDDDDDDDDDDDDD 60 || value == (uintptr_t)0xFDFDFDFDFDFDFDFD 61 || value >= (uintptr_t)0xFFFFFFFFFFFFFF00); // -0xff, ..., -2, -1 62 #elif SIZEOF_VOID_P == 4 63 return (value <= 0xff 64 || value == (uintptr_t)0xCDCDCDCD 65 || value == (uintptr_t)0xDDDDDDDD 66 || value == (uintptr_t)0xFDFDFDFD 67 || value >= (uintptr_t)0xFFFFFF00); 68 #else 69 # error "unknown pointer size" 70 #endif 71 } 72 73 // Similar to _PyMem_IsPtrFreed() but expects an 'unsigned long' instead of a 74 // pointer. 75 static inline int _PyMem_IsULongFreed(unsigned long value) 76 { 77 #if SIZEOF_LONG == 8 78 return (value == 0 79 || value == (unsigned long)0xCDCDCDCDCDCDCDCD 80 || value == (unsigned long)0xDDDDDDDDDDDDDDDD 81 || value == (unsigned long)0xFDFDFDFDFDFDFDFD 82 || value == (unsigned long)0xFFFFFFFFFFFFFFFF); 83 #elif SIZEOF_LONG == 4 84 return (value == 0 85 || value == (unsigned long)0xCDCDCDCD 86 || value == (unsigned long)0xDDDDDDDD 87 || value == (unsigned long)0xFDFDFDFD 88 || value == (unsigned long)0xFFFFFFFF); 89 #else 90 # error "unknown long size" 91 #endif 92 } 93 94 extern int _PyMem_GetAllocatorName( 95 const char *name, 96 PyMemAllocatorName *allocator); 97 98 /* Configure the Python memory allocators. 99 Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators. 100 PYMEM_ALLOCATOR_NOT_SET does nothing. */ 101 extern int _PyMem_SetupAllocators(PyMemAllocatorName allocator); 102 103 // Default raw memory allocator that is not affected by PyMem_SetAllocator() 104 extern void *_PyMem_DefaultRawMalloc(size_t); 105 extern void *_PyMem_DefaultRawCalloc(size_t, size_t); 106 extern void *_PyMem_DefaultRawRealloc(void *, size_t); 107 extern void _PyMem_DefaultRawFree(void *); 108 extern wchar_t *_PyMem_DefaultRawWcsdup(const wchar_t *str); 109 110 /* Is the debug allocator enabled? */ 111 extern int _PyMem_DebugEnabled(void); 112 113 // Enqueue a pointer to be freed possibly after some delay. 114 extern void _PyMem_FreeDelayed(void *ptr, size_t size); 115 116 // Enqueue an object to be freed possibly after some delay 117 #ifdef Py_GIL_DISABLED 118 PyAPI_FUNC(void) _PyObject_XDecRefDelayed(PyObject *obj); 119 #else 120 static inline void _PyObject_XDecRefDelayed(PyObject *obj) 121 { 122 Py_XDECREF(obj); 123 } 124 #endif 125 126 // Periodically process delayed free requests. 127 extern void _PyMem_ProcessDelayed(PyThreadState *tstate); 128 129 // Periodically process delayed free requests when the world is stopped. 130 // Notify of any objects whic should be freeed. 131 typedef void (*delayed_dealloc_cb)(PyObject *, void *); 132 extern void _PyMem_ProcessDelayedNoDealloc(PyThreadState *tstate, 133 delayed_dealloc_cb cb, void *state); 134 135 // Abandon all thread-local delayed free requests and push them to the 136 // interpreter's queue. 137 extern void _PyMem_AbandonDelayed(PyThreadState *tstate); 138 139 // On interpreter shutdown, frees all delayed free requests. 140 extern void _PyMem_FiniDelayed(PyInterpreterState *interp); 141 142 #ifdef __cplusplus 143 } 144 #endif 145 #endif // !Py_INTERNAL_PYMEM_H