pymem.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* The PyMem_ family: low-level memory allocation interfaces.
  2. See objimpl.h for the PyObject_ memory family.
  3. */
  4. #ifndef Py_PYMEM_H
  5. #define Py_PYMEM_H
  6. #include "pyport.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifndef Py_LIMITED_API
  11. PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
  12. PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
  13. PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
  14. PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
  15. #endif
  16. /* BEWARE:
  17. Each interface exports both functions and macros. Extension modules should
  18. use the functions, to ensure binary compatibility across Python versions.
  19. Because the Python implementation is free to change internal details, and
  20. the macros may (or may not) expose details for speed, if you do use the
  21. macros you must recompile your extensions with each Python release.
  22. Never mix calls to PyMem_ with calls to the platform malloc/realloc/
  23. calloc/free. For example, on Windows different DLLs may end up using
  24. different heaps, and if you use PyMem_Malloc you'll get the memory from the
  25. heap used by the Python DLL; it could be a disaster if you free()'ed that
  26. directly in your own extension. Using PyMem_Free instead ensures Python
  27. can return the memory to the proper heap. As another example, in
  28. PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
  29. memory functions in special debugging wrappers that add additional
  30. debugging info to dynamic memory blocks. The system routines have no idea
  31. what to do with that stuff, and the Python wrappers have no idea what to do
  32. with raw blocks obtained directly by the system routines then.
  33. The GIL must be held when using these APIs.
  34. */
  35. /*
  36. * Raw memory interface
  37. * ====================
  38. */
  39. /* Functions
  40. Functions supplying platform-independent semantics for malloc/realloc/
  41. free. These functions make sure that allocating 0 bytes returns a distinct
  42. non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
  43. may be returned), even if the platform malloc and realloc don't.
  44. Returned pointers must be checked for NULL explicitly. No action is
  45. performed on failure (no exception is set, no warning is printed, etc).
  46. */
  47. PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
  48. PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
  49. PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
  50. PyAPI_FUNC(void) PyMem_Free(void *ptr);
  51. #ifndef Py_LIMITED_API
  52. PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
  53. PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
  54. #endif
  55. /* Macros. */
  56. /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
  57. for malloc(0), which would be treated as an error. Some platforms
  58. would return a pointer with no memory behind it, which would break
  59. pymalloc. To solve these problems, allocate an extra byte. */
  60. /* Returns NULL to indicate error if a negative size or size larger than
  61. Py_ssize_t can represent is supplied. Helps prevents security holes. */
  62. #define PyMem_MALLOC(n) PyMem_Malloc(n)
  63. #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
  64. #define PyMem_FREE(p) PyMem_Free(p)
  65. /*
  66. * Type-oriented memory interface
  67. * ==============================
  68. *
  69. * Allocate memory for n objects of the given type. Returns a new pointer
  70. * or NULL if the request was too large or memory allocation failed. Use
  71. * these macros rather than doing the multiplication yourself so that proper
  72. * overflow checking is always done.
  73. */
  74. #define PyMem_New(type, n) \
  75. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  76. ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
  77. #define PyMem_NEW(type, n) \
  78. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  79. ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
  80. /*
  81. * The value of (p) is always clobbered by this macro regardless of success.
  82. * The caller MUST check if (p) is NULL afterwards and deal with the memory
  83. * error if so. This means the original value of (p) MUST be saved for the
  84. * caller's memory error handler to not lose track of it.
  85. */
  86. #define PyMem_Resize(p, type, n) \
  87. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  88. (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
  89. #define PyMem_RESIZE(p, type, n) \
  90. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  91. (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
  92. /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
  93. * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
  94. */
  95. #define PyMem_Del PyMem_Free
  96. #define PyMem_DEL PyMem_FREE
  97. #ifndef Py_LIMITED_API
  98. typedef enum {
  99. /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
  100. PYMEM_DOMAIN_RAW,
  101. /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
  102. PYMEM_DOMAIN_MEM,
  103. /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
  104. PYMEM_DOMAIN_OBJ
  105. } PyMemAllocatorDomain;
  106. typedef struct {
  107. /* user context passed as the first argument to the 4 functions */
  108. void *ctx;
  109. /* allocate a memory block */
  110. void* (*malloc) (void *ctx, size_t size);
  111. /* allocate a memory block initialized by zeros */
  112. void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
  113. /* allocate or resize a memory block */
  114. void* (*realloc) (void *ctx, void *ptr, size_t new_size);
  115. /* release a memory block */
  116. void (*free) (void *ctx, void *ptr);
  117. } PyMemAllocatorEx;
  118. /* Get the memory block allocator of the specified domain. */
  119. PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
  120. PyMemAllocatorEx *allocator);
  121. /* Set the memory block allocator of the specified domain.
  122. The new allocator must return a distinct non-NULL pointer when requesting
  123. zero bytes.
  124. For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
  125. is not held when the allocator is called.
  126. If the new allocator is not a hook (don't call the previous allocator), the
  127. PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
  128. on top on the new allocator. */
  129. PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
  130. PyMemAllocatorEx *allocator);
  131. /* Setup hooks to detect bugs in the following Python memory allocator
  132. functions:
  133. - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
  134. - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
  135. - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
  136. Newly allocated memory is filled with the byte 0xCB, freed memory is filled
  137. with the byte 0xDB. Additionnal checks:
  138. - detect API violations, ex: PyObject_Free() called on a buffer allocated
  139. by PyMem_Malloc()
  140. - detect write before the start of the buffer (buffer underflow)
  141. - detect write after the end of the buffer (buffer overflow)
  142. The function does nothing if Python is not compiled is debug mode. */
  143. PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
  144. #endif
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif /* !Py_PYMEM_H */