bpo-39542: Simplify _Py_NewReference() (GH-18332)

* Remove _Py_INC_REFTOTAL and _Py_DEC_REFTOTAL macros: modify
  directly _Py_RefTotal.
* _Py_ForgetReference() is no longer defined if the Py_TRACE_REFS
  macro is not defined.
* Remove _Py_NewReference() implementation from object.c:
  unify the two implementations in object.h inline function.
* Fix Py_TRACE_REFS build: _Py_INC_TPALLOCS() macro has been removed.
This commit is contained in:
Victor Stinner 2020-02-03 17:55:05 +01:00 committed by GitHub
parent 24e5ad4689
commit 49932fec62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 58 deletions

View file

@ -379,25 +379,11 @@ decision that's up to the implementer of each new type so if you want,
you can count such references to the type object.)
*/
/* First define a pile of simple helper macros, one set per special
* build symbol. These either expand to the obvious things, or to
* nothing at all when the special mode isn't in effect. The main
* macros can later be defined just once then, yet expand to different
* things depending on which special build options are and aren't in effect.
* Trust me <wink>: while painful, this is 20x easier to understand than,
* e.g, defining _Py_NewReference five different times in a maze of nested
* #ifdefs (we used to do that -- it was impenetrable).
*/
#ifdef Py_REF_DEBUG
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
PyObject *op);
PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
#define _Py_INC_REFTOTAL _Py_RefTotal++
#define _Py_DEC_REFTOTAL _Py_RefTotal--
#else
#define _Py_INC_REFTOTAL
#define _Py_DEC_REFTOTAL
#endif /* Py_REF_DEBUG */
/* Update the Python traceback of an object. This function must be called
@ -406,33 +392,33 @@ PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
#ifdef Py_TRACE_REFS
/* Py_TRACE_REFS is such major surgery that we call external routines. */
PyAPI_FUNC(void) _Py_NewReference(PyObject *);
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
#else
/* Without Py_TRACE_REFS, there's little enough to do that we expand code
inline. */
#endif
static inline void _Py_NewReference(PyObject *op)
{
if (_Py_tracemalloc_config.tracing) {
_PyTraceMalloc_NewReference(op);
}
_Py_INC_REFTOTAL;
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
#endif
Py_REFCNT(op) = 1;
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op, 1);
#endif
}
static inline void _Py_ForgetReference(PyObject *Py_UNUSED(op))
{
/* nothing to do */
}
#endif /* !Py_TRACE_REFS */
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
static inline void _Py_INCREF(PyObject *op)
{
_Py_INC_REFTOTAL;
#ifdef Py_REF_DEBUG
_Py_RefTotal++;
#endif
op->ob_refcnt++;
}
@ -444,7 +430,9 @@ static inline void _Py_DECREF(
#endif
PyObject *op)
{
_Py_DEC_REFTOTAL;
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
if (--op->ob_refcnt != 0) {
#ifdef Py_REF_DEBUG
if (op->ob_refcnt < 0) {