bpo-35059: Convert Py_INCREF() to static inline function (GH-10079) · python/cpython@2aaf0c1
@@ -735,11 +735,7 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
735735#define _Py_INC_REFTOTAL _Py_RefTotal++
736736#define _Py_DEC_REFTOTAL _Py_RefTotal--
737737#define _Py_REF_DEBUG_COMMA ,
738-#define _Py_CHECK_REFCNT(OP) \
739-{ if (((PyObject*)OP)->ob_refcnt < 0) \
740- _Py_NegativeRefcount(__FILE__, __LINE__, \
741- (PyObject *)(OP)); \
742-}
738+743739/* Py_REF_DEBUG also controls the display of refcounts and memory block
744740 * allocations at the interactive prompt and at interpreter shutdown
745741 */
@@ -748,7 +744,6 @@ PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
748744#define _Py_INC_REFTOTAL
749745#define _Py_DEC_REFTOTAL
750746#define _Py_REF_DEBUG_COMMA
751-#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
752747#endif /* Py_REF_DEBUG */
753748754749#ifdef COUNT_ALLOCS
@@ -780,17 +775,21 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
780775781776#else
782777/* Without Py_TRACE_REFS, there's little enough to do that we expand code
783- * inline.
784- */
785-#define _Py_NewReference(op) ( \
786- (_Py_tracemalloc_config.tracing \
787- ? _PyTraceMalloc_NewReference(op) \
788- : 0), \
789- _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
790- _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
791- Py_REFCNT(op) = 1)
778+ inline. */
779+static inline void _Py_NewReference(PyObject *op)
780+{
781+if (_Py_tracemalloc_config.tracing) {
782+_PyTraceMalloc_NewReference(op);
783+ }
784+_Py_INC_TPALLOCS(op);
785+_Py_INC_REFTOTAL;
786+Py_REFCNT(op) = 1;
787+}
792788793-#define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
789+static inline void _Py_ForgetReference(PyObject *op)
790+{
791+_Py_INC_TPFREES(op);
792+}
794793795794#ifdef Py_LIMITED_API
796795PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
@@ -801,19 +800,33 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
801800#endif
802801#endif /* !Py_TRACE_REFS */
803802804-#define Py_INCREF(op) ( \
805- _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
806- ((PyObject *)(op))->ob_refcnt++)
807-808-#define Py_DECREF(op) \
809- do { \
810- PyObject *_py_decref_tmp = (PyObject *)(op); \
811- if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
812- --(_py_decref_tmp)->ob_refcnt != 0) \
813- _Py_CHECK_REFCNT(_py_decref_tmp) \
814- else \
815- _Py_Dealloc(_py_decref_tmp); \
816- } while (0)
803+804+static inline void _Py_INCREF(PyObject *op)
805+{
806+_Py_INC_REFTOTAL;
807+op->ob_refcnt++;
808+}
809+810+#define Py_INCREF(op) _Py_INCREF((PyObject *)(op))
811+812+static inline void _Py_DECREF(const char *filename, int lineno,
813+PyObject *op)
814+{
815+_Py_DEC_REFTOTAL;
816+if (--op->ob_refcnt != 0) {
817+#ifdef Py_REF_DEBUG
818+if (op->ob_refcnt < 0) {
819+_Py_NegativeRefcount(filename, lineno, op);
820+ }
821+#endif
822+ }
823+else {
824+_Py_Dealloc(op);
825+ }
826+}
827+828+#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, (PyObject *)(op))
829+817830818831/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
819832 * and tp_dealloc implementations.