objimpl.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_Free(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 Py_DEBUG
  40. macro 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. // Deprecated aliases only kept for backward compatibility.
  84. // PyObject_Del and PyObject_DEL are defined with no parameter to be able to
  85. // use them as function pointers (ex: tp_free = PyObject_Del).
  86. #define PyObject_MALLOC PyObject_Malloc
  87. #define PyObject_REALLOC PyObject_Realloc
  88. #define PyObject_FREE PyObject_Free
  89. #define PyObject_Del PyObject_Free
  90. #define PyObject_DEL PyObject_Free
  91. /*
  92. * Generic object allocator interface
  93. * ==================================
  94. */
  95. /* Functions */
  96. PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  97. PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
  98. PyTypeObject *, Py_ssize_t);
  99. #define PyObject_INIT(op, typeobj) \
  100. PyObject_Init(_PyObject_CAST(op), (typeobj))
  101. #define PyObject_INIT_VAR(op, typeobj, size) \
  102. PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
  103. PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
  104. PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
  105. #define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
  106. // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
  107. // PyObject_MALLOC() with _PyObject_SIZE().
  108. #define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))
  109. #define PyObject_NewVar(type, typeobj, n) \
  110. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  111. // Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
  112. // directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
  113. #define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))
  114. /*
  115. * Garbage Collection Support
  116. * ==========================
  117. */
  118. /* C equivalent of gc.collect(). */
  119. PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
  120. /* C API for controlling the state of the garbage collector */
  121. PyAPI_FUNC(int) PyGC_Enable(void);
  122. PyAPI_FUNC(int) PyGC_Disable(void);
  123. PyAPI_FUNC(int) PyGC_IsEnabled(void);
  124. #if !defined(Py_LIMITED_API)
  125. /* Visit all live GC-capable objects, similar to gc.get_objects(None). The
  126. * supplied callback is called on every such object with the void* arg set
  127. * to the supplied arg. Returning 0 from the callback ends iteration, returning
  128. * 1 allows iteration to continue. Returning any other value may result in
  129. * undefined behaviour.
  130. *
  131. * If new objects are (de)allocated by the callback it is undefined if they
  132. * will be visited.
  133. * Garbage collection is disabled during operation. Explicitly running a
  134. * collection in the callback may lead to undefined behaviour e.g. visiting the
  135. * same objects multiple times or not at all.
  136. */
  137. typedef int (*gcvisitobjects_t)(PyObject*, void*);
  138. PyAPI_FUNC(void) PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void* arg);
  139. #endif
  140. /* Test if a type has a GC head */
  141. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  142. PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
  143. #define PyObject_GC_Resize(type, op, n) \
  144. ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
  145. PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
  146. PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
  147. /* Tell the GC to track this object.
  148. *
  149. * See also private _PyObject_GC_TRACK() macro. */
  150. PyAPI_FUNC(void) PyObject_GC_Track(void *);
  151. /* Tell the GC to stop tracking this object.
  152. *
  153. * See also private _PyObject_GC_UNTRACK() macro. */
  154. PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
  155. PyAPI_FUNC(void) PyObject_GC_Del(void *);
  156. #define PyObject_GC_New(type, typeobj) \
  157. _Py_CAST(type*, _PyObject_GC_New(typeobj))
  158. #define PyObject_GC_NewVar(type, typeobj, n) \
  159. _Py_CAST(type*, _PyObject_GC_NewVar((typeobj), (n)))
  160. PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
  161. PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
  162. /* Utility macro to help write tp_traverse functions.
  163. * To use this macro, the tp_traverse function must name its arguments
  164. * "visit" and "arg". This is intended to keep tp_traverse functions
  165. * looking as much alike as possible.
  166. */
  167. #define Py_VISIT(op) \
  168. do { \
  169. if (op) { \
  170. int vret = visit(_PyObject_CAST(op), arg); \
  171. if (vret) \
  172. return vret; \
  173. } \
  174. } while (0)
  175. #ifndef Py_LIMITED_API
  176. # define Py_CPYTHON_OBJIMPL_H
  177. # include "cpython/objimpl.h"
  178. # undef Py_CPYTHON_OBJIMPL_H
  179. #endif
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif /* !Py_OBJIMPL_H */