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` |
Georg Brandl | 303e675 | 2010-03-07 20:58:31 +0000 | [diff] [blame^] | 34 | or :cfunc:`PyObject_GC_NewVar`. |
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 | |
Jeroen Ruigrok van der Werven | 691f5f1 | 2009-04-25 19:44:55 +0000 | [diff] [blame] | 51 | .. versionchanged:: 2.5 |
| 52 | This function used an :ctype:`int` type for *size*. This might require |
| 53 | changes in your code for properly supporting 64-bit systems. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 54 | |
Jeroen Ruigrok van der Werven | 691f5f1 | 2009-04-25 19:44:55 +0000 | [diff] [blame] | 55 | |
| 56 | .. cfunction:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 57 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 58 | Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the |
| 59 | resized object or *NULL* on failure. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 60 | |
Jeroen Ruigrok van der Werven | 691f5f1 | 2009-04-25 19:44:55 +0000 | [diff] [blame] | 61 | .. versionchanged:: 2.5 |
| 62 | This function used an :ctype:`int` type for *newsize*. This might |
| 63 | require changes in your code for properly supporting 64-bit systems. |
| 64 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
| 66 | .. cfunction:: void PyObject_GC_Track(PyObject *op) |
| 67 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 68 | Adds the object *op* to the set of container objects tracked by the |
| 69 | collector. The collector can run at unexpected times so objects must be |
| 70 | valid while being tracked. This should be called once all the fields |
| 71 | followed by the :attr:`tp_traverse` handler become valid, usually near the |
| 72 | end of the constructor. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | .. cfunction:: void _PyObject_GC_TRACK(PyObject *op) |
| 76 | |
| 77 | A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for |
| 78 | extension modules. |
| 79 | |
| 80 | Similarly, the deallocator for the object must conform to a similar pair of |
| 81 | rules: |
| 82 | |
| 83 | #. Before fields which refer to other containers are invalidated, |
| 84 | :cfunc:`PyObject_GC_UnTrack` must be called. |
| 85 | |
| 86 | #. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`. |
| 87 | |
| 88 | |
| 89 | .. cfunction:: void PyObject_GC_Del(void *op) |
| 90 | |
| 91 | Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or |
| 92 | :cfunc:`PyObject_GC_NewVar`. |
| 93 | |
| 94 | |
| 95 | .. cfunction:: void PyObject_GC_UnTrack(void *op) |
| 96 | |
| 97 | 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] | 98 | collector. Note that :cfunc:`PyObject_GC_Track` can be called again on |
| 99 | this object to add it back to the set of tracked objects. The deallocator |
| 100 | (:attr:`tp_dealloc` handler) should call this for the object before any of |
| 101 | the fields used by the :attr:`tp_traverse` handler become invalid. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | .. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op) |
| 105 | |
| 106 | A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for |
| 107 | extension modules. |
| 108 | |
| 109 | The :attr:`tp_traverse` handler accepts a function parameter of this type: |
| 110 | |
| 111 | |
| 112 | .. ctype:: int (*visitproc)(PyObject *object, void *arg) |
| 113 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 114 | Type of the visitor function passed to the :attr:`tp_traverse` handler. |
| 115 | The function should be called with an object to traverse as *object* and |
| 116 | the third parameter to the :attr:`tp_traverse` handler as *arg*. The |
| 117 | Python core uses several visitor functions to implement cyclic garbage |
| 118 | detection; it's not expected that users will need to write their own |
| 119 | visitor functions. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 120 | |
| 121 | The :attr:`tp_traverse` handler must have the following type: |
| 122 | |
| 123 | |
| 124 | .. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg) |
| 125 | |
| 126 | Traversal function for a container object. Implementations must call the |
| 127 | *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] | 128 | parameters to *visit* being the contained object and the *arg* value passed |
| 129 | to the handler. The *visit* function must not be called with a *NULL* |
| 130 | object argument. If *visit* returns a non-zero value that value should be |
| 131 | returned immediately. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 132 | |
| 133 | To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is |
| 134 | provided. In order to use this macro, the :attr:`tp_traverse` implementation |
| 135 | must name its arguments exactly *visit* and *arg*: |
| 136 | |
| 137 | |
| 138 | .. cfunction:: void Py_VISIT(PyObject *o) |
| 139 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 140 | Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns |
| 141 | a non-zero value, then return it. Using this macro, :attr:`tp_traverse` |
| 142 | handlers look like:: |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 143 | |
| 144 | static int |
| 145 | my_traverse(Noddy *self, visitproc visit, void *arg) |
| 146 | { |
| 147 | Py_VISIT(self->foo); |
| 148 | Py_VISIT(self->bar); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | .. versionadded:: 2.4 |
| 153 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 154 | The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL* |
| 155 | if the object is immutable. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | .. ctype:: int (*inquiry)(PyObject *self) |
| 159 | |
Jeroen Ruigrok van der Werven | 3537124 | 2009-04-25 19:10:52 +0000 | [diff] [blame] | 160 | Drop references that may have created reference cycles. Immutable objects |
| 161 | do not have to define this method since they can never directly create |
| 162 | reference cycles. Note that the object must still be valid after calling |
| 163 | this method (don't just call :cfunc:`Py_DECREF` on a reference). The |
| 164 | collector will call this method if it detects that this object is involved |
| 165 | in a reference cycle. |