pymem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef Py_CPYTHON_PYMEM_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
  8. PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
  9. PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
  10. PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
  11. /* Try to get the allocators name set by _PyMem_SetupAllocators(). */
  12. PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
  13. PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
  14. /* strdup() using PyMem_RawMalloc() */
  15. PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
  16. /* strdup() using PyMem_Malloc() */
  17. PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
  18. /* wcsdup() using PyMem_RawMalloc() */
  19. PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
  20. typedef enum {
  21. /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
  22. PYMEM_DOMAIN_RAW,
  23. /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
  24. PYMEM_DOMAIN_MEM,
  25. /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
  26. PYMEM_DOMAIN_OBJ
  27. } PyMemAllocatorDomain;
  28. typedef enum {
  29. PYMEM_ALLOCATOR_NOT_SET = 0,
  30. PYMEM_ALLOCATOR_DEFAULT = 1,
  31. PYMEM_ALLOCATOR_DEBUG = 2,
  32. PYMEM_ALLOCATOR_MALLOC = 3,
  33. PYMEM_ALLOCATOR_MALLOC_DEBUG = 4,
  34. #ifdef WITH_PYMALLOC
  35. PYMEM_ALLOCATOR_PYMALLOC = 5,
  36. PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6,
  37. #endif
  38. } PyMemAllocatorName;
  39. typedef struct {
  40. /* user context passed as the first argument to the 4 functions */
  41. void *ctx;
  42. /* allocate a memory block */
  43. void* (*malloc) (void *ctx, size_t size);
  44. /* allocate a memory block initialized by zeros */
  45. void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
  46. /* allocate or resize a memory block */
  47. void* (*realloc) (void *ctx, void *ptr, size_t new_size);
  48. /* release a memory block */
  49. void (*free) (void *ctx, void *ptr);
  50. } PyMemAllocatorEx;
  51. /* Get the memory block allocator of the specified domain. */
  52. PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
  53. PyMemAllocatorEx *allocator);
  54. /* Set the memory block allocator of the specified domain.
  55. The new allocator must return a distinct non-NULL pointer when requesting
  56. zero bytes.
  57. For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
  58. is not held when the allocator is called.
  59. If the new allocator is not a hook (don't call the previous allocator), the
  60. PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
  61. on top on the new allocator. */
  62. PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
  63. PyMemAllocatorEx *allocator);
  64. /* Setup hooks to detect bugs in the following Python memory allocator
  65. functions:
  66. - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
  67. - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
  68. - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
  69. Newly allocated memory is filled with the byte 0xCB, freed memory is filled
  70. with the byte 0xDB. Additional checks:
  71. - detect API violations, ex: PyObject_Free() called on a buffer allocated
  72. by PyMem_Malloc()
  73. - detect write before the start of the buffer (buffer underflow)
  74. - detect write after the end of the buffer (buffer overflow)
  75. The function does nothing if Python is not compiled is debug mode. */
  76. PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
  77. #ifdef __cplusplus
  78. }
  79. #endif