@@ -14,10 +14,19 @@ extern "C" {
1414#include "pycore_interp.h" // PyInterpreterState.gc
1515#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED
1616#include "pycore_pystate.h" // _PyInterpreterState_GET()
17+ #include "pycore_typeid.h" // _PyType_IncrefSlow
1718
1819
1920#define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1)
2021
22+ // This value is added to `ob_ref_shared` for objects that use deferred
23+ // reference counting so that they are not immediately deallocated when the
24+ // non-deferred reference count drops to zero.
25+ //
26+ // The value is half the maximum shared refcount because the low two bits of
27+ // `ob_ref_shared` are used for flags.
28+ #define _Py_REF_DEFERRED (PY_SSIZE_T_MAX / 8)
29+
2130// gh-121528, gh-118997: Similar to _Py_IsImmortal() but be more loose when
2231// comparing the reference count to stay compatible with C extensions built
2332// with the stable ABI 3.11 or older. Such extensions implement INCREF/DECREF
@@ -280,6 +289,64 @@ extern PyStatus _PyObject_InitState(PyInterpreterState *interp);
280289extern void _PyObject_FiniState (PyInterpreterState * interp );
281290extern bool _PyRefchain_IsTraced (PyInterpreterState * interp , PyObject * obj );
282291
292+ static inline void
293+ _Py_INCREF_TYPE (PyTypeObject * type )
294+ {
295+ #ifndef Py_GIL_DISABLED
296+ Py_INCREF (type );
297+ #else
298+ if (!_PyType_HasFeature (type , Py_TPFLAGS_HEAPTYPE )) {
299+ assert (_Py_IsImmortal (type ));
300+ return ;
301+ }
302+
303+ _PyThreadStateImpl * tstate = (_PyThreadStateImpl * )_PyThreadState_GET ();
304+ PyHeapTypeObject * ht = (PyHeapTypeObject * )type ;
305+
306+ // Unsigned comparison so that `ht_id=-1` is treated as out-of-bounds.
307+ Py_ssize_t ht_id = ht -> _ht_id ;
308+ if ((size_t )ht_id >= (size_t )tstate -> types .size ) {
309+ _PyType_IncrefSlow (ht );
310+ }
311+ else {
312+ # ifdef Py_REF_DEBUG
313+ _Py_INCREF_IncRefTotal ();
314+ # endif
315+ _Py_INCREF_STAT_INC ();
316+ tstate -> types .refcounts [ht_id ]++ ;
317+ }
318+ #endif
319+ }
320+
321+ static inline void
322+ _Py_DECREF_TYPE (PyTypeObject * type )
323+ {
324+ #ifndef Py_GIL_DISABLED
325+ Py_DECREF (type );
326+ #else
327+ if (!_PyType_HasFeature (type , Py_TPFLAGS_HEAPTYPE )) {
328+ assert (_Py_IsImmortal (type ));
329+ return ;
330+ }
331+
332+ _PyThreadStateImpl * tstate = (_PyThreadStateImpl * )_PyThreadState_GET ();
333+ PyHeapTypeObject * ht = (PyHeapTypeObject * )type ;
334+
335+ // Unsigned comparison so that `ht_id=-1` is treated as out-of-bounds.
336+ Py_ssize_t ht_id = ht -> _ht_id ;
337+ if ((size_t )ht_id >= (size_t )tstate -> types .size ) {
338+ Py_DECREF (type );
339+ }
340+ else {
341+ # ifdef Py_REF_DEBUG
342+ _Py_DECREF_DecRefTotal ();
343+ # endif
344+ _Py_DECREF_STAT_INC ();
345+ tstate -> types .refcounts [ht_id ]-- ;
346+ }
347+ #endif
348+ }
349+
283350/* Inline functions trading binary compatibility for speed:
284351 _PyObject_Init() is the fast version of PyObject_Init(), and
285352 _PyObject_InitVar() is the fast version of PyObject_InitVar().
@@ -291,7 +358,7 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj)
291358 assert (op != NULL );
292359 Py_SET_TYPE (op , typeobj );
293360 assert (_PyType_HasFeature (typeobj , Py_TPFLAGS_HEAPTYPE ) || _Py_IsImmortalLoose (typeobj ));
294- Py_INCREF (typeobj );
361+ _Py_INCREF_TYPE (typeobj );
295362 _Py_NewReference (op );
296363}
297364
0 commit comments