blob: b0a2d5c08dddc37cca94843479f6593d75d1a6b2 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _supporting-cycle-detection:
4
5Supporting Cyclic Garbage Collection
6====================================
7
8Python's support for detecting and collecting garbage which involves circular
9references requires support from object types which are "containers" for other
10objects which may also be containers. Types which do not store references to
11other objects, or which only store references to atomic types (such as numbers
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000012or strings), do not need to provide any explicit support for garbage
13collection.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15.. An example showing the use of these interfaces can be found in "Supporting the
16.. Cycle Collector (XXX not found: ../ext/example-cycle-support.html)".
17
Antoine Pitrou92fae552013-08-01 21:17:24 +020018To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
Georg Brandlf6842722008-01-19 22:08:21 +000019include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
Antoine Pitrou92fae552013-08-01 21:17:24 +020020:c:member:`~PyTypeObject.tp_traverse` handler. If instances of the type are mutable, a
21:c:member:`~PyTypeObject.tp_clear` implementation must also be provided.
Georg Brandlf6842722008-01-19 22:08:21 +000022
23
24.. data:: Py_TPFLAGS_HAVE_GC
25 :noindex:
26
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000027 Objects with a type with this flag set must conform with the rules
28 documented here. For convenience these objects will be referred to as
29 container objects.
Georg Brandlf6842722008-01-19 22:08:21 +000030
31Constructors for container types must conform to two rules:
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033#. The memory for the object must be allocated using :c:func:`PyObject_GC_New`
34 or :c:func:`PyObject_GC_NewVar`.
Georg Brandlf6842722008-01-19 22:08:21 +000035
36#. Once all the fields which may contain references to other containers are
Sandro Tosi98ed08f2012-01-14 16:42:02 +010037 initialized, it must call :c:func:`PyObject_GC_Track`.
Georg Brandlf6842722008-01-19 22:08:21 +000038
39
Sandro Tosi98ed08f2012-01-14 16:42:02 +010040.. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
Georg Brandlf6842722008-01-19 22:08:21 +000041
Sandro Tosi98ed08f2012-01-14 16:42:02 +010042 Analogous to :c:func:`PyObject_New` but for container objects with the
Georg Brandlf6842722008-01-19 22:08:21 +000043 :const:`Py_TPFLAGS_HAVE_GC` flag set.
44
45
Sandro Tosi98ed08f2012-01-14 16:42:02 +010046.. c:function:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +000047
Sandro Tosi98ed08f2012-01-14 16:42:02 +010048 Analogous to :c:func:`PyObject_NewVar` but for container objects with the
Georg Brandlf6842722008-01-19 22:08:21 +000049 :const:`Py_TPFLAGS_HAVE_GC` flag set.
50
Jeroen Ruigrok van der Werven691f5f12009-04-25 19:44:55 +000051 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010052 This function used an :c:type:`int` type for *size*. This might require
Jeroen Ruigrok van der Werven691f5f12009-04-25 19:44:55 +000053 changes in your code for properly supporting 64-bit systems.
Georg Brandlf6842722008-01-19 22:08:21 +000054
Jeroen Ruigrok van der Werven691f5f12009-04-25 19:44:55 +000055
Sandro Tosi98ed08f2012-01-14 16:42:02 +010056.. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
Georg Brandlf6842722008-01-19 22:08:21 +000057
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058 Resize an object allocated by :c:func:`PyObject_NewVar`. Returns the
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000059 resized object or *NULL* on failure.
Georg Brandlf6842722008-01-19 22:08:21 +000060
Jeroen Ruigrok van der Werven691f5f12009-04-25 19:44:55 +000061 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010062 This function used an :c:type:`int` type for *newsize*. This might
Jeroen Ruigrok van der Werven691f5f12009-04-25 19:44:55 +000063 require changes in your code for properly supporting 64-bit systems.
64
Georg Brandlf6842722008-01-19 22:08:21 +000065
Sandro Tosi98ed08f2012-01-14 16:42:02 +010066.. c:function:: void PyObject_GC_Track(PyObject *op)
Georg Brandlf6842722008-01-19 22:08:21 +000067
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000068 Adds the object *op* to the set of container objects tracked by the
69 collector. The collector can run at unexpected times so objects must be
70 valid while being tracked. This should be called once all the fields
Antoine Pitrou92fae552013-08-01 21:17:24 +020071 followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000072 end of the constructor.
Georg Brandlf6842722008-01-19 22:08:21 +000073
74
Sandro Tosi98ed08f2012-01-14 16:42:02 +010075.. c:function:: void _PyObject_GC_TRACK(PyObject *op)
Georg Brandlf6842722008-01-19 22:08:21 +000076
Sandro Tosi98ed08f2012-01-14 16:42:02 +010077 A macro version of :c:func:`PyObject_GC_Track`. It should not be used for
Georg Brandlf6842722008-01-19 22:08:21 +000078 extension modules.
79
80Similarly, the deallocator for the object must conform to a similar pair of
81rules:
82
83#. Before fields which refer to other containers are invalidated,
Sandro Tosi98ed08f2012-01-14 16:42:02 +010084 :c:func:`PyObject_GC_UnTrack` must be called.
Georg Brandlf6842722008-01-19 22:08:21 +000085
Sandro Tosi98ed08f2012-01-14 16:42:02 +010086#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`.
Georg Brandlf6842722008-01-19 22:08:21 +000087
88
Sandro Tosi98ed08f2012-01-14 16:42:02 +010089.. c:function:: void PyObject_GC_Del(void *op)
Georg Brandlf6842722008-01-19 22:08:21 +000090
Sandro Tosi98ed08f2012-01-14 16:42:02 +010091 Releases memory allocated to an object using :c:func:`PyObject_GC_New` or
92 :c:func:`PyObject_GC_NewVar`.
Georg Brandlf6842722008-01-19 22:08:21 +000093
94
Sandro Tosi98ed08f2012-01-14 16:42:02 +010095.. c:function:: void PyObject_GC_UnTrack(void *op)
Georg Brandlf6842722008-01-19 22:08:21 +000096
97 Remove the object *op* from the set of container objects tracked by the
Sandro Tosi98ed08f2012-01-14 16:42:02 +010098 collector. Note that :c:func:`PyObject_GC_Track` can be called again on
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000099 this object to add it back to the set of tracked objects. The deallocator
Antoine Pitrou92fae552013-08-01 21:17:24 +0200100 (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of
101 the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
Georg Brandlf6842722008-01-19 22:08:21 +0000102
103
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100104.. c:function:: void _PyObject_GC_UNTRACK(PyObject *op)
Georg Brandlf6842722008-01-19 22:08:21 +0000105
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100106 A macro version of :c:func:`PyObject_GC_UnTrack`. It should not be used for
Georg Brandlf6842722008-01-19 22:08:21 +0000107 extension modules.
108
Antoine Pitrou92fae552013-08-01 21:17:24 +0200109The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
Georg Brandlf6842722008-01-19 22:08:21 +0000110
111
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100112.. c:type:: int (*visitproc)(PyObject *object, void *arg)
Georg Brandlf6842722008-01-19 22:08:21 +0000113
Antoine Pitrou92fae552013-08-01 21:17:24 +0200114 Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler.
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000115 The function should be called with an object to traverse as *object* and
Antoine Pitrou92fae552013-08-01 21:17:24 +0200116 the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*. The
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000117 Python core uses several visitor functions to implement cyclic garbage
118 detection; it's not expected that users will need to write their own
119 visitor functions.
Georg Brandlf6842722008-01-19 22:08:21 +0000120
Antoine Pitrou92fae552013-08-01 21:17:24 +0200121The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
Georg Brandlf6842722008-01-19 22:08:21 +0000122
123
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100124.. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
Georg Brandlf6842722008-01-19 22:08:21 +0000125
126 Traversal function for a container object. Implementations must call the
127 *visit* function for each object directly contained by *self*, with the
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000128 parameters to *visit* being the contained object and the *arg* value passed
129 to the handler. The *visit* function must not be called with a *NULL*
130 object argument. If *visit* returns a non-zero value that value should be
131 returned immediately.
Georg Brandlf6842722008-01-19 22:08:21 +0000132
Antoine Pitrou92fae552013-08-01 21:17:24 +0200133To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
134provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
Georg Brandlf6842722008-01-19 22:08:21 +0000135must name its arguments exactly *visit* and *arg*:
136
137
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100138.. c:function:: void Py_VISIT(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000139
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000140 Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns
Antoine Pitrou92fae552013-08-01 21:17:24 +0200141 a non-zero value, then return it. Using this macro, :c:member:`~PyTypeObject.tp_traverse`
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000142 handlers look like::
Georg Brandlf6842722008-01-19 22:08:21 +0000143
144 static int
145 my_traverse(Noddy *self, visitproc visit, void *arg)
146 {
147 Py_VISIT(self->foo);
148 Py_VISIT(self->bar);
149 return 0;
150 }
151
152 .. versionadded:: 2.4
153
Antoine Pitrou92fae552013-08-01 21:17:24 +0200154The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or *NULL*
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000155if the object is immutable.
Georg Brandlf6842722008-01-19 22:08:21 +0000156
157
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100158.. c:type:: int (*inquiry)(PyObject *self)
Georg Brandlf6842722008-01-19 22:08:21 +0000159
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000160 Drop references that may have created reference cycles. Immutable objects
161 do not have to define this method since they can never directly create
162 reference cycles. Note that the object must still be valid after calling
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100163 this method (don't just call :c:func:`Py_DECREF` on a reference). The
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000164 collector will call this method if it detects that this object is involved
165 in a reference cycle.