blob: cc5fd5c546809ec23a4125d92f277815905d6036 [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
18To create a container type, the :attr:`tp_flags` field of the type object must
19include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
20:attr:`tp_traverse` handler. If instances of the type are mutable, a
21:attr:`tp_clear` implementation must also be provided.
22
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
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000033#. The memory for the object must be allocated using :cfunc:`PyObject_GC_New`
34 or :cfunc:`PyObject_GC_VarNew`.
Georg Brandlf6842722008-01-19 22:08:21 +000035
36#. Once all the fields which may contain references to other containers are
37 initialized, it must call :cfunc:`PyObject_GC_Track`.
38
39
40.. cfunction:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
41
42 Analogous to :cfunc:`PyObject_New` but for container objects with the
43 :const:`Py_TPFLAGS_HAVE_GC` flag set.
44
45
46.. cfunction:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
47
48 Analogous to :cfunc:`PyObject_NewVar` but for container objects with the
49 :const:`Py_TPFLAGS_HAVE_GC` flag set.
50
51
52.. cfunction:: PyVarObject * PyObject_GC_Resize(PyVarObject *op, Py_ssize_t)
53
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000054 Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the
55 resized object or *NULL* on failure.
Georg Brandlf6842722008-01-19 22:08:21 +000056
57
58.. cfunction:: void PyObject_GC_Track(PyObject *op)
59
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000060 Adds the object *op* to the set of container objects tracked by the
61 collector. The collector can run at unexpected times so objects must be
62 valid while being tracked. This should be called once all the fields
63 followed by the :attr:`tp_traverse` handler become valid, usually near the
64 end of the constructor.
Georg Brandlf6842722008-01-19 22:08:21 +000065
66
67.. cfunction:: void _PyObject_GC_TRACK(PyObject *op)
68
69 A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for
70 extension modules.
71
72Similarly, the deallocator for the object must conform to a similar pair of
73rules:
74
75#. Before fields which refer to other containers are invalidated,
76 :cfunc:`PyObject_GC_UnTrack` must be called.
77
78#. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`.
79
80
81.. cfunction:: void PyObject_GC_Del(void *op)
82
83 Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or
84 :cfunc:`PyObject_GC_NewVar`.
85
86
87.. cfunction:: void PyObject_GC_UnTrack(void *op)
88
89 Remove the object *op* from the set of container objects tracked by the
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +000090 collector. Note that :cfunc:`PyObject_GC_Track` can be called again on
91 this object to add it back to the set of tracked objects. The deallocator
92 (:attr:`tp_dealloc` handler) should call this for the object before any of
93 the fields used by the :attr:`tp_traverse` handler become invalid.
Georg Brandlf6842722008-01-19 22:08:21 +000094
95
96.. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op)
97
98 A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for
99 extension modules.
100
101The :attr:`tp_traverse` handler accepts a function parameter of this type:
102
103
104.. ctype:: int (*visitproc)(PyObject *object, void *arg)
105
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000106 Type of the visitor function passed to the :attr:`tp_traverse` handler.
107 The function should be called with an object to traverse as *object* and
108 the third parameter to the :attr:`tp_traverse` handler as *arg*. The
109 Python core uses several visitor functions to implement cyclic garbage
110 detection; it's not expected that users will need to write their own
111 visitor functions.
Georg Brandlf6842722008-01-19 22:08:21 +0000112
113The :attr:`tp_traverse` handler must have the following type:
114
115
116.. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
117
118 Traversal function for a container object. Implementations must call the
119 *visit* function for each object directly contained by *self*, with the
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000120 parameters to *visit* being the contained object and the *arg* value passed
121 to the handler. The *visit* function must not be called with a *NULL*
122 object argument. If *visit* returns a non-zero value that value should be
123 returned immediately.
Georg Brandlf6842722008-01-19 22:08:21 +0000124
125To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is
126provided. In order to use this macro, the :attr:`tp_traverse` implementation
127must name its arguments exactly *visit* and *arg*:
128
129
130.. cfunction:: void Py_VISIT(PyObject *o)
131
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000132 Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns
133 a non-zero value, then return it. Using this macro, :attr:`tp_traverse`
134 handlers look like::
Georg Brandlf6842722008-01-19 22:08:21 +0000135
136 static int
137 my_traverse(Noddy *self, visitproc visit, void *arg)
138 {
139 Py_VISIT(self->foo);
140 Py_VISIT(self->bar);
141 return 0;
142 }
143
144 .. versionadded:: 2.4
145
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000146The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL*
147if the object is immutable.
Georg Brandlf6842722008-01-19 22:08:21 +0000148
149
150.. ctype:: int (*inquiry)(PyObject *self)
151
Jeroen Ruigrok van der Werven35371242009-04-25 19:10:52 +0000152 Drop references that may have created reference cycles. Immutable objects
153 do not have to define this method since they can never directly create
154 reference cycles. Note that the object must still be valid after calling
155 this method (don't just call :cfunc:`Py_DECREF` on a reference). The
156 collector will call this method if it detects that this object is involved
157 in a reference cycle.