pymem.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /* BEWARE:
  11. Each interface exports both functions and macros. Extension modules should
  12. use the functions, to ensure binary compatibility across Python versions.
  13. Because the Python implementation is free to change internal details, and
  14. the macros may (or may not) expose details for speed, if you do use the
  15. macros you must recompile your extensions with each Python release.
  16. Never mix calls to PyMem_ with calls to the platform malloc/realloc/
  17. calloc/free. For example, on Windows different DLLs may end up using
  18. different heaps, and if you use PyMem_Malloc you'll get the memory from the
  19. heap used by the Python DLL; it could be a disaster if you free()'ed that
  20. directly in your own extension. Using PyMem_Free instead ensures Python
  21. can return the memory to the proper heap. As another example, in
  22. PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
  23. memory functions in special debugging wrappers that add additional
  24. debugging info to dynamic memory blocks. The system routines have no idea
  25. what to do with that stuff, and the Python wrappers have no idea what to do
  26. with raw blocks obtained directly by the system routines then.
  27. The GIL must be held when using these APIs.
  28. */
  29. /*
  30. * Raw memory interface
  31. * ====================
  32. */
  33. /* Functions
  34. Functions supplying platform-independent semantics for malloc/realloc/
  35. free. These functions make sure that allocating 0 bytes returns a distinct
  36. non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
  37. may be returned), even if the platform malloc and realloc don't.
  38. Returned pointers must be checked for NULL explicitly. No action is
  39. performed on failure (no exception is set, no warning is printed, etc).
  40. */
  41. PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
  42. PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
  43. PyAPI_FUNC(void) PyMem_Free(void *ptr);
  44. /* Macros. */
  45. /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
  46. for malloc(0), which would be treated as an error. Some platforms
  47. would return a pointer with no memory behind it, which would break
  48. pymalloc. To solve these problems, allocate an extra byte. */
  49. /* Returns NULL to indicate error if a negative size or size larger than
  50. Py_ssize_t can represent is supplied. Helps prevents security holes. */
  51. #define PyMem_MALLOC(n) PyMem_Malloc(n)
  52. #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
  53. #define PyMem_FREE(p) PyMem_Free(p)
  54. /*
  55. * Type-oriented memory interface
  56. * ==============================
  57. *
  58. * Allocate memory for n objects of the given type. Returns a new pointer
  59. * or NULL if the request was too large or memory allocation failed. Use
  60. * these macros rather than doing the multiplication yourself so that proper
  61. * overflow checking is always done.
  62. */
  63. #define PyMem_New(type, n) \
  64. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  65. ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
  66. #define PyMem_NEW(type, n) \
  67. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  68. ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
  69. /*
  70. * The value of (p) is always clobbered by this macro regardless of success.
  71. * The caller MUST check if (p) is NULL afterwards and deal with the memory
  72. * error if so. This means the original value of (p) MUST be saved for the
  73. * caller's memory error handler to not lose track of it.
  74. */
  75. #define PyMem_Resize(p, type, n) \
  76. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  77. (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
  78. #define PyMem_RESIZE(p, type, n) \
  79. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  80. (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
  81. /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
  82. * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
  83. */
  84. #define PyMem_Del PyMem_Free
  85. #define PyMem_DEL PyMem_FREE
  86. #ifndef Py_LIMITED_API
  87. # define Py_CPYTHON_PYMEM_H
  88. # include "cpython/pymem.h"
  89. # undef Py_CPYTHON_PYMEM_H
  90. #endif
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif /* !Py_PYMEM_H */