Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2b11ec

Browse files
committed
Add IS_TRACKED and IS_MOVED macros. This makes the logic a little more clear.
1 parent 0ebac97 commit a2b11ec

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

Modules/gcmodule.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
/* Get the object given the GC head */
2929
#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
3030

31+
/* True if an object is tracked by the GC */
32+
#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_next != NULL)
3133

3234
/*** Global GC state ***/
3335

@@ -73,6 +75,9 @@ static int debug;
7375
/* Special gc_refs value */
7476
#define GC_MOVED -123
7577

78+
/* True if an object has been moved to the older generation */
79+
#define IS_MOVED(o) ((AS_GC(o))->gc.gc_refs == GC_MOVED)
80+
7681
/* list of uncollectable objects */
7782
static PyObject *garbage;
7883

@@ -170,8 +175,7 @@ static int
170175
visit_decref(PyObject *op, void *data)
171176
{
172177
if (op && PyObject_IS_GC(op)) {
173-
PyGC_Head *gc = AS_GC(op);
174-
if (gc->gc.gc_next != NULL)
178+
if (IS_TRACKED(op))
175179
AS_GC(op)->gc.gc_refs--;
176180
}
177181
return 0;
@@ -212,8 +216,8 @@ static int
212216
visit_move(PyObject *op, PyGC_Head *tolist)
213217
{
214218
if (PyObject_IS_GC(op)) {
215-
PyGC_Head *gc = AS_GC(op);
216-
if (gc->gc.gc_next != NULL && gc->gc.gc_refs != GC_MOVED) {
219+
if (IS_TRACKED(op) && !IS_MOVED(op)) {
220+
PyGC_Head *gc = AS_GC(op);
217221
gc_list_remove(gc);
218222
gc_list_append(gc, tolist);
219223
gc->gc.gc_refs = GC_MOVED;
@@ -856,8 +860,7 @@ void
856860
PyObject_GC_UnTrack(void *op)
857861
{
858862
#ifdef WITH_CYCLE_GC
859-
PyGC_Head *gc = AS_GC(op);
860-
if (gc->gc.gc_next != NULL)
863+
if (IS_TRACKED(op))
861864
_PyObject_GC_UNTRACK(op);
862865
#endif
863866
}
@@ -941,7 +944,7 @@ PyObject_GC_Del(void *op)
941944
{
942945
#ifdef WITH_CYCLE_GC
943946
PyGC_Head *g = AS_GC(op);
944-
if (g->gc.gc_next != NULL)
947+
if (IS_TRACKED(op))
945948
gc_list_remove(g);
946949
if (generations[0].count > 0) {
947950
generations[0].count--;

0 commit comments

Comments
 (0)