Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36eb4df

Browse files
committed
Refactored some of the Py_TRACE_REFS code. New private API function
_Py_AddToAllObjects() that simply inserts an object at the front of the doubly-linked list of all objects. Changed PyType_Ready() (the closest thing we've got to a choke point for type objects) to call that.
1 parent 3e40c7f commit 36eb4df

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

Include/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ PyAPI_FUNC(void) _Py_NewReference(PyObject *);
582582
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
583583
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
584584
PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
585+
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *);
585586

586587
#else
587588
/* Without Py_TRACE_REFS, there's little enough to do that we expand code

Objects/object.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ int Py_DivisionWarningFlag;
2020
#ifdef Py_TRACE_REFS
2121
/* Head of doubly-linked list of all objects. */
2222
static PyObject refchain = {&refchain, &refchain};
23+
24+
/* Insert op at the fron of the doubly-linked list of all objects. */
25+
void
26+
_Py_AddToAllObjects(PyObject *op)
27+
{
28+
op->_ob_next = refchain._ob_next;
29+
op->_ob_prev = &refchain;
30+
refchain._ob_next->_ob_prev = op;
31+
refchain._ob_next = op;
32+
}
2333
#endif
2434

2535
#ifdef COUNT_ALLOCS
@@ -91,12 +101,9 @@ inc_count(PyTypeObject *tp)
91101
type_list = tp;
92102
#ifdef Py_TRACE_REFS
93103
/* Also insert in the doubly-linked list of all objects. */
94-
if (tp->_ob_next == NULL) {
95-
PyObject *op = (PyObject *)tp;
96-
op->_ob_next = refchain._ob_next;
97-
op->_ob_prev = &refchain;
98-
refchain._ob_next->_ob_prev = op;
99-
refchain._ob_next = op;
104+
if (tp->_ob_prev == NULL) {
105+
assert(tp->_ob_next == NULL);
106+
_Py_AddToAllObjects((PyObject *)tp);
100107
}
101108
#endif
102109
}
@@ -1956,10 +1963,7 @@ _Py_NewReference(PyObject *op)
19561963
{
19571964
_Py_INC_REFTOTAL;
19581965
op->ob_refcnt = 1;
1959-
op->_ob_next = refchain._ob_next;
1960-
op->_ob_prev = &refchain;
1961-
refchain._ob_next->_ob_prev = op;
1962-
refchain._ob_next = op;
1966+
_Py_AddToAllObjects(op);
19631967
_Py_INC_TPALLOCS(op);
19641968
}
19651969

Objects/typeobject.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,6 +3052,18 @@ PyType_Ready(PyTypeObject *type)
30523052

30533053
type->tp_flags |= Py_TPFLAGS_READYING;
30543054

3055+
#ifdef Py_TRACE_REFS
3056+
/* PyType_Ready is the closest thing we have to a choke point
3057+
* for type objects, so is the best place I can think of to try
3058+
* to get type objects into the doubly-linked list of all objects.
3059+
* Still, not all type objects go thru PyType_Ready.
3060+
*/
3061+
if (type->_ob_next == NULL) {
3062+
assert(type->_ob_prev == NULL);
3063+
_Py_AddToAllObjects((PyObject *)type);
3064+
}
3065+
#endif
3066+
30553067
/* Initialize tp_base (defaults to BaseObject unless that's us) */
30563068
base = type->tp_base;
30573069
if (base == NULL && type != &PyBaseObject_Type)

0 commit comments

Comments
 (0)