@@ -64,12 +64,12 @@ static int
6464contextvar_del (PyContextVar * var );
6565
6666
67- #if PyContext_MAXFREELIST > 0
67+ #ifdef WITH_FREELISTS
6868static struct _Py_context_state *
6969get_context_state (void )
7070{
71- PyInterpreterState * interp = _PyInterpreterState_GET ();
72- return & interp -> context ;
71+ _PyFreeListState * state = _PyFreeListState_GET ();
72+ return & state -> context_state ;
7373}
7474#endif
7575
@@ -340,13 +340,9 @@ static inline PyContext *
340340_context_alloc (void )
341341{
342342 PyContext * ctx ;
343- #if PyContext_MAXFREELIST > 0
343+ #ifdef WITH_FREELISTS
344344 struct _Py_context_state * state = get_context_state ();
345- #ifdef Py_DEBUG
346- // _context_alloc() must not be called after _PyContext_Fini()
347- assert (state -> numfree != -1 );
348- #endif
349- if (state -> numfree ) {
345+ if (state -> numfree > 0 ) {
350346 state -> numfree -- ;
351347 ctx = state -> freelist ;
352348 state -> freelist = (PyContext * )ctx -> ctx_weakreflist ;
@@ -471,13 +467,9 @@ context_tp_dealloc(PyContext *self)
471467 }
472468 (void )context_tp_clear (self );
473469
474- #if PyContext_MAXFREELIST > 0
470+ #ifdef WITH_FREELISTS
475471 struct _Py_context_state * state = get_context_state ();
476- #ifdef Py_DEBUG
477- // _context_alloc() must not be called after _PyContext_Fini()
478- assert (state -> numfree != -1 );
479- #endif
480- if (state -> numfree < PyContext_MAXFREELIST ) {
472+ if (state -> numfree >= 0 && state -> numfree < PyContext_MAXFREELIST ) {
481473 state -> numfree ++ ;
482474 self -> ctx_weakreflist = (PyObject * )state -> freelist ;
483475 state -> freelist = self ;
@@ -1275,28 +1267,27 @@ get_token_missing(void)
12751267
12761268
12771269void
1278- _PyContext_ClearFreeList (PyInterpreterState * interp )
1270+ _PyContext_ClearFreeList (_PyFreeListState * freelist_state , int is_finalization )
12791271{
1280- #if PyContext_MAXFREELIST > 0
1281- struct _Py_context_state * state = & interp -> context ;
1282- for (; state -> numfree ; state -> numfree -- ) {
1272+ #ifdef WITH_FREELISTS
1273+ struct _Py_context_state * state = & freelist_state -> context_state ;
1274+ for (; state -> numfree > 0 ; state -> numfree -- ) {
12831275 PyContext * ctx = state -> freelist ;
12841276 state -> freelist = (PyContext * )ctx -> ctx_weakreflist ;
12851277 ctx -> ctx_weakreflist = NULL ;
12861278 PyObject_GC_Del (ctx );
12871279 }
1280+ if (is_finalization ) {
1281+ state -> numfree = -1 ;
1282+ }
12881283#endif
12891284}
12901285
12911286
12921287void
1293- _PyContext_Fini (PyInterpreterState * interp )
1288+ _PyContext_Fini (_PyFreeListState * state )
12941289{
1295- _PyContext_ClearFreeList (interp );
1296- #if defined(Py_DEBUG ) && PyContext_MAXFREELIST > 0
1297- struct _Py_context_state * state = & interp -> context ;
1298- state -> numfree = -1 ;
1299- #endif
1290+ _PyContext_ClearFreeList (state , 1 );
13001291}
13011292
13021293
0 commit comments