pymem.h 8.8 KB

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