@@ -1628,12 +1628,12 @@ PyTypeObject PyAsyncGen_Type = {
16281628};
16291629
16301630
1631- #if _PyAsyncGen_MAXFREELIST > 0
1631+ #ifdef WITH_FREELISTS
16321632static struct _Py_async_gen_state *
16331633get_async_gen_state (void )
16341634{
1635- PyInterpreterState * interp = _PyInterpreterState_GET ();
1636- return & interp -> async_gen ;
1635+ _PyFreeListState * state = _PyFreeListState_GET ();
1636+ return & state -> async_gen_state ;
16371637}
16381638#endif
16391639
@@ -1656,36 +1656,36 @@ PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
16561656
16571657
16581658void
1659- _PyAsyncGen_ClearFreeLists (PyInterpreterState * interp )
1659+ _PyAsyncGen_ClearFreeLists (_PyFreeListState * freelist_state , int is_finalization )
16601660{
1661- #if _PyAsyncGen_MAXFREELIST > 0
1662- struct _Py_async_gen_state * state = & interp -> async_gen ;
1661+ #ifdef WITH_FREELISTS
1662+ struct _Py_async_gen_state * state = & freelist_state -> async_gen_state ;
16631663
1664- while (state -> value_numfree ) {
1664+ while (state -> value_numfree > 0 ) {
16651665 _PyAsyncGenWrappedValue * o ;
16661666 o = state -> value_freelist [-- state -> value_numfree ];
16671667 assert (_PyAsyncGenWrappedValue_CheckExact (o ));
16681668 PyObject_GC_Del (o );
16691669 }
16701670
1671- while (state -> asend_numfree ) {
1671+ while (state -> asend_numfree > 0 ) {
16721672 PyAsyncGenASend * o ;
16731673 o = state -> asend_freelist [-- state -> asend_numfree ];
16741674 assert (Py_IS_TYPE (o , & _PyAsyncGenASend_Type ));
16751675 PyObject_GC_Del (o );
16761676 }
1677+
1678+ if (is_finalization ) {
1679+ state -> value_numfree = -1 ;
1680+ state -> asend_numfree = -1 ;
1681+ }
16771682#endif
16781683}
16791684
16801685void
1681- _PyAsyncGen_Fini (PyInterpreterState * interp )
1686+ _PyAsyncGen_Fini (_PyFreeListState * state )
16821687{
1683- _PyAsyncGen_ClearFreeLists (interp );
1684- #if defined(Py_DEBUG ) && _PyAsyncGen_MAXFREELIST > 0
1685- struct _Py_async_gen_state * state = & interp -> async_gen ;
1686- state -> value_numfree = -1 ;
1687- state -> asend_numfree = -1 ;
1688- #endif
1688+ _PyAsyncGen_ClearFreeLists (state , 1 );
16891689}
16901690
16911691
@@ -1732,13 +1732,9 @@ async_gen_asend_dealloc(PyAsyncGenASend *o)
17321732 _PyObject_GC_UNTRACK ((PyObject * )o );
17331733 Py_CLEAR (o -> ags_gen );
17341734 Py_CLEAR (o -> ags_sendval );
1735- #if _PyAsyncGen_MAXFREELIST > 0
1735+ #ifdef WITH_FREELISTS
17361736 struct _Py_async_gen_state * state = get_async_gen_state ();
1737- #ifdef Py_DEBUG
1738- // async_gen_asend_dealloc() must not be called after _PyAsyncGen_Fini()
1739- assert (state -> asend_numfree != -1 );
1740- #endif
1741- if (state -> asend_numfree < _PyAsyncGen_MAXFREELIST ) {
1737+ if (state -> asend_numfree >= 0 && state -> asend_numfree < _PyAsyncGen_MAXFREELIST ) {
17421738 assert (PyAsyncGenASend_CheckExact (o ));
17431739 _PyGC_CLEAR_FINALIZED ((PyObject * )o );
17441740 state -> asend_freelist [state -> asend_numfree ++ ] = o ;
@@ -1906,13 +1902,9 @@ static PyObject *
19061902async_gen_asend_new (PyAsyncGenObject * gen , PyObject * sendval )
19071903{
19081904 PyAsyncGenASend * o ;
1909- #if _PyAsyncGen_MAXFREELIST > 0
1905+ #ifdef WITH_FREELISTS
19101906 struct _Py_async_gen_state * state = get_async_gen_state ();
1911- #ifdef Py_DEBUG
1912- // async_gen_asend_new() must not be called after _PyAsyncGen_Fini()
1913- assert (state -> asend_numfree != -1 );
1914- #endif
1915- if (state -> asend_numfree ) {
1907+ if (state -> asend_numfree > 0 ) {
19161908 state -> asend_numfree -- ;
19171909 o = state -> asend_freelist [state -> asend_numfree ];
19181910 _Py_NewReference ((PyObject * )o );
@@ -1945,13 +1937,9 @@ async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
19451937{
19461938 _PyObject_GC_UNTRACK ((PyObject * )o );
19471939 Py_CLEAR (o -> agw_val );
1948- #if _PyAsyncGen_MAXFREELIST > 0
1940+ #ifdef WITH_FREELISTS
19491941 struct _Py_async_gen_state * state = get_async_gen_state ();
1950- #ifdef Py_DEBUG
1951- // async_gen_wrapped_val_dealloc() must not be called after _PyAsyncGen_Fini()
1952- assert (state -> value_numfree != -1 );
1953- #endif
1954- if (state -> value_numfree < _PyAsyncGen_MAXFREELIST ) {
1942+ if (state -> value_numfree >= 0 && state -> value_numfree < _PyAsyncGen_MAXFREELIST ) {
19551943 assert (_PyAsyncGenWrappedValue_CheckExact (o ));
19561944 state -> value_freelist [state -> value_numfree ++ ] = o ;
19571945 OBJECT_STAT_INC (to_freelist );
@@ -2022,13 +2010,9 @@ _PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
20222010 _PyAsyncGenWrappedValue * o ;
20232011 assert (val );
20242012
2025- #if _PyAsyncGen_MAXFREELIST > 0
2026- struct _Py_async_gen_state * state = & tstate -> interp -> async_gen ;
2027- #ifdef Py_DEBUG
2028- // _PyAsyncGenValueWrapperNew() must not be called after _PyAsyncGen_Fini()
2029- assert (state -> value_numfree != -1 );
2030- #endif
2031- if (state -> value_numfree ) {
2013+ #ifdef WITH_FREELISTS
2014+ struct _Py_async_gen_state * state = get_async_gen_state ();
2015+ if (state -> value_numfree > 0 ) {
20322016 state -> value_numfree -- ;
20332017 o = state -> value_freelist [state -> value_numfree ];
20342018 OBJECT_STAT_INC (from_freelist );
0 commit comments