pycore_pymem.h 3.0 KB

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