Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _supporting-cycle-detection: |
| 4 | |
| 5 | Supporting Cyclic Garbage Collection |
| 6 | ==================================== |
| 7 | |
| 8 | Python's support for detecting and collecting garbage which involves circular |
| 9 | references requires support from object types which are "containers" for other |
| 10 | objects which may also be containers. Types which do not store references to |
| 11 | other objects, or which only store references to atomic types (such as numbers |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 12 | or strings), do not need to provide any explicit support for garbage |
| 13 | collection. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 14 | |
| 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 | |
| 18 | To create a container type, the :attr:`tp_flags` field of the type object must |
| 19 | include 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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 27 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 30 | |
| 31 | Constructors for container types must conform to two rules: |
| 32 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 33 | #. The memory for the object must be allocated using :cfunc:`PyObject_GC_New` |
| 34 | or :cfunc:`PyObject_GC_VarNew`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 35 | |
| 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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 54 | Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the |
| 55 | resized object or *NULL* on failure. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | .. cfunction:: void PyObject_GC_Track(PyObject *op) |
| 59 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 60 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
| 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 | |
| 72 | Similarly, the deallocator for the object must conform to a similar pair of |
| 73 | rules: |
| 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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 90 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 94 | |
| 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 | |
| 101 | The :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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 106 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 112 | |
| 113 | The :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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 120 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 124 | |
| 125 | To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is |
| 126 | provided. In order to use this macro, the :attr:`tp_traverse` implementation |
| 127 | must name its arguments exactly *visit* and *arg*: |
| 128 | |
| 129 | |
| 130 | .. cfunction:: void Py_VISIT(PyObject *o) |
| 131 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 132 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 135 | |
| 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 Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 146 | The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL* |
| 147 | if the object is immutable. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | .. ctype:: int (*inquiry)(PyObject *self) |
| 151 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame^] | 152 | 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. |