@@ -128,6 +128,15 @@ gc_decref(PyGC_Head *g)
128128
129129#define GEN_HEAD (gcstate , n ) (&(gcstate)->generations[n].head)
130130
131+
132+ static GCState *
133+ get_gc_state (void )
134+ {
135+ PyInterpreterState * interp = _PyInterpreterState_GET ();
136+ return & interp -> gc ;
137+ }
138+
139+
131140void
132141_PyGC_InitState (GCState * gcstate )
133142{
@@ -1465,8 +1474,7 @@ static PyObject *
14651474gc_enable_impl (PyObject * module )
14661475/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
14671476{
1468- PyThreadState * tstate = _PyThreadState_GET ();
1469- GCState * gcstate = & tstate -> interp -> gc ;
1477+ GCState * gcstate = get_gc_state ();
14701478 gcstate -> enabled = 1 ;
14711479 Py_RETURN_NONE ;
14721480}
@@ -1481,8 +1489,7 @@ static PyObject *
14811489gc_disable_impl (PyObject * module )
14821490/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
14831491{
1484- PyThreadState * tstate = _PyThreadState_GET ();
1485- GCState * gcstate = & tstate -> interp -> gc ;
1492+ GCState * gcstate = get_gc_state ();
14861493 gcstate -> enabled = 0 ;
14871494 Py_RETURN_NONE ;
14881495}
@@ -1497,8 +1504,7 @@ static int
14971504gc_isenabled_impl (PyObject * module )
14981505/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
14991506{
1500- PyThreadState * tstate = _PyThreadState_GET ();
1501- GCState * gcstate = & tstate -> interp -> gc ;
1507+ GCState * gcstate = get_gc_state ();
15021508 return gcstate -> enabled ;
15031509}
15041510
@@ -1563,8 +1569,7 @@ static PyObject *
15631569gc_set_debug_impl (PyObject * module , int flags )
15641570/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
15651571{
1566- PyThreadState * tstate = _PyThreadState_GET ();
1567- GCState * gcstate = & tstate -> interp -> gc ;
1572+ GCState * gcstate = get_gc_state ();
15681573 gcstate -> debug = flags ;
15691574 Py_RETURN_NONE ;
15701575}
@@ -1579,8 +1584,7 @@ static int
15791584gc_get_debug_impl (PyObject * module )
15801585/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
15811586{
1582- PyThreadState * tstate = _PyThreadState_GET ();
1583- GCState * gcstate = & tstate -> interp -> gc ;
1587+ GCState * gcstate = get_gc_state ();
15841588 return gcstate -> debug ;
15851589}
15861590
@@ -1593,8 +1597,7 @@ PyDoc_STRVAR(gc_set_thresh__doc__,
15931597static PyObject *
15941598gc_set_threshold (PyObject * self , PyObject * args )
15951599{
1596- PyThreadState * tstate = _PyThreadState_GET ();
1597- GCState * gcstate = & tstate -> interp -> gc ;
1600+ GCState * gcstate = get_gc_state ();
15981601 if (!PyArg_ParseTuple (args , "i|ii:set_threshold" ,
15991602 & gcstate -> generations [0 ].threshold ,
16001603 & gcstate -> generations [1 ].threshold ,
@@ -1617,8 +1620,7 @@ static PyObject *
16171620gc_get_threshold_impl (PyObject * module )
16181621/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
16191622{
1620- PyThreadState * tstate = _PyThreadState_GET ();
1621- GCState * gcstate = & tstate -> interp -> gc ;
1623+ GCState * gcstate = get_gc_state ();
16221624 return Py_BuildValue ("(iii)" ,
16231625 gcstate -> generations [0 ].threshold ,
16241626 gcstate -> generations [1 ].threshold ,
@@ -1635,8 +1637,7 @@ static PyObject *
16351637gc_get_count_impl (PyObject * module )
16361638/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
16371639{
1638- PyThreadState * tstate = _PyThreadState_GET ();
1639- GCState * gcstate = & tstate -> interp -> gc ;
1640+ GCState * gcstate = get_gc_state ();
16401641 return Py_BuildValue ("(iii)" ,
16411642 gcstate -> generations [0 ].count ,
16421643 gcstate -> generations [1 ].count ,
@@ -1679,15 +1680,13 @@ Return the list of objects that directly refer to any of objs.");
16791680static PyObject *
16801681gc_get_referrers (PyObject * self , PyObject * args )
16811682{
1682- PyThreadState * tstate = _PyThreadState_GET ();
1683- int i ;
16841683 PyObject * result = PyList_New (0 );
16851684 if (!result ) {
16861685 return NULL ;
16871686 }
16881687
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 ++ ) {
16911690 if (!(gc_referrers_for (args , GEN_HEAD (gcstate , i ), result ))) {
16921691 Py_DECREF (result );
16931692 return NULL ;
@@ -1806,11 +1805,10 @@ gc_get_stats_impl(PyObject *module)
18061805{
18071806 int i ;
18081807 struct gc_generation_stats stats [NUM_GENERATIONS ], * st ;
1809- PyThreadState * tstate = _PyThreadState_GET ();
18101808
18111809 /* To get consistent values despite allocations while constructing
18121810 the result list, we use a snapshot of the running stats. */
1813- GCState * gcstate = & tstate -> interp -> gc ;
1811+ GCState * gcstate = get_gc_state () ;
18141812 for (i = 0 ; i < NUM_GENERATIONS ; i ++ ) {
18151813 stats [i ] = gcstate -> generation_stats [i ];
18161814 }
@@ -1901,8 +1899,7 @@ static PyObject *
19011899gc_freeze_impl (PyObject * module )
19021900/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
19031901{
1904- PyThreadState * tstate = _PyThreadState_GET ();
1905- GCState * gcstate = & tstate -> interp -> gc ;
1902+ GCState * gcstate = get_gc_state ();
19061903 for (int i = 0 ; i < NUM_GENERATIONS ; ++ i ) {
19071904 gc_list_merge (GEN_HEAD (gcstate , i ), & gcstate -> permanent_generation .head );
19081905 gcstate -> generations [i ].count = 0 ;
@@ -1922,8 +1919,7 @@ static PyObject *
19221919gc_unfreeze_impl (PyObject * module )
19231920/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
19241921{
1925- PyThreadState * tstate = _PyThreadState_GET ();
1926- GCState * gcstate = & tstate -> interp -> gc ;
1922+ GCState * gcstate = get_gc_state ();
19271923 gc_list_merge (& gcstate -> permanent_generation .head ,
19281924 GEN_HEAD (gcstate , NUM_GENERATIONS - 1 ));
19291925 Py_RETURN_NONE ;
@@ -1939,8 +1935,7 @@ static Py_ssize_t
19391935gc_get_freeze_count_impl (PyObject * module )
19401936/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
19411937{
1942- PyThreadState * tstate = _PyThreadState_GET ();
1943- GCState * gcstate = & tstate -> interp -> gc ;
1938+ GCState * gcstate = get_gc_state ();
19441939 return gc_list_size (& gcstate -> permanent_generation .head );
19451940}
19461941
@@ -2006,8 +2001,7 @@ static struct PyModuleDef gcmodule = {
20062001PyMODINIT_FUNC
20072002PyInit_gc (void )
20082003{
2009- PyThreadState * tstate = _PyThreadState_GET ();
2010- GCState * gcstate = & tstate -> interp -> gc ;
2004+ GCState * gcstate = get_gc_state ();
20112005
20122006 PyObject * m = PyModule_Create (& gcmodule );
20132007
@@ -2316,8 +2310,7 @@ PyObject_GC_Del(void *op)
23162310 if (_PyObject_GC_IS_TRACKED (op )) {
23172311 gc_list_remove (g );
23182312 }
2319- PyThreadState * tstate = _PyThreadState_GET ();
2320- GCState * gcstate = & tstate -> interp -> gc ;
2313+ GCState * gcstate = get_gc_state ();
23212314 if (gcstate -> generations [0 ].count > 0 ) {
23222315 gcstate -> generations [0 ].count -- ;
23232316 }
0 commit comments