pymem.h 3.3 KB

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