Skip to content

Commit 3c09dca

Browse files
authored
bpo-35059: Convert _Py_Dealloc() to static inline function (GH-10223)
Convert _Py_Dealloc() macro into a static inline function. Moreover, it is now also defined as a static inline function if Py_TRACE_REFS is defined.
1 parent e1b2995 commit 3c09dca

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

Include/object.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
768768
/* Py_TRACE_REFS is such major surgery that we call external routines. */
769769
PyAPI_FUNC(void) _Py_NewReference(PyObject *);
770770
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
771-
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
772771
PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
773772
PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
774773
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
@@ -790,15 +789,25 @@ static inline void _Py_ForgetReference(PyObject *op)
790789
{
791790
_Py_INC_TPFREES(op);
792791
}
792+
#endif /* !Py_TRACE_REFS */
793+
793794

794-
#ifdef Py_LIMITED_API
795795
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
796+
797+
#ifndef Py_LIMITED_API
798+
static inline void _Py_Dealloc_inline(PyObject *op)
799+
{
800+
destructor dealloc = Py_TYPE(op)->tp_dealloc;
801+
#ifdef Py_TRACE_REFS
802+
_Py_ForgetReference(op);
796803
#else
797-
#define _Py_Dealloc(op) ( \
798-
_Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
799-
(*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
804+
_Py_INC_TPFREES(op);
800805
#endif
801-
#endif /* !Py_TRACE_REFS */
806+
(*dealloc)(op);
807+
}
808+
809+
# define _Py_Dealloc(op) _Py_Dealloc_inline(op)
810+
#endif /* !defined(Py_LIMITED_API) */
802811

803812

804813
static inline void _Py_INCREF(PyObject *op)

Objects/object.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,14 +1964,6 @@ _Py_ForgetReference(PyObject *op)
19641964
_Py_INC_TPFREES(op);
19651965
}
19661966

1967-
void
1968-
_Py_Dealloc(PyObject *op)
1969-
{
1970-
destructor dealloc = Py_TYPE(op)->tp_dealloc;
1971-
_Py_ForgetReference(op);
1972-
(*dealloc)(op);
1973-
}
1974-
19751967
/* Print all live objects. Because PyObject_Print is called, the
19761968
* interpreter must be in a healthy state.
19771969
*/
@@ -2265,18 +2257,20 @@ _PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
22652257
Py_FatalError("_PyObject_AssertFailed");
22662258
}
22672259

2268-
#ifndef Py_TRACE_REFS
2269-
/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2270-
Define this here, so we can undefine the macro. */
2260+
22712261
#undef _Py_Dealloc
2272-
void _Py_Dealloc(PyObject *);
2262+
22732263
void
22742264
_Py_Dealloc(PyObject *op)
22752265
{
2276-
_Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2277-
(*Py_TYPE(op)->tp_dealloc)(op);
2278-
}
2266+
destructor dealloc = Py_TYPE(op)->tp_dealloc;
2267+
#ifdef Py_TRACE_REFS
2268+
_Py_ForgetReference(op);
2269+
#else
2270+
_Py_INC_TPFREES(op);
22792271
#endif
2272+
(*dealloc)(op);
2273+
}
22802274

22812275
#ifdef __cplusplus
22822276
}

0 commit comments

Comments
 (0)