[Patches] GC patch 3 (GC disabled)

Neil Schemenauer nascheme@enme.ucalgary.ca
Wed, 14 Jun 2000 13:39:00 -0600


--ZGiS0Q5IWpPtfppv
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

This patch adds all the changes for when WITH_CYCLE_GC is
undefined (ie. GC is disabled).  This patch does not modify any
behavior (its just macros).

    Neil

--ZGiS0Q5IWpPtfppv
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="nogc.diff"

Index: type.1/Include/objimpl.h
*** type.1/Include/objimpl.h Wed, 14 Jun 2000 13:16:32 -0600 nas (python/o/19_objimpl.h 1.1.2.1.1.1 644)
--- type.1(w)/Include/objimpl.h Wed, 14 Jun 2000 13:29:58 -0600 nas (python/o/19_objimpl.h 1.1.2.1.1.1 644)
***************
*** 234,243 ****
--- 234,261 ----
     the 1st step is performed automatically for you, so in a C++ class
     constructor you would start directly with PyObject_Init/InitVar. */
  
+ /*
+  * Garbage Collection Support
+  * ==========================
+  */
  
+ /* To make a new object participate in garbage collection use
+    PyObject_{New, VarNew, Del} to manage the memory.  Set the type flag
+    Py_TPFLAGS_GC and define the type method tp_recurse.  You should also
+    add the method tp_clear if your object is mutable.  Include
+    PyGC_INFO_SIZE in the calculation of tp_basicsize.  Call
+    PyObject_GC_Init after the pointers followed by tp_recurse become
+    valid (usually just before returning the object from the allocation
+    method.  Call PyObject_GC_Fini before those pointers become invalid
+    (usually at the top of the deallocation method).  */
  
  #ifndef WITH_CYCLE_GC
+ 
  #define PyGC_INFO_SIZE 0
+ #define PyObject_GC_DEL PyObject_DEL
+ #define PyObject_GC_Init(op)
+ #define PyObject_GC_Fini(op)
+ 
  #endif
  
  #ifdef __cplusplus
Index: type.1/Modules/cPickle.c
*** type.1/Modules/cPickle.c Mon, 05 Jun 2000 00:51:28 -0600 nas (python/C/27_cPickle.c 1.1.2.2 644)
--- type.1(w)/Modules/cPickle.c Wed, 14 Jun 2000 13:18:46 -0600 nas (python/C/27_cPickle.c 1.1.2.2 644)
***************
*** 2893,2899 ****
                  Py_DECREF(inst);
                  goto err;
                }
! 
                return (PyObject *)inst;
              }
            Py_DECREF(__getinitargs__);
--- 2893,2899 ----
                  Py_DECREF(inst);
                  goto err;
                }
!               PyObject_GC_Init((PyObject *)inst);
                return (PyObject *)inst;
              }
            Py_DECREF(__getinitargs__);
Index: type.1/Modules/newmodule.c
*** type.1/Modules/newmodule.c Mon, 08 May 2000 12:24:03 -0600 nas (python/D/13_newmodule. 1.1.2.1 644)
--- type.1(w)/Modules/newmodule.c Wed, 14 Jun 2000 13:18:46 -0600 nas (python/D/13_newmodule. 1.1.2.1 644)
***************
*** 56,61 ****
--- 56,62 ----
  	Py_INCREF(dict);
  	inst->in_class = (PyClassObject *)klass;
  	inst->in_dict = dict;
+ 	PyObject_GC_Init((PyObject *)inst);
  	return (PyObject *)inst;
  }
  
Index: type.1/Objects/classobject.c
*** type.1/Objects/classobject.c Wed, 14 Jun 2000 13:16:32 -0600 nas (python/E/16_classobjec 1.1.2.2.1.2 644)
--- type.1(w)/Objects/classobject.c Wed, 14 Jun 2000 13:33:31 -0600 nas (python/E/16_classobjec 1.1.2.2.1.2 644)
***************
*** 132,137 ****
--- 132,138 ----
  	Py_XINCREF(op->cl_getattr);
  	Py_XINCREF(op->cl_setattr);
  	Py_XINCREF(op->cl_delattr);
