blob: fe2b0cabf28b457243eda294e3e9b59b2aca78e2 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +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
12or strings), do not need to provide any explicit support for garbage collection.
13
14To create a container type, the :attr:`tp_flags` field of the type object must
15include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
16:attr:`tp_traverse` handler. If instances of the type are mutable, a
17:attr:`tp_clear` implementation must also be provided.
18
19
20.. data:: Py_TPFLAGS_HAVE_GC
Georg Brandlc31b0f72008-01-20 11:00:14 +000021 :noindex:
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23 Objects with a type with this flag set must conform with the rules documented
24 here. For convenience these objects will be referred to as container objects.
25
26Constructors for container types must conform to two rules:
27
28#. The memory for the object must be allocated using :cfunc:`PyObject_GC_New` or
29 :cfunc:`PyObject_GC_VarNew`.
30
31#. Once all the fields which may contain references to other containers are
32 initialized, it must call :cfunc:`PyObject_GC_Track`.
33
34
35.. cfunction:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
36
37 Analogous to :cfunc:`PyObject_New` but for container objects with the
38 :const:`Py_TPFLAGS_HAVE_GC` flag set.
39
40
41.. cfunction:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
42
43 Analogous to :cfunc:`PyObject_NewVar` but for container objects with the
44 :const:`Py_TPFLAGS_HAVE_GC` flag set.
45
46
47.. cfunction:: PyVarObject * PyObject_GC_Resize(PyVarObject *op, Py_ssize_t)
48
49 Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the resized
50 object or *NULL* on failure.
51
52
53.. cfunction:: void PyObject_GC_Track(PyObject *op)
54
55 Adds the object *op* to the set of container objects tracked by the collector.
56 The collector can run at unexpected times so objects must be valid while being
57 tracked. This should be called once all the fields followed by the
58 :attr:`tp_traverse` handler become valid, usually near the end of the
59 constructor.
60
61
62.. cfunction:: void _PyObject_GC_TRACK(PyObject *op)
63
64 A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for
65 extension modules.
66
67Similarly, the deallocator for the object must conform to a similar pair of
68rules:
69
70#. Before fields which refer to other containers are invalidated,
71 :cfunc:`PyObject_GC_UnTrack` must be called.
72
73#. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`.
74
75
76.. cfunction:: void PyObject_GC_Del(void *op)
77
78 Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or
79 :cfunc:`PyObject_GC_NewVar`.
80
81
82.. cfunction:: void PyObject_GC_UnTrack(void *op)
83
84 Remove the object *op* from the set of container objects tracked by the
85 collector. Note that :cfunc:`PyObject_GC_Track` can be called again on this
86 object to add it back to the set of tracked objects. The deallocator
87 (:attr:`tp_dealloc` handler) should call this for the object before any of the
88 fields used by the :attr:`tp_traverse` handler become invalid.
89
90
91.. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op)
92
93 A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for
94 extension modules.
95
96The :attr:`tp_traverse` handler accepts a function parameter of this type:
97
98
99.. ctype:: int (*visitproc)(PyObject *object, void *arg)
100
101 Type of the visitor function passed to the :attr:`tp_traverse` handler. The
102 function should be called with an object to traverse as *object* and the third
103 parameter to the :attr:`tp_traverse` handler as *arg*. The Python core uses
104 several visitor functions to implement cyclic garbage detection; it's not
105 expected that users will need to write their own visitor functions.
106
107The :attr:`tp_traverse` handler must have the following type:
108
109
110.. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
111
112 Traversal function for a container object. Implementations must call the
113 *visit* function for each object directly contained by *self*, with the
114 parameters to *visit* being the contained object and the *arg* value passed to
115 the handler. The *visit* function must not be called with a *NULL* object
116 argument. If *visit* returns a non-zero value that value should be returned
117 immediately.
118
119To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is
120provided. In order to use this macro, the :attr:`tp_traverse` implementation
121must name its arguments exactly *visit* and *arg*:
122
123
124.. cfunction:: void Py_VISIT(PyObject *o)
125
126 Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns a
127 non-zero value, then return it. Using this macro, :attr:`tp_traverse` handlers
128 look like::
129
130 static int
131 my_traverse(Noddy *self, visitproc visit, void *arg)
132 {
133 Py_VISIT(self->foo);
134 Py_VISIT(self->bar);
135 return 0;
136 }
137
138The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL* if
139the object is immutable.
140
141
142.. ctype:: int (*inquiry)(PyObject *self)
143
144 Drop references that may have created reference cycles. Immutable objects do
145 not have to define this method since they can never directly create reference
146 cycles. Note that the object must still be valid after calling this method
147 (don't just call :cfunc:`Py_DECREF` on a reference). The collector will call
148 this method if it detects that this object is involved in a reference cycle.