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

Skip to content

Commit 4cc6ac7

Browse files
committed
Neil Schemenauer: small fixes for GC
1 parent ce8e1dc commit 4cc6ac7

7 files changed

Lines changed: 17 additions & 13 deletions

File tree

Include/objimpl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
183183
(PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
184184
(typeobj), (n)) )
185185

186+
#define PyObject_DEL(op) PyObject_FREE(op)
187+
186188
/* This example code implements an object constructor with a custom
187189
allocator, where PyObject_New is inlined, and shows the important
188190
distinction between two steps (at least):
@@ -221,7 +223,7 @@ extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
221223
PyObject_{New, VarNew, Del} to manage the memory. Set the type flag
222224
Py_TPFLAGS_GC and define the type method tp_recurse. You should also
223225
add the method tp_clear if your object is mutable. Include
224-
PyGC_INFO_SIZE in the calculation of tp_basicsize. Call
226+
PyGC_HEAD_SIZE in the calculation of tp_basicsize. Call
225227
PyObject_GC_Init after the pointers followed by tp_recurse become
226228
valid (usually just before returning the object from the allocation
227229
method. Call PyObject_GC_Fini before those pointers become invalid
@@ -234,7 +236,6 @@ extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
234236
#define PyObject_GC_Fini(op)
235237
#define PyObject_AS_GC(op) (op)
236238
#define PyObject_FROM_GC(op) (op)
237-
#define PyObject_DEL(op) PyObject_FREE(op)
238239

239240
#else
240241

@@ -268,10 +269,6 @@ typedef struct _gc_head {
268269
/* Get the object given the PyGC_Head */
269270
#define PyObject_FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
270271

271-
#define PyObject_DEL(op) PyObject_FREE( PyObject_IS_GC(op) ? \
272-
(ANY *)PyObject_AS_GC(op) : \
273-
(ANY *)(op) )
274-
275272
#endif /* WITH_CYCLE_GC */
276273

277274
#ifdef __cplusplus

Objects/classobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class_dealloc(op)
128128
Py_XDECREF(op->cl_getattr);
129129
Py_XDECREF(op->cl_setattr);
130130
Py_XDECREF(op->cl_delattr);
131+
op = (PyClassObject *) PyObject_AS_GC(op);
131132
PyObject_DEL(op);
132133
}
133134

@@ -473,6 +474,7 @@ PyInstance_New(class, arg, kw)
473474
inst->in_dict = PyDict_New();
474475
PyObject_GC_Init(inst);
475476
if (inst->in_dict == NULL) {
477+
inst = (PyInstanceObject *) PyObject_AS_GC(inst);
476478
PyObject_DEL(inst);
477479
return NULL;
478480
}
@@ -588,6 +590,7 @@ instance_dealloc(inst)
588590
#endif /* Py_TRACE_REFS */
589591
Py_DECREF(inst->in_class);
590592
Py_XDECREF(inst->in_dict);
593+
inst = (PyInstanceObject *) PyObject_AS_GC(inst);
591594
PyObject_DEL(inst);
592595
}
593596

@@ -1763,6 +1766,7 @@ PyMethod_Fini()
17631766
while (free_list) {
17641767
PyMethodObject *im = free_list;
17651768
free_list = (PyMethodObject *)(im->im_self);
1769+
im = (PyMethodObject *) PyObject_AS_GC(im);
17661770
PyObject_DEL(im);
17671771
}
17681772
}

Objects/dictobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ dict_dealloc(mp)
472472
}
473473
if (mp->ma_table != NULL)
474474
PyMem_DEL(mp->ma_table);
475+
mp = (dictobject *) PyObject_AS_GC(mp);
475476
PyObject_DEL(mp);
476477
Py_TRASHCAN_SAFE_END(mp)
477478
}

Objects/funcobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func_dealloc(op)
172172
Py_DECREF(op->func_name);
173173
Py_XDECREF(op->func_defaults);
174174
Py_XDECREF(op->func_doc);
175+
op = (PyFunctionObject *) PyObject_AS_GC(op);
175176
PyObject_DEL(op);
176177
}
177178

Objects/listobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ list_dealloc(op)
209209
}
210210
PyMem_FREE(op->ob_item);
211211
}
212+
op = (PyListObject *) PyObject_AS_GC(op);
212213
PyObject_DEL(op);
213214
Py_TRASHCAN_SAFE_END(op)
214215
}

Objects/object.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,11 @@ _PyObject_Del(op)
171171
PyObject *op;
172172
{
173173
#ifdef WITH_CYCLE_GC
174-
if (PyType_IS_GC(op->ob_type)) {
175-
PyGC_Head *g = PyObject_AS_GC(op);
176-
PyObject_FREE(g);
177-
} else
178-
#endif
179-
{
180-
PyObject_FREE(op);
174+
if (op && PyType_IS_GC(op->ob_type)) {
175+
op = (PyObject *) PyObject_AS_GC(op);
181176
}
177+
#endif
178+
PyObject_FREE(op);
182179
}
183180

184181
#ifndef WITH_CYCLE_GC

Objects/tupleobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ tupledealloc(op)
175175
}
176176
#endif
177177
}
178+
op = (PyTupleObject *) PyObject_AS_GC(op);
178179
PyObject_DEL(op);
179180
done:
180181
Py_TRASHCAN_SAFE_END(op)
@@ -559,6 +560,7 @@ _PyTuple_Resize(pv, newsize, last_is_sticky)
559560
*pv = (PyObject *) sv;
560561
if (sv == NULL) {
561562
PyObject_GC_Init((PyObject *)v);
563+
v = (PyTupleObject *) PyObject_AS_GC(v);
562564
PyObject_DEL(v);
563565
PyErr_NoMemory();
564566
return -1;
@@ -595,6 +597,7 @@ PyTuple_Fini()
595597
while (p) {
596598
q = p;
597599
p = (PyTupleObject *)(p->ob_item[0]);
600+
q = (PyTupleObject *) PyObject_AS_GC(q);
598601
PyObject_DEL(q);
599602
}
600603
}

0 commit comments

Comments
 (0)