(root)/
Python-3.12.0/
Include/
internal/
pycore_pymem.h
       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  typedef struct {
      15      /* We tag each block with an API ID in order to tag API violations */
      16      char api_id;
      17      PyMemAllocatorEx alloc;
      18  } debug_alloc_api_t;
      19  
      20  struct _pymem_allocators {
      21      PyThread_type_lock mutex;
      22      struct {
      23          PyMemAllocatorEx raw;
      24          PyMemAllocatorEx mem;
      25          PyMemAllocatorEx obj;
      26      } standard;
      27      struct {
      28          debug_alloc_api_t raw;
      29          debug_alloc_api_t mem;
      30          debug_alloc_api_t obj;
      31      } debug;
      32      PyObjectArenaAllocator obj_arena;
      33  };
      34  
      35  
      36  /* Set the memory allocator of the specified domain to the default.
      37     Save the old allocator into *old_alloc if it's non-NULL.
      38     Return on success, or return -1 if the domain is unknown. */
      39  PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
      40      PyMemAllocatorDomain domain,
      41      PyMemAllocatorEx *old_alloc);
      42  
      43  /* Special bytes broadcast into debug memory blocks at appropriate times.
      44     Strings of these are unlikely to be valid addresses, floats, ints or
      45     7-bit ASCII.
      46  
      47     - PYMEM_CLEANBYTE: clean (newly allocated) memory
      48     - PYMEM_DEADBYTE dead (newly freed) memory
      49     - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block
      50  
      51     Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and
      52     0xFD to use the same values than Windows CRT debug malloc() and free().
      53     If modified, _PyMem_IsPtrFreed() should be updated as well. */
      54  #define PYMEM_CLEANBYTE      0xCD
      55  #define PYMEM_DEADBYTE       0xDD
      56  #define PYMEM_FORBIDDENBYTE  0xFD
      57  
      58  /* Heuristic checking if a pointer value is newly allocated
      59     (uninitialized), newly freed or NULL (is equal to zero).
      60  
      61     The pointer is not dereferenced, only the pointer value is checked.
      62  
      63     The heuristic relies on the debug hooks on Python memory allocators which
      64     fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory
      65     with DEADBYTE (0xDD). Detect also "untouchable bytes" marked
      66     with FORBIDDENBYTE (0xFD). */
      67  static inline int _PyMem_IsPtrFreed(const void *ptr)
      68  {
      69      uintptr_t value = (uintptr_t)ptr;
      70  #if SIZEOF_VOID_P == 8
      71      return (value == 0
      72              || value == (uintptr_t)0xCDCDCDCDCDCDCDCD
      73              || value == (uintptr_t)0xDDDDDDDDDDDDDDDD
      74              || value == (uintptr_t)0xFDFDFDFDFDFDFDFD);
      75  #elif SIZEOF_VOID_P == 4
      76      return (value == 0
      77              || value == (uintptr_t)0xCDCDCDCD
      78              || value == (uintptr_t)0xDDDDDDDD
      79              || value == (uintptr_t)0xFDFDFDFD);
      80  #else
      81  #  error "unknown pointer size"
      82  #endif
      83  }
      84  
      85  PyAPI_FUNC(int) _PyMem_GetAllocatorName(
      86      const char *name,
      87      PyMemAllocatorName *allocator);
      88  
      89  /* Configure the Python memory allocators.
      90     Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators.
      91     PYMEM_ALLOCATOR_NOT_SET does nothing. */
      92  PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
      93  
      94  
      95  #ifdef __cplusplus
      96  }
      97  #endif
      98  #endif /* !Py_INTERNAL_PYMEM_H */