+ 	PyObject_GC_Init((PyObject *)op);
  	return (PyObject *) op;
  }
  
***************
*** 141,153 ****
  class_dealloc(op)
  	PyClassObject *op;
  {
  	Py_DECREF(op->cl_bases);
  	Py_DECREF(op->cl_dict);
  	Py_XDECREF(op->cl_name);
  	Py_XDECREF(op->cl_getattr);
  	Py_XDECREF(op->cl_setattr);
  	Py_XDECREF(op->cl_delattr);
! 	PyObject_DEL(op);
  }
  
  static PyObject *
--- 142,155 ----
  class_dealloc(op)
  	PyClassObject *op;
  {
+ 	PyObject_GC_Fini((PyObject *)op);
  	Py_DECREF(op->cl_bases);
  	Py_DECREF(op->cl_dict);
  	Py_XDECREF(op->cl_name);
  	Py_XDECREF(op->cl_getattr);
  	Py_XDECREF(op->cl_setattr);
  	Py_XDECREF(op->cl_delattr);
! 	PyObject_GC_DEL(op);
  }
  
  static PyObject *
***************
*** 467,472 ****
--- 469,475 ----
  	Py_INCREF(class);
  	inst->in_class = (PyClassObject *)class;
  	inst->in_dict = PyDict_New();
+ 	PyObject_GC_Init((PyObject *)inst);
  	if (inst->in_dict == NULL) {
  		Py_DECREF(inst);
  		return NULL;
***************
*** 514,524 ****
  	PyObject *error_type, *error_value, *error_traceback;
  	PyObject *del;
  	static PyObject *delstr;
  	/* Call the __del__ method if it exists.  First temporarily
  	   revive the object and save the current exception, if any. */
  #ifdef Py_TRACE_REFS
  	/* much too complicated if Py_TRACE_REFS defined */
- 	extern long _Py_RefTotal;
  	inst->ob_type = &PyInstance_Type;
  	_Py_NewReference((PyObject *)inst);
  	_Py_RefTotal--;		/* compensate for increment in NEWREF */
--- 517,528 ----
  	PyObject *error_type, *error_value, *error_traceback;
  	PyObject *del;
  	static PyObject *delstr;
+ 	extern long _Py_RefTotal;
+ 	PyObject_GC_Fini((PyObject *)inst);
  	/* Call the __del__ method if it exists.  First temporarily
  	   revive the object and save the current exception, if any. */
  #ifdef Py_TRACE_REFS
  	/* much too complicated if Py_TRACE_REFS defined */
  	inst->ob_type = &PyInstance_Type;
  	_Py_NewReference((PyObject *)inst);
  	_Py_RefTotal--;		/* compensate for increment in NEWREF */
***************
*** 566,571 ****
--- 570,576 ----
  #ifdef COUNT_ALLOCS
  		inst->ob_type->tp_free--;
  #endif
+ 		PyObject_GC_Init((PyObject *)inst);
  		return; /* __del__ added a reference; don't delete now */
  	}
  #ifdef Py_TRACE_REFS
***************
*** 577,583 ****
  #endif /* Py_TRACE_REFS */
  	Py_DECREF(inst->in_class);
  	Py_XDECREF(inst->in_dict);
! 	PyObject_DEL(inst);
  }
  
  static PyObject *
--- 582,588 ----
  #endif /* Py_TRACE_REFS */
  	Py_DECREF(inst->in_class);
  	Py_XDECREF(inst->in_dict);
! 	PyObject_GC_DEL(inst);
  }
  
  static PyObject *
***************
*** 1537,1542 ****
--- 1542,1548 ----
  	im->im_self = self;
  	Py_INCREF(class);
  	im->im_class = class;
+ 	PyObject_GC_Init((PyObject *)im);
  	return (PyObject *)im;
  }
  
