objimpl.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* The PyObject_ memory family: high-level object memory interfaces.
  2. See pymem.h for the low-level PyMem_ family.
  3. */
  4. #ifndef Py_OBJIMPL_H
  5. #define Py_OBJIMPL_H
  6. #include "pymem.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 PyObject_ memory functions with calls to the platform
  17. malloc/realloc/ calloc/free, or with calls to PyMem_.
  18. */
  19. /*
  20. Functions and macros for modules that implement new object types.
  21. - PyObject_New(type, typeobj) allocates memory for a new object of the given
  22. type, and initializes part of it. 'type' must be the C structure type used
  23. to represent the object, and 'typeobj' the address of the corresponding
  24. type object. Reference count and type pointer are filled in; the rest of
  25. the bytes of the object are *undefined*! The resulting expression type is
  26. 'type *'. The size of the object is determined by the tp_basicsize field
  27. of the type object.
  28. - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
  29. object with room for n items. In addition to the refcount and type pointer
  30. fields, this also fills in the ob_size field.
  31. - PyObject_Del(op) releases the memory allocated for an object. It does not
  32. run a destructor -- it only frees the memory. PyObject_Free is identical.
  33. - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
  34. allocate memory. Instead of a 'type' parameter, they take a pointer to a
  35. new object (allocated by an arbitrary allocator), and initialize its object
  36. header fields.
  37. Note that objects created with PyObject_{New, NewVar} are allocated using the
  38. specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
  39. enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
  40. is also #defined.
  41. In case a specific form of memory management is needed (for example, if you
  42. must use the platform malloc heap(s), or shared memory, or C++ local storage or
  43. operator new), you must first allocate the object with your custom allocator,
  44. then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
  45. specific fields: reference count, type pointer, possibly others. You should
  46. be aware that Python has no control over these objects because they don't
  47. cooperate with the Python memory manager. Such objects may not be eligible
  48. for automatic garbage collection and you have to make sure that they are
  49. released accordingly whenever their destructor gets called (cf. the specific
  50. form of memory management you're using).
  51. Unless you have specific memory management requirements, use
  52. PyObject_{New, NewVar, Del}.
  53. */
  54. /*
  55. * Raw object memory interface
  56. * ===========================
  57. */
  58. /* Functions to call the same malloc/realloc/free as used by Python's
  59. object allocator. If WITH_PYMALLOC is enabled, these may differ from
  60. the platform malloc/realloc/free. The Python object allocator is
  61. designed for fast, cache-conscious allocation of many "small" objects,
  62. and with low hidden memory overhead.
  63. PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
  64. PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
  65. PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
  66. at p.
  67. Returned pointers must be checked for NULL explicitly; no action is
  68. performed on failure other than to return NULL (no warning it printed, no
  69. exception is set, etc).
  70. For allocating objects, use PyObject_{New, NewVar} instead whenever
  71. possible. The PyObject_{Malloc, Realloc, Free} family is exposed
  72. so that you can exploit Python's small-block allocator for non-object
  73. uses. If you must use these routines to allocate object memory, make sure
  74. the object gets initialized via PyObject_{Init, InitVar} after obtaining
  75. the raw memory.
  76. */
  77. PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
  78. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  79. PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
  80. #endif
  81. PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
  82. PyAPI_FUNC(void) PyObject_Free(void *ptr);
  83. /* Macros */
  84. #define PyObject_MALLOC PyObject_Malloc
  85. #define PyObject_REALLOC PyObject_Realloc
  86. #define PyObject_FREE PyObject_Free
  87. #define PyObject_Del PyObject_Free
  88. #define PyObject_DEL PyObject_Free
  89. /*
  90. * Generic object allocator interface
  91. * ==================================
  92. */
  93. /* Functions */
  94. PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  95. PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
  96. PyTypeObject *, Py_ssize_t);
  97. PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
  98. PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
  99. #define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
  100. // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
  101. // PyObject_MALLOC() with _PyObject_SIZE().
  102. #define PyObject_NEW(type, typeobj) PyObject_New(type, typeobj)
  103. #define PyObject_NewVar(type, typeobj, n) \
  104. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  105. // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
  106. // PyObject_MALLOC() with _PyObject_VAR_SIZE().
  107. #define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)
  108. #ifdef Py_LIMITED_API
  109. /* Define PyObject_INIT() and PyObject_INIT_VAR() as aliases to PyObject_Init()
  110. and PyObject_InitVar() in the limited C API for compatibility with the
  111. CPython C API. */
  112. # define PyObject_INIT(op, typeobj) \
  113. PyObject_Init(_PyObject_CAST(op), (typeobj))
  114. # define PyObject_INIT_VAR(op, typeobj, size) \
  115. PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
  116. #else
  117. /* PyObject_INIT() and PyObject_INIT_VAR() are defined in cpython/objimpl.h */
  118. #endif
  119. /*
  120. * Garbage Collection Support
  121. * ==========================
  122. */
  123. /* C equivalent of gc.collect() which ignores the state of gc.enabled. */
  124. PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
  125. /* Test if a type has a GC head */
  126. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  127. PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
  128. #define PyObject_GC_Resize(type, op, n) \
  129. ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
  130. PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
  131. PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
  132. /* Tell the GC to track this object.
  133. *
  134. * See also private _PyObject_GC_TRACK() macro. */
  135. PyAPI_FUNC(void) PyObject_GC_Track(void *);
  136. /* Tell the GC to stop tracking this object.
  137. *
  138. * See also private _PyObject_GC_UNTRACK() macro. */
  139. PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
  140. PyAPI_FUNC(void) PyObject_GC_Del(void *);
  141. #define PyObject_GC_New(type, typeobj) \
  142. ( (type *) _PyObject_GC_New(typeobj) )
  143. #define PyObject_GC_NewVar(type, typeobj, n) \
  144. ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
  145. PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
  146. PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
  147. /* Utility macro to help write tp_traverse functions.
  148. * To use this macro, the tp_traverse function must name its arguments
  149. * "visit" and "arg". This is intended to keep tp_traverse functions
  150. * looking as much alike as possible.
  151. */
  152. #define Py_VISIT(op) \
  153. do { \
  154. if (op) { \
  155. int vret = visit(_PyObject_CAST(op), arg); \
  156. if (vret) \
  157. return vret; \
  158. } \
  159. } while (0)
  160. #ifndef Py_LIMITED_API
  161. # define Py_CPYTHON_OBJIMPL_H
  162. # include "cpython/objimpl.h"
  163. # undef Py_CPYTHON_OBJIMPL_H
  164. #endif
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* !Py_OBJIMPL_H */