blob: b7be993ccfa527346eb94ca744b0b8efe5a347ae [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`
31 or :cfunc:`PyObject_GC_VarNew`.
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 .. versionchanged:: 2.5
49 This function used an :ctype:`int` type for *size*. This might require
50 changes in your code for properly supporting 64-bit systems.
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000052
53.. cfunction:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000055 Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the
56 resized object or *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000058 .. versionchanged:: 2.5
59 This function used an :ctype:`int` type for *newsize*. This might
60 require changes in your code for properly supporting 64-bit systems.
61
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
63.. cfunction:: void PyObject_GC_Track(PyObject *op)
64
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000065 Adds the object *op* to the set of container objects tracked by the
66 collector. The collector can run at unexpected times so objects must be
67 valid while being tracked. This should be called once all the fields
68 followed by the :attr:`tp_traverse` handler become valid, usually near the
69 end of the constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +000070
71
72.. cfunction:: void _PyObject_GC_TRACK(PyObject *op)
73
74 A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for
75 extension modules.
76
77Similarly, the deallocator for the object must conform to a similar pair of
78rules:
79
80#. Before fields which refer to other containers are invalidated,
81 :cfunc:`PyObject_GC_UnTrack` must be called.
82
83#. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`.
84
85
86.. cfunction:: void PyObject_GC_Del(void *op)
87
88 Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or
89 :cfunc:`PyObject_GC_NewVar`.
90
91
92.. cfunction:: void PyObject_GC_UnTrack(void *op)
93
94 Remove the object *op* from the set of container objects tracked by the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000095 collector. Note that :cfunc:`PyObject_GC_Track` can be called again on
96 this object to add it back to the set of tracked objects. The deallocator
97 (:attr:`tp_dealloc` handler) should call this for the object before any of
98 the fields used by the :attr:`tp_traverse` handler become invalid.
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
100
101.. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op)
102
103 A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for
104 extension modules.
105
106The :attr:`tp_traverse` handler accepts a function parameter of this type:
107
108
109.. ctype:: int (*visitproc)(PyObject *object, void *arg)
110
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000111 Type of the visitor function passed to the :attr:`tp_traverse` handler.
112 The function should be called with an object to traverse as *object* and
113 the third parameter to the :attr:`tp_traverse` handler as *arg*. The
114 Python core uses several visitor functions to implement cyclic garbage
115 detection; it's not expected that users will need to write their own
116 visitor functions.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000117
118The :attr:`tp_traverse` handler must have the following type:
119
120
121.. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
122
123 Traversal function for a container object. Implementations must call the
124 *visit* function for each object directly contained by *self*, with the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000125 parameters to *visit* being the contained object and the *arg* value passed
126 to the handler. The *visit* function must not be called with a *NULL*
127 object argument. If *visit* returns a non-zero value that value should be
128 returned immediately.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
130To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is
131provided. In order to use this macro, the :attr:`tp_traverse` implementation
132must name its arguments exactly *visit* and *arg*:
133
134
135.. cfunction:: void Py_VISIT(PyObject *o)
136
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000137 Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns
138 a non-zero value, then return it. Using this macro, :attr:`tp_traverse`
139 handlers look like::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141 static int
142 my_traverse(Noddy *self, visitproc visit, void *arg)
143 {
144 Py_VISIT(self->foo);
145 Py_VISIT(self->bar);
146 return 0;
147 }
148
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000149The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL*
150if the object is immutable.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000151
152
153.. ctype:: int (*inquiry)(PyObject *self)
154
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000155 Drop references that may have created reference cycles. Immutable objects
156 do not have to define this method since they can never directly create
157 reference cycles. Note that the object must still be valid after calling
158 this method (don't just call :cfunc:`Py_DECREF` on a reference). The
159 collector will call this method if it detects that this object is involved
160 in a reference cycle.