***************
*** 1612,1617 ****
--- 1618,1624 ----
  instancemethod_dealloc(im)
  	register PyMethodObject *im;
  {
+ 	PyObject_GC_Fini((PyObject *)im);
  	Py_DECREF(im->im_func);
  	Py_XDECREF(im->im_self);
  	Py_DECREF(im->im_class);
Index: type.1/Objects/dictobject.c
*** type.1/Objects/dictobject.c Wed, 14 Jun 2000 13:16:32 -0600 nas (python/E/19_dictobject 1.1.2.1.1.2 644)
--- type.1(w)/Objects/dictobject.c Wed, 14 Jun 2000 13:18:46 -0600 nas (python/E/19_dictobject 1.1.2.1.1.2 644)
***************
*** 129,134 ****
--- 129,135 ----
  	mp->ma_table = NULL;
  	mp->ma_fill = 0;
  	mp->ma_used = 0;
+ 	PyObject_GC_Init((PyObject *)mp);
  	return (PyObject *)mp;
  }
  
***************
*** 481,486 ****
--- 482,488 ----
  	register int i;
  	register dictentry *ep;
  	Py_TRASHCAN_SAFE_BEGIN(mp)
+ 	PyObject_GC_Fini((PyObject *)mp);
  	for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
  		if (ep->me_key != NULL) {
  			Py_DECREF(ep->me_key);
***************
*** 491,497 ****
  	}
  	if (mp->ma_table != NULL)
  		PyMem_DEL(mp->ma_table);
! 	PyObject_DEL(mp);
  	Py_TRASHCAN_SAFE_END(mp)
  }
  
--- 493,499 ----
  	}
  	if (mp->ma_table != NULL)
  		PyMem_DEL(mp->ma_table);
! 	PyObject_GC_DEL(mp);
  	Py_TRASHCAN_SAFE_END(mp)
  }
  
Index: type.1/Objects/funcobject.c
*** type.1/Objects/funcobject.c Wed, 14 Jun 2000 13:16:32 -0600 nas (python/E/23_funcobject 1.1.2.2.1.2 644)
--- type.1(w)/Objects/funcobject.c Wed, 14 Jun 2000 13:18:46 -0600 nas (python/E/23_funcobject 1.1.2.2.1.2 644)
***************
*** 63,68 ****
--- 63,69 ----
  		Py_INCREF(doc);
  		op->func_doc = doc;
  	}
+ 	PyObject_GC_Init((PyObject *)op);
  	return (PyObject *)op;
  }
  
***************
*** 186,197 ****
  func_dealloc(op)
  	PyFunctionObject *op;
  {
  	Py_DECREF(op->func_code);
  	Py_DECREF(op->func_globals);
  	Py_DECREF(op->func_name);
  	Py_XDECREF(op->func_defaults);
  	Py_XDECREF(op->func_doc);
! 	PyObject_DEL(op);
  }
  
  static PyObject*
--- 187,199 ----
  func_dealloc(op)
  	PyFunctionObject *op;
  {
+ 	PyObject_GC_Fini((PyObject *)op);
  	Py_DECREF(op->func_code);
  	Py_DECREF(op->func_globals);
  	Py_DECREF(op->func_name);
  	Py_XDECREF(op->func_defaults);
  	Py_XDECREF(op->func_doc);
! 	PyObject_GC_DEL(op);
  }
  
  static PyObject*
Index: type.1/Objects/listobject.c
*** type.1/Objects/listobject.c Wed, 14 Jun 2000 13:16:32 -0600 nas (python/E/25_listobject 1.1.2.3.1.2 644)
--- type.1(w)/Objects/listobject.c Wed, 14 Jun 2000 13:21:44 -0600 nas (python/E/25_listobject 1.1.2.3.1.2 644)
***************
*** 89,94 ****
--- 89,95 ----
  	PyObject_INIT_VAR(op, &PyList_Type, size);
  	for (i = 0; i < size; i++)
  		op->ob_item[i] = NULL;
