Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.7 KiB

  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. */
  28. /*
  29. * Raw memory interface
  30. * ====================
  31. */
  32. /* Functions
  33. Functions supplying platform-independent semantics for malloc/realloc/
  34. free. These functions make sure that allocating 0 bytes returns a distinct
  35. non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
  36. may be returned), even if the platform malloc and realloc don't.
  37. Returned pointers must be checked for NULL explicitly. No action is
  38. performed on failure (no exception is set, no warning is printed, etc).
  39. */
  40. PyAPI_FUNC(void *) PyMem_Malloc(size_t);
  41. PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t);
  42. PyAPI_FUNC(void) PyMem_Free(void *);
  43. /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
  44. no longer supported. They used to call PyErr_NoMemory() on failure. */
  45. /* Macros. */
  46. #ifdef PYMALLOC_DEBUG
  47. /* Redirect all memory operations to Python's debugging allocator. */
  48. #define PyMem_MALLOC PyObject_MALLOC
  49. #define PyMem_REALLOC PyObject_REALLOC
  50. #define PyMem_FREE PyObject_FREE
  51. #else /* ! PYMALLOC_DEBUG */
  52. /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
  53. for malloc(0), which would be treated as an error. Some platforms
  54. would return a pointer with no memory behind it, which would break
  55. pymalloc. To solve these problems, allocate an extra byte. */
  56. #define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
  57. #define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
  58. #define PyMem_FREE free
  59. #endif /* PYMALLOC_DEBUG */
  60. /*
  61. * Type-oriented memory interface
  62. * ==============================
  63. *
  64. * These are carried along for historical reasons. There's rarely a good
  65. * reason to use them anymore (you can just as easily do the multiply and
  66. * cast yourself).
  67. */
  68. #define PyMem_New(type, n) \
  69. ( (type *) PyMem_Malloc((n) * sizeof(type)) )
  70. #define PyMem_NEW(type, n) \
  71. ( (type *) PyMem_MALLOC((n) * sizeof(type)) )
  72. #define PyMem_Resize(p, type, n) \
  73. ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
  74. #define PyMem_RESIZE(p, type, n) \
  75. ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
  76. /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
  77. * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
  78. */
  79. #define PyMem_Del PyMem_Free
  80. #define PyMem_DEL PyMem_FREE
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif /* !Py_PYMEM_H */