blob: 4cab0f544ed811bb2b4edaa645cd42f8dfe4a6b3 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Antoine Pitrou39668f52013-08-01 21:12:45 +020015To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
Georg Brandl54a3faa2008-01-20 09:30:57 +000016include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
Antoine Pitrou39668f52013-08-01 21:12:45 +020017:c:member:`~PyTypeObject.tp_traverse` handler. If instances of the type are mutable, a
18:c:member:`~PyTypeObject.tp_clear` implementation must also be provided.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
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
Georg Brandl60203b42010-10-06 10:11:56 +000030#. The memory for the object must be allocated using :c:func:`PyObject_GC_New`
31 or :c:func:`PyObject_GC_NewVar`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33#. Once all the fields which may contain references to other containers are
Georg Brandl60203b42010-10-06 10:11:56 +000034 initialized, it must call :c:func:`PyObject_GC_Track`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
36
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
Georg Brandl60203b42010-10-06 10:11:56 +000039 Analogous to :c:func:`PyObject_New` but for container objects with the
Georg Brandl54a3faa2008-01-20 09:30:57 +000040 :const:`Py_TPFLAGS_HAVE_GC` flag set.
41
42
Georg Brandl60203b42010-10-06 10:11:56 +000043.. c:function:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
Georg Brandl60203b42010-10-06 10:11:56 +000045 Analogous to :c:func:`PyObject_NewVar` but for container objects with the
Georg Brandl54a3faa2008-01-20 09:30:57 +000046 :const:`Py_TPFLAGS_HAVE_GC` flag set.
47
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000048
Georg Brandl60203b42010-10-06 10:11:56 +000049.. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
Georg Brandl54a3faa2008-01-20 09:30:57 +000050
Georg Brandl60203b42010-10-06 10:11:56 +000051 Resize an object allocated by :c:func:`PyObject_NewVar`. Returns the
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020052 resized object or ``NULL`` on failure. *op* must not be tracked by the collector yet.
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
54
Georg Brandl60203b42010-10-06 10:11:56 +000055.. c:function:: void PyObject_GC_Track(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
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
Antoine Pitrou39668f52013-08-01 21:12:45 +020060 followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000061 end of the constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
Pablo Galindof13072b2020-04-11 01:21:54 +010063.. c:function:: int PyObject_GC_IsTracked(PyObject *op)
64
65 Returns 1 if the object type of *op* implements the GC protocol and *op* is being
66 currently tracked by the garbage collector and 0 otherwise.
67
68 This is analogous to the Python function :func:`gc.is_tracked`.
69
70 .. versionadded:: 3.9
71
72
73.. c:function:: int PyObject_GC_IsFinalized(PyObject *op)
74
75 Returns 1 if the object type of *op* implements the GC protocol and *op* has been
76 already finalized by the garbage collector and 0 otherwise.
77
78 This is analogous to the Python function :func:`gc.is_finalized`.
79
80 .. versionadded:: 3.9
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Georg Brandl54a3faa2008-01-20 09:30:57 +000082Similarly, the deallocator for the object must conform to a similar pair of
83rules:
84
85#. Before fields which refer to other containers are invalidated,
Georg Brandl60203b42010-10-06 10:11:56 +000086 :c:func:`PyObject_GC_UnTrack` must be called.
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: void PyObject_GC_Del(void *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
Georg Brandl60203b42010-10-06 10:11:56 +000093 Releases memory allocated to an object using :c:func:`PyObject_GC_New` or
94 :c:func:`PyObject_GC_NewVar`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: void PyObject_GC_UnTrack(void *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
99 Remove the object *op* from the set of container objects tracked by the
Georg Brandl60203b42010-10-06 10:11:56 +0000100 collector. Note that :c:func:`PyObject_GC_Track` can be called again on
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000101 this object to add it back to the set of tracked objects. The deallocator
Antoine Pitrou39668f52013-08-01 21:12:45 +0200102 (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of
103 the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000104
105
Victor Stinner1a6be912018-11-13 12:52:18 +0100106.. versionchanged:: 3.8
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107
Victor Stinner1a6be912018-11-13 12:52:18 +0100108 The :c:func:`_PyObject_GC_TRACK` and :c:func:`_PyObject_GC_UNTRACK` macros
109 have been removed from the public C API.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000110
Antoine Pitrou39668f52013-08-01 21:12:45 +0200111The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
113
Georg Brandl60203b42010-10-06 10:11:56 +0000114.. c:type:: int (*visitproc)(PyObject *object, void *arg)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000115
Antoine Pitrou39668f52013-08-01 21:12:45 +0200116 Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler.
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000117 The function should be called with an object to traverse as *object* and
Antoine Pitrou39668f52013-08-01 21:12:45 +0200118 the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*. The
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000119 Python core uses several visitor functions to implement cyclic garbage
120 detection; it's not expected that users will need to write their own
121 visitor functions.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
Antoine Pitrou39668f52013-08-01 21:12:45 +0200123The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000124
125
Georg Brandl60203b42010-10-06 10:11:56 +0000126.. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
128 Traversal function for a container object. Implementations must call the
129 *visit* function for each object directly contained by *self*, with the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000130 parameters to *visit* being the contained object and the *arg* value passed
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200131 to the handler. The *visit* function must not be called with a ``NULL``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000132 object argument. If *visit* returns a non-zero value that value should be
133 returned immediately.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
Antoine Pitrou39668f52013-08-01 21:12:45 +0200135To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
136provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137must name its arguments exactly *visit* and *arg*:
138
139
Georg Brandl60203b42010-10-06 10:11:56 +0000140.. c:function:: void Py_VISIT(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000141
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200142 If *o* is not ``NULL``, call the *visit* callback, with arguments *o*
Benjamin Petersona49ccf22016-06-02 11:35:59 -0700143 and *arg*. If *visit* returns a non-zero value, then return it.
144 Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers
145 look like::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000146
147 static int
148 my_traverse(Noddy *self, visitproc visit, void *arg)
149 {
150 Py_VISIT(self->foo);
151 Py_VISIT(self->bar);
152 return 0;
153 }
154
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200155The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or ``NULL``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000156if the object is immutable.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000157
158
Georg Brandl60203b42010-10-06 10:11:56 +0000159.. c:type:: int (*inquiry)(PyObject *self)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000160
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000161 Drop references that may have created reference cycles. Immutable objects
162 do not have to define this method since they can never directly create
163 reference cycles. Note that the object must still be valid after calling
Georg Brandl60203b42010-10-06 10:11:56 +0000164 this method (don't just call :c:func:`Py_DECREF` on a reference). The
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000165 collector will call this method if it detects that this object is involved
166 in a reference cycle.