File indexing completed on 2025-11-19 09:50:49
0001 #ifndef Py_INTERNAL_PYMEM_H
0002 #define Py_INTERNAL_PYMEM_H
0003
0004 #include "pycore_llist.h" // struct llist_node
0005 #include "pycore_lock.h" // PyMutex
0006
0007 #ifdef __cplusplus
0008 extern "C" {
0009 #endif
0010
0011 #ifndef Py_BUILD_CORE
0012 # error "this header requires Py_BUILD_CORE define"
0013 #endif
0014
0015
0016
0017
0018 PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
0019
0020
0021 extern char* _PyMem_RawStrdup(const char *str);
0022
0023
0024
0025 PyAPI_FUNC(char*) _PyMem_Strdup(const char *str);
0026
0027
0028 extern wchar_t* _PyMem_RawWcsdup(const wchar_t *str);
0029
0030 typedef struct {
0031
0032 char api_id;
0033 PyMemAllocatorEx alloc;
0034 } debug_alloc_api_t;
0035
0036 struct _pymem_allocators {
0037 PyMutex mutex;
0038 struct {
0039 PyMemAllocatorEx raw;
0040 PyMemAllocatorEx mem;
0041 PyMemAllocatorEx obj;
0042 } standard;
0043 struct {
0044 debug_alloc_api_t raw;
0045 debug_alloc_api_t mem;
0046 debug_alloc_api_t obj;
0047 } debug;
0048 int is_debug_enabled;
0049 PyObjectArenaAllocator obj_arena;
0050 };
0051
0052 struct _Py_mem_interp_free_queue {
0053 int has_work;
0054 PyMutex mutex;
0055 struct llist_node head;
0056 };
0057
0058
0059
0060
0061 extern int _PyMem_SetDefaultAllocator(
0062 PyMemAllocatorDomain domain,
0063 PyMemAllocatorEx *old_alloc);
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 #define PYMEM_CLEANBYTE 0xCD
0077 #define PYMEM_DEADBYTE 0xDD
0078 #define PYMEM_FORBIDDENBYTE 0xFD
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static inline int _PyMem_IsPtrFreed(const void *ptr)
0090 {
0091 uintptr_t value = (uintptr_t)ptr;
0092 #if SIZEOF_VOID_P == 8
0093 return (value == 0
0094 || value == (uintptr_t)0xCDCDCDCDCDCDCDCD
0095 || value == (uintptr_t)0xDDDDDDDDDDDDDDDD
0096 || value == (uintptr_t)0xFDFDFDFDFDFDFDFD);
0097 #elif SIZEOF_VOID_P == 4
0098 return (value == 0
0099 || value == (uintptr_t)0xCDCDCDCD
0100 || value == (uintptr_t)0xDDDDDDDD
0101 || value == (uintptr_t)0xFDFDFDFD);
0102 #else
0103 # error "unknown pointer size"
0104 #endif
0105 }
0106
0107 extern int _PyMem_GetAllocatorName(
0108 const char *name,
0109 PyMemAllocatorName *allocator);
0110
0111
0112
0113
0114 extern int _PyMem_SetupAllocators(PyMemAllocatorName allocator);
0115
0116
0117 extern int _PyMem_DebugEnabled(void);
0118
0119
0120 extern void _PyMem_FreeDelayed(void *ptr);
0121
0122
0123 extern void _PyObject_FreeDelayed(void *ptr);
0124
0125
0126 extern void _PyMem_ProcessDelayed(PyThreadState *tstate);
0127
0128
0129
0130 extern void _PyMem_AbandonDelayed(PyThreadState *tstate);
0131
0132
0133 extern void _PyMem_FiniDelayed(PyInterpreterState *interp);
0134
0135 #ifdef __cplusplus
0136 }
0137 #endif
0138 #endif