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