@@ -56,6 +56,24 @@ _PyObject_CheckConsistency(PyObject *op, int check_content)
5656#ifdef  Py_REF_DEBUG 
5757Py_ssize_t  _Py_RefTotal ;
5858
59+ static  inline  void 
60+ reftotal_increment (void )
61+ {
62+     _Py_RefTotal ++ ;
63+ }
64+ 
65+ static  inline  void 
66+ reftotal_decrement (void )
67+ {
68+     _Py_RefTotal -- ;
69+ }
70+ 
71+ void 
72+ _Py_AddRefTotal (Py_ssize_t  n )
73+ {
74+     _Py_RefTotal  +=  n ;
75+ }
76+ 
5977Py_ssize_t 
6078_Py_GetRefTotal (void )
6179{
@@ -121,6 +139,32 @@ _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
121139                           filename , lineno , __func__ );
122140}
123141
142+ /* This is exposed strictly for use in Py_INCREF(). */ 
143+ PyAPI_FUNC (void )
144+ _Py_IncRefTotal_DO_NOT_USE_THIS (void )
145+ {
146+     reftotal_increment ();
147+ }
148+ 
149+ /* This is exposed strictly for use in Py_DECREF(). */ 
150+ PyAPI_FUNC (void )
151+ _Py_DecRefTotal_DO_NOT_USE_THIS (void )
152+ {
153+     reftotal_decrement ();
154+ }
155+ 
156+ void 
157+ _Py_IncRefTotal (void )
158+ {
159+     reftotal_increment ();
160+ }
161+ 
162+ void 
163+ _Py_DecRefTotal (void )
164+ {
165+     reftotal_decrement ();
166+ }
167+ 
124168#endif  /* Py_REF_DEBUG */ 
125169
126170void 
@@ -138,12 +182,18 @@ Py_DecRef(PyObject *o)
138182void 
139183_Py_IncRef (PyObject  * o )
140184{
185+ #ifdef  Py_REF_DEBUG 
186+     reftotal_increment ();
187+ #endif 
141188    Py_INCREF (o );
142189}
143190
144191void 
145192_Py_DecRef (PyObject  * o )
146193{
194+ #ifdef  Py_REF_DEBUG 
195+     reftotal_decrement ();
196+ #endif 
147197    Py_DECREF (o );
148198}
149199
@@ -238,17 +288,12 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
238288    /* tp_finalize resurrected it!  Make it look like the original Py_DECREF 
239289     * never happened. */ 
240290    Py_ssize_t  refcnt  =  Py_REFCNT (self );
241-     _Py_NewReference (self );
291+     _Py_NewReferenceNoTotal (self );
242292    Py_SET_REFCNT (self , refcnt );
243293
244294    _PyObject_ASSERT (self ,
245295                     (!_PyType_IS_GC (Py_TYPE (self ))
246296                      ||  _PyObject_GC_IS_TRACKED (self )));
247-     /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased 
248-        _Py_RefTotal, so we need to undo that. */ 
249- #ifdef  Py_REF_DEBUG 
250-     _Py_RefTotal -- ;
251- #endif 
252297    return  -1 ;
253298}
254299
@@ -2010,21 +2055,33 @@ _PyTypes_FiniTypes(PyInterpreterState *interp)
20102055}
20112056
20122057
2013- void 
2014- _Py_NewReference (PyObject  * op )
2058+ static   inline   void 
2059+ new_reference (PyObject  * op )
20152060{
20162061    if  (_PyRuntime .tracemalloc .config .tracing ) {
20172062        _PyTraceMalloc_NewReference (op );
20182063    }
2019- #ifdef  Py_REF_DEBUG 
2020-     _Py_RefTotal ++ ;
2021- #endif 
20222064    Py_SET_REFCNT (op , 1 );
20232065#ifdef  Py_TRACE_REFS 
20242066    _Py_AddToAllObjects (op , 1 );
20252067#endif 
20262068}
20272069
2070+ void 
2071+ _Py_NewReference (PyObject  * op )
2072+ {
2073+ #ifdef  Py_REF_DEBUG 
2074+     reftotal_increment ();
2075+ #endif 
2076+     new_reference (op );
2077+ }
2078+ 
2079+ void 
2080+ _Py_NewReferenceNoTotal (PyObject  * op )
2081+ {
2082+     new_reference (op );
2083+ }
2084+ 
20282085
20292086#ifdef  Py_TRACE_REFS 
20302087void 
0 commit comments