@@ -128,6 +128,15 @@ gc_decref(PyGC_Head *g)
128
128
129
129
#define GEN_HEAD (gcstate , n ) (&(gcstate)->generations[n].head)
130
130
131
+
132
+ static GCState *
133
+ get_gc_state (void )
134
+ {
135
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
136
+ return & interp -> gc ;
137
+ }
138
+
139
+
131
140
void
132
141
_PyGC_InitState (GCState * gcstate )
133
142
{
@@ -1465,8 +1474,7 @@ static PyObject *
1465
1474
gc_enable_impl (PyObject * module )
1466
1475
/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
1467
1476
{
1468
- PyThreadState * tstate = _PyThreadState_GET ();
1469
- GCState * gcstate = & tstate -> interp -> gc ;
1477
+ GCState * gcstate = get_gc_state ();
1470
1478
gcstate -> enabled = 1 ;
1471
1479
Py_RETURN_NONE ;
1472
1480
}
@@ -1481,8 +1489,7 @@ static PyObject *
1481
1489
gc_disable_impl (PyObject * module )
1482
1490
/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
1483
1491
{
1484
- PyThreadState * tstate = _PyThreadState_GET ();
1485
- GCState * gcstate = & tstate -> interp -> gc ;
1492
+ GCState * gcstate = get_gc_state ();
1486
1493
gcstate -> enabled = 0 ;
1487
1494
Py_RETURN_NONE ;
1488
1495
}
@@ -1497,8 +1504,7 @@ static int
1497
1504
gc_isenabled_impl (PyObject * module )
1498
1505
/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
1499
1506
{
1500
- PyThreadState * tstate = _PyThreadState_GET ();
1501
- GCState * gcstate = & tstate -> interp -> gc ;
1507
+ GCState * gcstate = get_gc_state ();
1502
1508
return gcstate -> enabled ;
1503
1509
}
1504
1510
@@ -1563,8 +1569,7 @@ static PyObject *
1563
1569
gc_set_debug_impl (PyObject * module , int flags )
1564
1570
/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
1565
1571
{
1566
- PyThreadState * tstate = _PyThreadState_GET ();
1567
- GCState * gcstate = & tstate -> interp -> gc ;
1572
+ GCState * gcstate = get_gc_state ();
1568
1573
gcstate -> debug = flags ;
1569
1574
Py_RETURN_NONE ;
1570
1575
}
@@ -1579,8 +1584,7 @@ static int
1579
1584
gc_get_debug_impl (PyObject * module )
1580
1585
/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
1581
1586
{
1582
- PyThreadState * tstate = _PyThreadState_GET ();
1583
- GCState * gcstate = & tstate -> interp -> gc ;
1587
+ GCState * gcstate = get_gc_state ();
1584
1588
return gcstate -> debug ;
1585
1589
}
1586
1590
@@ -1593,8 +1597,7 @@ PyDoc_STRVAR(gc_set_thresh__doc__,
1593
1597
static PyObject *
1594
1598
gc_set_threshold (PyObject * self , PyObject * args )
1595
1599
{
1596
- PyThreadState * tstate = _PyThreadState_GET ();
1597
- GCState * gcstate = & tstate -> interp -> gc ;
1600
+ GCState * gcstate = get_gc_state ();
1598
1601
if (!PyArg_ParseTuple (args , "i|ii:set_threshold" ,
1599
1602
& gcstate -> generations [0 ].threshold ,
1600
1603
& gcstate -> generations [1 ].threshold ,
@@ -1617,8 +1620,7 @@ static PyObject *
1617
1620
gc_get_threshold_impl (PyObject * module )
1618
1621
/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
1619
1622
{
1620
- PyThreadState * tstate = _PyThreadState_GET ();
1621
- GCState * gcstate = & tstate -> interp -> gc ;
1623
+ GCState * gcstate = get_gc_state ();
1622
1624
return Py_BuildValue ("(iii)" ,
1623
1625
gcstate -> generations [0 ].threshold ,
1624
1626
gcstate -> generations [1 ].threshold ,
@@ -1635,8 +1637,7 @@ static PyObject *
1635
1637
gc_get_count_impl (PyObject * module )
1636
1638
/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
1637
1639
{
1638
- PyThreadState * tstate = _PyThreadState_GET ();
1639
- GCState * gcstate = & tstate -> interp -> gc ;
1640
+ GCState * gcstate = get_gc_state ();
1640
1641
return Py_BuildValue ("(iii)" ,
1641
1642
gcstate -> generations [0 ].count ,
1642
1643
gcstate -> generations [1 ].count ,
@@ -1679,15 +1680,13 @@ Return the list of objects that directly refer to any of objs.");
1679
1680
static PyObject *
1680
1681
gc_get_referrers (PyObject * self , PyObject * args )
1681
1682
{
1682
- PyThreadState * tstate = _PyThreadState_GET ();
1683
- int i ;
1684
1683
PyObject * result = PyList_New (0 );
1685
1684
if (!result ) {
1686
1685
return NULL ;
1687
1686
}
1688
1687
1689
- GCState * gcstate = & tstate -> interp -> gc ;
1690
- for (i = 0 ; i < NUM_GENERATIONS ; i ++ ) {
1688
+ GCState * gcstate = get_gc_state () ;
1689
+ for (int i = 0 ; i < NUM_GENERATIONS ; i ++ ) {
1691
1690
if (!(gc_referrers_for (args , GEN_HEAD (gcstate , i ), result ))) {
1692
1691
Py_DECREF (result );
1693
1692
return NULL ;
@@ -1806,11 +1805,10 @@ gc_get_stats_impl(PyObject *module)
1806
1805
{
1807
1806
int i ;
1808
1807
struct gc_generation_stats stats [NUM_GENERATIONS ], * st ;
1809
- PyThreadState * tstate = _PyThreadState_GET ();
1810
1808
1811
1809
/* To get consistent values despite allocations while constructing
1812
1810
the result list, we use a snapshot of the running stats. */
1813
- GCState * gcstate = & tstate -> interp -> gc ;
1811
+ GCState * gcstate = get_gc_state () ;
1814
1812
for (i = 0 ; i < NUM_GENERATIONS ; i ++ ) {
1815
1813
stats [i ] = gcstate -> generation_stats [i ];
1816
1814
}
@@ -1901,8 +1899,7 @@ static PyObject *
1901
1899
gc_freeze_impl (PyObject * module )
1902
1900
/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1903
1901
{
1904
- PyThreadState * tstate = _PyThreadState_GET ();
1905
- GCState * gcstate = & tstate -> interp -> gc ;
1902
+ GCState * gcstate = get_gc_state ();
1906
1903
for (int i = 0 ; i < NUM_GENERATIONS ; ++ i ) {
1907
1904
gc_list_merge (GEN_HEAD (gcstate , i ), & gcstate -> permanent_generation .head );
1908
1905
gcstate -> generations [i ].count = 0 ;
@@ -1922,8 +1919,7 @@ static PyObject *
1922
1919
gc_unfreeze_impl (PyObject * module )
1923
1920
/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1924
1921
{
1925
- PyThreadState * tstate = _PyThreadState_GET ();
1926
- GCState * gcstate = & tstate -> interp -> gc ;
1922
+ GCState * gcstate = get_gc_state ();
1927
1923
gc_list_merge (& gcstate -> permanent_generation .head ,
1928
1924
GEN_HEAD (gcstate , NUM_GENERATIONS - 1 ));
1929
1925
Py_RETURN_NONE ;
@@ -1939,8 +1935,7 @@ static Py_ssize_t
1939
1935
gc_get_freeze_count_impl (PyObject * module )
1940
1936
/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
1941
1937
{
1942
- PyThreadState * tstate = _PyThreadState_GET ();
1943
- GCState * gcstate = & tstate -> interp -> gc ;
1938
+ GCState * gcstate = get_gc_state ();
1944
1939
return gc_list_size (& gcstate -> permanent_generation .head );
1945
1940
}
1946
1941
@@ -2006,8 +2001,7 @@ static struct PyModuleDef gcmodule = {
2006
2001
PyMODINIT_FUNC
2007
2002
PyInit_gc (void )
2008
2003
{
2009
- PyThreadState * tstate = _PyThreadState_GET ();
2010
- GCState * gcstate = & tstate -> interp -> gc ;
2004
+ GCState * gcstate = get_gc_state ();
2011
2005
2012
2006
PyObject * m = PyModule_Create (& gcmodule );
2013
2007
@@ -2316,8 +2310,7 @@ PyObject_GC_Del(void *op)
2316
2310
if (_PyObject_GC_IS_TRACKED (op )) {
2317
2311
gc_list_remove (g );
2318
2312
}
2319
- PyThreadState * tstate = _PyThreadState_GET ();
2320
- GCState * gcstate = & tstate -> interp -> gc ;
2313
+ GCState * gcstate = get_gc_state ();
2321
2314
if (gcstate -> generations [0 ].count > 0 ) {
2322
2315
gcstate -> generations [0 ].count -- ;
2323
2316
}
0 commit comments