@@ -64,12 +64,12 @@ static int
64
64
contextvar_del (PyContextVar * var );
65
65
66
66
67
- #if PyContext_MAXFREELIST > 0
67
+ #ifdef WITH_FREELISTS
68
68
static struct _Py_context_state *
69
69
get_context_state (void )
70
70
{
71
- PyInterpreterState * interp = _PyInterpreterState_GET ();
72
- return & interp -> context ;
71
+ _PyFreeListState * state = _PyFreeListState_GET ();
72
+ return & state -> context_state ;
73
73
}
74
74
#endif
75
75
@@ -340,13 +340,9 @@ static inline PyContext *
340
340
_context_alloc (void )
341
341
{
342
342
PyContext * ctx ;
343
- #if PyContext_MAXFREELIST > 0
343
+ #ifdef WITH_FREELISTS
344
344
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 ) {
350
346
state -> numfree -- ;
351
347
ctx = state -> freelist ;
352
348
state -> freelist = (PyContext * )ctx -> ctx_weakreflist ;
@@ -471,13 +467,9 @@ context_tp_dealloc(PyContext *self)
471
467
}
472
468
(void )context_tp_clear (self );
473
469
474
- #if PyContext_MAXFREELIST > 0
470
+ #ifdef WITH_FREELISTS
475
471
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 ) {
481
473
state -> numfree ++ ;
482
474
self -> ctx_weakreflist = (PyObject * )state -> freelist ;
483
475
state -> freelist = self ;
@@ -1275,28 +1267,27 @@ get_token_missing(void)
1275
1267
1276
1268
1277
1269
void
1278
- _PyContext_ClearFreeList (PyInterpreterState * interp )
1270
+ _PyContext_ClearFreeList (_PyFreeListState * freelist_state , int is_finalization )
1279
1271
{
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 -- ) {
1283
1275
PyContext * ctx = state -> freelist ;
1284
1276
state -> freelist = (PyContext * )ctx -> ctx_weakreflist ;
1285
1277
ctx -> ctx_weakreflist = NULL ;
1286
1278
PyObject_GC_Del (ctx );
1287
1279
}
1280
+ if (is_finalization ) {
1281
+ state -> numfree = -1 ;
1282
+ }
1288
1283
#endif
1289
1284
}
1290
1285
1291
1286
1292
1287
void
1293
- _PyContext_Fini (PyInterpreterState * interp )
1288
+ _PyContext_Fini (_PyFreeListState * state )
1294
1289
{
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 );
1300
1291
}
1301
1292
1302
1293
0 commit comments