@@ -53,6 +53,8 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
53
53
54
54
55
55
/* Tell the GC to track this object.
56
+ *
57
+ * The object must not be tracked by the GC.
56
58
*
57
59
* NB: While the object is tracked by the collector, it must be safe to call the
58
60
* ob_traverse method.
@@ -61,20 +63,24 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
61
63
* because it's not object header. So we don't use _PyGCHead_PREV() and
62
64
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
63
65
*
64
- * The PyObject_GC_Track() function is the public version of this macro .
66
+ * See also the public PyObject_GC_Track() function .
65
67
*/
66
- static inline void _PyObject_GC_TRACK_impl (const char * filename , int lineno ,
67
- PyObject * op )
68
+ static inline void _PyObject_GC_TRACK (
69
+ // The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
70
+ #ifndef NDEBUG
71
+ const char * filename , int lineno ,
72
+ #endif
73
+ PyObject * op )
68
74
{
69
75
_PyObject_ASSERT_FROM (op , !_PyObject_GC_IS_TRACKED (op ),
70
76
"object already tracked by the garbage collector" ,
71
- filename , lineno , "_PyObject_GC_TRACK" );
77
+ filename , lineno , __func__ );
72
78
73
79
PyGC_Head * gc = _Py_AS_GC (op );
74
80
_PyObject_ASSERT_FROM (op ,
75
81
(gc -> _gc_prev & _PyGC_PREV_MASK_COLLECTING ) == 0 ,
76
82
"object is in generation which is garbage collected" ,
77
- filename , lineno , "_PyObject_GC_TRACK" );
83
+ filename , lineno , __func__ );
78
84
79
85
PyThreadState * tstate = _PyThreadState_GET ();
80
86
PyGC_Head * generation0 = tstate -> interp -> gc .generation0 ;
@@ -85,24 +91,26 @@ static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
85
91
generation0 -> _gc_prev = (uintptr_t )gc ;
86
92
}
87
93
88
- #define _PyObject_GC_TRACK (op ) \
89
- _PyObject_GC_TRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
90
-
91
94
/* Tell the GC to stop tracking this object.
92
95
*
93
96
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
94
97
* must be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
95
98
*
96
99
* The object must be tracked by the GC.
97
100
*
98
- * The PyObject_GC_UnTrack() function is the public version of this macro.
101
+ * See also the public PyObject_GC_UnTrack() which accept an object which is
102
+ * not tracked.
99
103
*/
100
- static inline void _PyObject_GC_UNTRACK_impl (const char * filename , int lineno ,
101
- PyObject * op )
104
+ static inline void _PyObject_GC_UNTRACK (
105
+ // The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
106
+ #ifndef NDEBUG
107
+ const char * filename , int lineno ,
108
+ #endif
109
+ PyObject * op )
102
110
{
103
111
_PyObject_ASSERT_FROM (op , _PyObject_GC_IS_TRACKED (op ),
104
112
"object not tracked by the garbage collector" ,
105
- filename , lineno , "_PyObject_GC_UNTRACK" );
113
+ filename , lineno , __func__ );
106
114
107
115
PyGC_Head * gc = _Py_AS_GC (op );
108
116
PyGC_Head * prev = _PyGCHead_PREV (gc );
@@ -113,8 +121,20 @@ static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
113
121
gc -> _gc_prev &= _PyGC_PREV_MASK_FINALIZED ;
114
122
}
115
123
116
- #define _PyObject_GC_UNTRACK (op ) \
117
- _PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
124
+ // Macros to accept any type for the parameter, and to automatically pass
125
+ // the filename and the filename (if NDEBUG is not defined) where the macro
126
+ // is called.
127
+ #ifdef NDEBUG
128
+ # define _PyObject_GC_TRACK (op ) \
129
+ _PyObject_GC_TRACK(_PyObject_CAST(op))
130
+ # define _PyObject_GC_UNTRACK (op ) \
131
+ _PyObject_GC_UNTRACK(_PyObject_CAST(op))
132
+ #else
133
+ # define _PyObject_GC_TRACK (op ) \
134
+ _PyObject_GC_TRACK(__FILE__, __LINE__, _PyObject_CAST(op))
135
+ # define _PyObject_GC_UNTRACK (op ) \
136
+ _PyObject_GC_UNTRACK(__FILE__, __LINE__, _PyObject_CAST(op))
137
+ #endif
118
138
119
139
#ifdef Py_REF_DEBUG
120
140
extern void _PyDebug_PrintTotalRefs (void );
0 commit comments