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