Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:46

0001 #ifndef Py_INTERNAL_PYMEM_H
0002 #define Py_INTERNAL_PYMEM_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006 
0007 #ifndef Py_BUILD_CORE
0008 #  error "this header requires Py_BUILD_CORE define"
0009 #endif
0010 
0011 #include "pymem.h"      // PyMemAllocatorName
0012 
0013 
0014 typedef struct {
0015     /* We tag each block with an API ID in order to tag API violations */
0016     char api_id;
0017     PyMemAllocatorEx alloc;
0018 } debug_alloc_api_t;
0019 
0020 struct _pymem_allocators {
0021     PyThread_type_lock mutex;
0022     struct {
0023         PyMemAllocatorEx raw;
0024         PyMemAllocatorEx mem;
0025         PyMemAllocatorEx obj;
0026     } standard;
0027     struct {
0028         debug_alloc_api_t raw;
0029         debug_alloc_api_t mem;
0030         debug_alloc_api_t obj;
0031     } debug;
0032     PyObjectArenaAllocator obj_arena;
0033 };
0034 
0035 
0036 /* Set the memory allocator of the specified domain to the default.
0037    Save the old allocator into *old_alloc if it's non-NULL.
0038    Return on success, or return -1 if the domain is unknown. */
0039 PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
0040     PyMemAllocatorDomain domain,
0041     PyMemAllocatorEx *old_alloc);
0042 
0043 /* Special bytes broadcast into debug memory blocks at appropriate times.
0044    Strings of these are unlikely to be valid addresses, floats, ints or
0045    7-bit ASCII.
0046 
0047    - PYMEM_CLEANBYTE: clean (newly allocated) memory
0048    - PYMEM_DEADBYTE dead (newly freed) memory
0049    - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block
0050 
0051    Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and
0052    0xFD to use the same values than Windows CRT debug malloc() and free().
0053    If modified, _PyMem_IsPtrFreed() should be updated as well. */
0054 #define PYMEM_CLEANBYTE      0xCD
0055 #define PYMEM_DEADBYTE       0xDD
0056 #define PYMEM_FORBIDDENBYTE  0xFD
0057 
0058 /* Heuristic checking if a pointer value is newly allocated
0059    (uninitialized), newly freed or NULL (is equal to zero).
0060 
0061    The pointer is not dereferenced, only the pointer value is checked.
0062 
0063    The heuristic relies on the debug hooks on Python memory allocators which
0064    fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory
0065    with DEADBYTE (0xDD). Detect also "untouchable bytes" marked
0066    with FORBIDDENBYTE (0xFD). */
0067 static inline int _PyMem_IsPtrFreed(const void *ptr)
0068 {
0069     uintptr_t value = (uintptr_t)ptr;
0070 #if SIZEOF_VOID_P == 8
0071     return (value == 0
0072             || value == (uintptr_t)0xCDCDCDCDCDCDCDCD
0073             || value == (uintptr_t)0xDDDDDDDDDDDDDDDD
0074             || value == (uintptr_t)0xFDFDFDFDFDFDFDFD);
0075 #elif SIZEOF_VOID_P == 4
0076     return (value == 0
0077             || value == (uintptr_t)0xCDCDCDCD
0078             || value == (uintptr_t)0xDDDDDDDD
0079             || value == (uintptr_t)0xFDFDFDFD);
0080 #else
0081 #  error "unknown pointer size"
0082 #endif
0083 }
0084 
0085 PyAPI_FUNC(int) _PyMem_GetAllocatorName(
0086     const char *name,
0087     PyMemAllocatorName *allocator);
0088 
0089 /* Configure the Python memory allocators.
0090    Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators.
0091    PYMEM_ALLOCATOR_NOT_SET does nothing. */
0092 PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
0093 
0094 
0095 #ifdef __cplusplus
0096 }
0097 #endif
0098 #endif /* !Py_INTERNAL_PYMEM_H */