+ 	PyObject_GC_Init((PyObject *)op);
  	return (PyObject *) op;
  }
  
***************
*** 216,221 ****
--- 217,223 ----
  {
  	int i;
  	Py_TRASHCAN_SAFE_BEGIN(op)
+ 	PyObject_GC_Fini((PyObject *)op);
  	if (op->ob_item != NULL) {
  		/* Do it backwards, for Christian Tismer.
  		   There's a simple test case where somehow this reduces
***************
*** 227,233 ****
  		}
  		PyMem_FREE(op->ob_item);
  	}
! 	PyObject_DEL(op);
  	Py_TRASHCAN_SAFE_END(op)
  }
  
--- 229,235 ----
  		}
  		PyMem_FREE(op->ob_item);
  	}
! 	PyObject_GC_DEL(op);
  	Py_TRASHCAN_SAFE_END(op)
  }
  
Index: type.1/Objects/tupleobject.c
*** type.1/Objects/tupleobject.c Wed, 14 Jun 2000 13:16:32 -0600 nas (python/E/33_tupleobjec 1.1.2.3.1.2 644)
--- type.1(w)/Objects/tupleobject.c Wed, 14 Jun 2000 13:22:30 -0600 nas (python/E/33_tupleobjec 1.1.2.3.1.2 644)
***************
*** 103,109 ****
  		op = (PyTupleObject *) PyObject_MALLOC(nbytes);
  		if (op == NULL)
  			return PyErr_NoMemory();
- 
  		PyObject_INIT_VAR(op, &PyTuple_Type, size);
  	}
  	for (i = 0; i < size; i++)
--- 103,108 ----
***************
*** 115,120 ****
--- 114,120 ----
  		Py_INCREF(op);	/* extra INCREF so that this is never freed */
  	}
  #endif
+ 	PyObject_GC_Init((PyObject *)op);
  	return (PyObject *) op;
  }
  
***************
*** 181,186 ****
--- 181,187 ----
  	register int i;
  	register int len =  op->ob_size;
  	Py_TRASHCAN_SAFE_BEGIN(op)
+ 	PyObject_GC_Fini((PyObject *)op);
  	if (len > 0) {
  		i = len;
  		while (--i >= 0)
***************
*** 194,200 ****
  		}
  #endif
  	}
! 	PyObject_DEL(op);
  done:
  	Py_TRASHCAN_SAFE_END(op)
  }
--- 195,201 ----
  		}
  #endif
  	}
! 	PyObject_GC_DEL(op);
  done:
  	Py_TRASHCAN_SAFE_END(op)
  }
***************
*** 560,566 ****
  					+ newsize * sizeof(PyObject *));
  		*pv = (PyObject *) sv;
  		if (sv == NULL) {
! 			PyObject_DEL(v);
  			PyErr_NoMemory();
  			return -1;
  		}
--- 561,568 ----
  					+ newsize * sizeof(PyObject *));
  		*pv = (PyObject *) sv;
  		if (sv == NULL) {
! 			PyObject_GC_Init((PyObject *)v);
! 			PyObject_GC_DEL(v);
  			PyErr_NoMemory();
  			return -1;
  		}
***************
*** 575,580 ****
--- 577,583 ----
  			sv->ob_item[i - sizediff] = NULL;
  		}
  	}
+ 	PyObject_GC_Init((PyObject *)sv);
  	sv->ob_size = newsize;
  	return 0;
  }
***************
*** 595,601 ****
  		while (p) {
  			q = p;
  			p = (PyTupleObject *)(p->ob_item[0]);
! 			PyObject_DEL(q);
  		}
  	}
  #endif
--- 598,604 ----
  		while (p) {
  			q = p;
  			p = (PyTupleObject *)(p->ob_item[0]);
! 			PyObject_GC_DEL(q);
  		}
  	}
  #endif

--ZGiS0Q5IWpPtfppv--