pymem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. a debug build (Py_DEBUG macro), Python wraps all calls to all PyMem_ and
  23. PyObject_ 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_Calloc(size_t nelem, size_t elsize);
  43. PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
  44. PyAPI_FUNC(void) PyMem_Free(void *ptr);
  45. /*
  46. * Type-oriented memory interface
  47. * ==============================
  48. *
  49. * Allocate memory for n objects of the given type. Returns a new pointer
  50. * or NULL if the request was too large or memory allocation failed. Use
  51. * these macros rather than doing the multiplication yourself so that proper
  52. * overflow checking is always done.
  53. */
  54. #define PyMem_New(type, n) \
  55. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  56. ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
  57. /*
  58. * The value of (p) is always clobbered by this macro regardless of success.
  59. * The caller MUST check if (p) is NULL afterwards and deal with the memory
  60. * error if so. This means the original value of (p) MUST be saved for the
  61. * caller's memory error handler to not lose track of it.
  62. */
  63. #define PyMem_Resize(p, type, n) \
  64. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  65. (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
  66. // Deprecated aliases only kept for backward compatibility.
  67. // PyMem_Del and PyMem_DEL are defined with no parameter to be able to use
  68. // them as function pointers (ex: dealloc = PyMem_Del).
  69. #define PyMem_MALLOC(n) PyMem_Malloc((n))
  70. #define PyMem_NEW(type, n) PyMem_New(type, (n))
  71. #define PyMem_REALLOC(p, n) PyMem_Realloc((p), (n))
  72. #define PyMem_RESIZE(p, type, n) PyMem_Resize((p), type, (n))
  73. #define PyMem_FREE(p) PyMem_Free((p))
  74. #define PyMem_Del(p) PyMem_Free((p))
  75. #define PyMem_DEL(p) PyMem_Free((p))
  76. #ifndef Py_LIMITED_API
  77. # define Py_CPYTHON_PYMEM_H
  78. # include "cpython/pymem.h"
  79. # undef Py_CPYTHON_PYMEM_H
  80. #endif
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif /* !Py_PYMEM_H */