• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_PYMEM_H
2 #define Py_INTERNAL_PYMEM_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include "pymem.h"      // PyMemAllocatorName
12 
13 
14 /* Set the memory allocator of the specified domain to the default.
15    Save the old allocator into *old_alloc if it's non-NULL.
16    Return on success, or return -1 if the domain is unknown. */
17 PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
18     PyMemAllocatorDomain domain,
19     PyMemAllocatorEx *old_alloc);
20 
21 /* Special bytes broadcast into debug memory blocks at appropriate times.
22    Strings of these are unlikely to be valid addresses, floats, ints or
23    7-bit ASCII.
24 
25    - PYMEM_CLEANBYTE: clean (newly allocated) memory
26    - PYMEM_DEADBYTE dead (newly freed) memory
27    - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block
28 
29    Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and
30    0xFD to use the same values than Windows CRT debug malloc() and free().
31    If modified, _PyMem_IsPtrFreed() should be updated as well. */
32 #define PYMEM_CLEANBYTE      0xCD
33 #define PYMEM_DEADBYTE       0xDD
34 #define PYMEM_FORBIDDENBYTE  0xFD
35 
36 /* Heuristic checking if a pointer value is newly allocated
37    (uninitialized), newly freed or NULL (is equal to zero).
38 
39    The pointer is not dereferenced, only the pointer value is checked.
40 
41    The heuristic relies on the debug hooks on Python memory allocators which
42    fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory
43    with DEADBYTE (0xDD). Detect also "untouchable bytes" marked
44    with FORBIDDENBYTE (0xFD). */
_PyMem_IsPtrFreed(void * ptr)45 static inline int _PyMem_IsPtrFreed(void *ptr)
46 {
47     uintptr_t value = (uintptr_t)ptr;
48 #if SIZEOF_VOID_P == 8
49     return (value == 0
50             || value == (uintptr_t)0xCDCDCDCDCDCDCDCD
51             || value == (uintptr_t)0xDDDDDDDDDDDDDDDD
52             || value == (uintptr_t)0xFDFDFDFDFDFDFDFD);
53 #elif SIZEOF_VOID_P == 4
54     return (value == 0
55             || value == (uintptr_t)0xCDCDCDCD
56             || value == (uintptr_t)0xDDDDDDDD
57             || value == (uintptr_t)0xFDFDFDFD);
58 #else
59 #  error "unknown pointer size"
60 #endif
61 }
62 
63 PyAPI_FUNC(int) _PyMem_GetAllocatorName(
64     const char *name,
65     PyMemAllocatorName *allocator);
66 
67 /* Configure the Python memory allocators.
68    Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators.
69    PYMEM_ALLOCATOR_NOT_SET does nothing. */
70 PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
71 
72 /* bpo-35053: Expose _Py_tracemalloc_config for _Py_NewReference()
73    which access directly _Py_tracemalloc_config.tracing for best
74    performances. */
75 struct _PyTraceMalloc_Config {
76     /* Module initialized?
77        Variable protected by the GIL */
78     enum {
79         TRACEMALLOC_NOT_INITIALIZED,
80         TRACEMALLOC_INITIALIZED,
81         TRACEMALLOC_FINALIZED
82     } initialized;
83 
84     /* Is tracemalloc tracing memory allocations?
85        Variable protected by the GIL */
86     int tracing;
87 
88     /* limit of the number of frames in a traceback, 1 by default.
89        Variable protected by the GIL. */
90     int max_nframe;
91 };
92 
93 #define _PyTraceMalloc_Config_INIT \
94     {.initialized = TRACEMALLOC_NOT_INITIALIZED, \
95      .tracing = 0, \
96      .max_nframe = 1}
97 
98 PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;
99 
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 #endif /* !Py_INTERNAL_PYMEM_H */
105