@@ -5508,66 +5508,85 @@ \section{Supporting Cyclic Garbarge Collection
55085508to provide any explicit support for garbage collection.
55095509
55105510To create a container type, the \member {tp_flags} field of the type
5511- object must include the \constant {Py_TPFLAGS_GC} and provide an
5512- implementation of the \member {tp_traverse} handler. The computed
5513- value of the \member {tp_basicsize} field must include
5514- \constant {PyGC_HEAD_SIZE} as well. If instances of the type are
5515- mutable, a \member {tp_clear} implementation must also be provided.
5511+ object must include the \constant {Py_TPFLAGS_HAVE_GC} and provide an
5512+ implementation of the \member {tp_traverse} handler. If instances of the
5513+ type are mutable, a \member {tp_clear} implementation must also be
5514+ provided.
55165515
5517- \begin {datadesc }{Py_TPFLAGS_GC }
5516+ \begin {datadesc }{Py_TPFLAGS_HAVE_GC }
55185517 Objects with a type with this flag set must conform with the rules
55195518 documented here. For convenience these objects will be referred to
55205519 as container objects.
55215520\end {datadesc }
55225521
5523- \begin {datadesc }{PyGC_HEAD_SIZE}
5524- Extra memory needed for the garbage collector. Container objects
5525- must include this in the calculation of their tp_basicsize. If the
5526- collector is disabled at compile time then this is \code {0}.
5527- \end {datadesc }
5528-
55295522Constructors for container types must conform to two rules:
55305523
55315524\begin {enumerate }
55325525\item The memory for the object must be allocated using
5533- \cfunction {PyObject_New ()} or \cfunction {PyObject_VarNew ()}.
5526+ \cfunction {PyObject_GC_New ()} or \cfunction {PyObject_GC_VarNew ()}.
55345527
55355528\item Once all the fields which may contain references to other
55365529 containers are initialized, it must call
5537- \cfunction {PyObject_GC_Init ()}.
5530+ \cfunction {PyObject_GC_Track ()}.
55385531\end {enumerate }
55395532
5540- \begin {cfuncdesc }{void}{PyObject_GC_Init}{PyObject *op}
5533+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_GC_New}{TYPE, PyTypeObject *type}
5534+ Analogous to \cfunction {PyObject_New()} but for container objects with
5535+ the \constant {Py_TPFLAGS_HAVE_GC} flag set.
5536+ \end {cfuncdesc }
5537+
5538+ \begin {cfuncdesc }{\var {TYPE}*}{PyObject_GC_NewVar}{TYPE, PyTypeObject *type,
5539+ int size}
5540+ Analogous to \cfunction {PyObject_NewVar()} but for container objects
5541+ with the \constant {Py_TPFLAGS_HAVE_GC} flag set.
5542+ \end {cfuncdesc }
5543+
5544+ \begin {cfuncdesc }{PyVarObject *}{PyObject_GC_Resize}{PyVarObject *op, int}
5545+ Resize an object allocated by \cfunction {PyObject_NewVar()}. Returns
5546+ the resized object or \NULL {} on failure.
5547+ \end {cfuncdesc }
5548+
5549+ \begin {cfuncdesc }{void}{PyObject_GC_Track}{PyObject *op}
55415550 Adds the object \var {op} to the set of container objects tracked by
55425551 the collector. The collector can run at unexpected times so objects
55435552 must be valid while being tracked. This should be called once all
55445553 the fields followed by the \member {tp_traverse} handler become valid,
55455554 usually near the end of the constructor.
55465555\end {cfuncdesc }
55475556
5557+ \begin {cfuncdesc }{void}{_PyObject_GC_TRACK}{PyObject *op}
5558+ A macro version of \cfunction {PyObject_GC_Track()}. It should not be
5559+ used for extension modules.
5560+ \end {cfuncdesc }
5561+
55485562Similarly, the deallocator for the object must conform to a similar
55495563pair of rules:
55505564
55515565\begin {enumerate }
55525566\item Before fields which refer to other containers are invalidated,
5553- \cfunction {PyObject_GC_Fini ()} must be called.
5567+ \cfunction {PyObject_GC_UnTrack ()} must be called.
55545568
55555569\item The object's memory must be deallocated using
5556- \cfunction {PyObject_Del ()}.
5570+ \cfunction {PyObject_GC_Del ()}.
55575571\end {enumerate }
55585572
5559- \begin {cfuncdesc }{void}{PyObject_GC_Fini}{PyObject *op}
5573+ \begin {cfuncdesc }{void}{PyObject_GC_Del}{PyObject *op}
5574+ Releases memory allocated to an object using
5575+ \cfunction {PyObject_GC_New()} or \cfunction {PyObject_GC_NewVar()}.
5576+ \end {cfuncdesc }
5577+
5578+ \begin {cfuncdesc }{void}{PyObject_GC_UnTrack}{PyObject *op}
55605579 Remove the object \var {op} from the set of container objects tracked
5561- by the collector. Note that \cfunction {PyObject_GC_Init ()} can be
5580+ by the collector. Note that \cfunction {PyObject_GC_Track ()} can be
55625581 called again on this object to add it back to the set of tracked
55635582 objects. The deallocator (\member {tp_dealloc} handler) should call
55645583 this for the object before any of the fields used by the
55655584 \member {tp_traverse} handler become invalid.
5585+ \end {cfuncdesc }
55665586
5567- \strong {Note:} Any container which may be referenced from another
5568- object reachable by the collector must itself be tracked by the
5569- collector, so it is generally not safe to call this function
5570- anywhere but in the object's deallocator.
5587+ \begin {cfuncdesc }{void}{_PyObject_GC_UNTRACK}{PyObject *op}
5588+ A macro version of \cfunction {PyObject_GC_UnTrack()}. It should not be
5589+ used for extension modules.
55715590\end {cfuncdesc }
55725591
55735592The \member {tp_traverse} handler accepts a function parameter of this
@@ -5650,9 +5669,9 @@ \subsection{Example Cycle Collector Support
56505669static void
56515670my_dealloc(MyObject *self)
56525671{
5653- PyObject_GC_Fini ((PyObject *) self);
5672+ PyObject_GC_UnTrack ((PyObject *) self);
56545673 Py_XDECREF(self->container);
5655- PyObject_Del (self);
5674+ PyObject_GC_Del (self);
56565675}
56575676\end {verbatim }
56585677
@@ -5662,7 +5681,7 @@ \subsection{Example Cycle Collector Support
56625681 PyObject_HEAD_INIT(NULL)
56635682 0,
56645683 "MyObject",
5665- sizeof(MyObject) + PyGC_HEAD_SIZE ,
5684+ sizeof(MyObject),
56665685 0,
56675686 (destructor)my_dealloc, /* tp_dealloc */
56685687 0, /* tp_print */
@@ -5679,7 +5698,7 @@ \subsection{Example Cycle Collector Support
56795698 0, /* tp_getattro */
56805699 0, /* tp_setattro */
56815700 0, /* tp_as_buffer */
5682- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC ,
5701+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC ,
56835702 0, /* tp_doc */
56845703 (traverseproc)my_traverse, /* tp_traverse */
56855704 (inquiry)my_clear, /* tp_clear */
@@ -5695,10 +5714,10 @@ \subsection{Example Cycle Collector Support
56955714 MyObject *result = NULL;
56965715
56975716 if (PyArg_ParseTuple(args, "|O:new_object", &container)) {
5698- result = PyObject_New (MyObject, &MyObject_Type);
5717+ result = PyObject_GC_New (MyObject, &MyObject_Type);
56995718 if (result != NULL) {
57005719 result->container = container;
5701- PyObject_GC_Init( );
5720+ PyObject_GC_Track(result );
57025721 }
57035722 }
57045723 return (PyObject *) result;
0 commit comments