/Doc/c-api/gcsupport.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 165 lines · 107 code · 58 blank · 0 comment · 0 complexity · 6668faf9ea75f917c8b802b4ae0d56b9 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _supporting-cycle-detection:
  3. Supporting Cyclic Garbage Collection
  4. ====================================
  5. Python's support for detecting and collecting garbage which involves circular
  6. references requires support from object types which are "containers" for other
  7. objects which may also be containers. Types which do not store references to
  8. other objects, or which only store references to atomic types (such as numbers
  9. or strings), do not need to provide any explicit support for garbage
  10. collection.
  11. .. An example showing the use of these interfaces can be found in "Supporting the
  12. .. Cycle Collector (XXX not found: ../ext/example-cycle-support.html)".
  13. To create a container type, the :attr:`tp_flags` field of the type object must
  14. include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
  15. :attr:`tp_traverse` handler. If instances of the type are mutable, a
  16. :attr:`tp_clear` implementation must also be provided.
  17. .. data:: Py_TPFLAGS_HAVE_GC
  18. :noindex:
  19. Objects with a type with this flag set must conform with the rules
  20. documented here. For convenience these objects will be referred to as
  21. container objects.
  22. Constructors for container types must conform to two rules:
  23. #. The memory for the object must be allocated using :cfunc:`PyObject_GC_New`
  24. or :cfunc:`PyObject_GC_VarNew`.
  25. #. Once all the fields which may contain references to other containers are
  26. initialized, it must call :cfunc:`PyObject_GC_Track`.
  27. .. cfunction:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
  28. Analogous to :cfunc:`PyObject_New` but for container objects with the
  29. :const:`Py_TPFLAGS_HAVE_GC` flag set.
  30. .. cfunction:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
  31. Analogous to :cfunc:`PyObject_NewVar` but for container objects with the
  32. :const:`Py_TPFLAGS_HAVE_GC` flag set.
  33. .. versionchanged:: 2.5
  34. This function used an :ctype:`int` type for *size*. This might require
  35. changes in your code for properly supporting 64-bit systems.
  36. .. cfunction:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
  37. Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the
  38. resized object or *NULL* on failure.
  39. .. versionchanged:: 2.5
  40. This function used an :ctype:`int` type for *newsize*. This might
  41. require changes in your code for properly supporting 64-bit systems.
  42. .. cfunction:: void PyObject_GC_Track(PyObject *op)
  43. Adds the object *op* to the set of container objects tracked by the
  44. collector. The collector can run at unexpected times so objects must be
  45. valid while being tracked. This should be called once all the fields
  46. followed by the :attr:`tp_traverse` handler become valid, usually near the
  47. end of the constructor.
  48. .. cfunction:: void _PyObject_GC_TRACK(PyObject *op)
  49. A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for
  50. extension modules.
  51. Similarly, the deallocator for the object must conform to a similar pair of
  52. rules:
  53. #. Before fields which refer to other containers are invalidated,
  54. :cfunc:`PyObject_GC_UnTrack` must be called.
  55. #. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`.
  56. .. cfunction:: void PyObject_GC_Del(void *op)
  57. Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or
  58. :cfunc:`PyObject_GC_NewVar`.
  59. .. cfunction:: void PyObject_GC_UnTrack(void *op)
  60. Remove the object *op* from the set of container objects tracked by the
  61. collector. Note that :cfunc:`PyObject_GC_Track` can be called again on
  62. this object to add it back to the set of tracked objects. The deallocator
  63. (:attr:`tp_dealloc` handler) should call this for the object before any of
  64. the fields used by the :attr:`tp_traverse` handler become invalid.
  65. .. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op)
  66. A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for
  67. extension modules.
  68. The :attr:`tp_traverse` handler accepts a function parameter of this type:
  69. .. ctype:: int (*visitproc)(PyObject *object, void *arg)
  70. Type of the visitor function passed to the :attr:`tp_traverse` handler.
  71. The function should be called with an object to traverse as *object* and
  72. the third parameter to the :attr:`tp_traverse` handler as *arg*. The
  73. Python core uses several visitor functions to implement cyclic garbage
  74. detection; it's not expected that users will need to write their own
  75. visitor functions.
  76. The :attr:`tp_traverse` handler must have the following type:
  77. .. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
  78. Traversal function for a container object. Implementations must call the
  79. *visit* function for each object directly contained by *self*, with the
  80. parameters to *visit* being the contained object and the *arg* value passed
  81. to the handler. The *visit* function must not be called with a *NULL*
  82. object argument. If *visit* returns a non-zero value that value should be
  83. returned immediately.
  84. To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is
  85. provided. In order to use this macro, the :attr:`tp_traverse` implementation
  86. must name its arguments exactly *visit* and *arg*:
  87. .. cfunction:: void Py_VISIT(PyObject *o)
  88. Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns
  89. a non-zero value, then return it. Using this macro, :attr:`tp_traverse`
  90. handlers look like::
  91. static int
  92. my_traverse(Noddy *self, visitproc visit, void *arg)
  93. {
  94. Py_VISIT(self->foo);
  95. Py_VISIT(self->bar);
  96. return 0;
  97. }
  98. .. versionadded:: 2.4
  99. The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL*
  100. if the object is immutable.
  101. .. ctype:: int (*inquiry)(PyObject *self)
  102. Drop references that may have created reference cycles. Immutable objects
  103. do not have to define this method since they can never directly create
  104. reference cycles. Note that the object must still be valid after calling
  105. this method (don't just call :cfunc:`Py_DECREF` on a reference). The
  106. collector will call this method if it detects that this object is involved
  107. in a reference cycle.