[Python-checkins] python/dist/src/Objects object.c,2.204,2.205 typeobject.c,2.218,2.219

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 23 Mar 2003 09:52:30 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv2910/Objects

Modified Files:
	object.c typeobject.c 
Log Message:
Improved new Py_TRACE_REFS gimmicks.
Arranged that all the objects exposed by __builtin__ appear in the list
of all objects.  I basically peed away two days tracking down a mystery
leak in sys.gettotalrefcount() in a ZODB app (== tons of code), because
the object leaking the references didn't appear in the sys.getobjects(0)
list.  The object happened to be False.  Now False is in the list, along
with other popular & previously missing leak candidates (like None).
Alas, we still don't have a choke point covering *all* Python objects,
so the list of all objects may still be incomplete.


Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.204
retrieving revision 2.205
diff -C2 -d -r2.204 -r2.205
*** object.c	23 Mar 2003 03:33:13 -0000	2.204
--- object.c	23 Mar 2003 17:52:28 -0000	2.205
***************
*** 19,35 ****
  
  #ifdef Py_TRACE_REFS
! /* Head of doubly-linked list of all objects. */
  static PyObject refchain = {&refchain, &refchain};
  
! /* Insert op at the fron of the doubly-linked list of all objects. */
  void
! _Py_AddToAllObjects(PyObject *op)
  {
! 	op->_ob_next = refchain._ob_next;
! 	op->_ob_prev = &refchain;
! 	refchain._ob_next->_ob_prev = op;
! 	refchain._ob_next = op;
! }
  #endif
  
  #ifdef COUNT_ALLOCS
--- 19,58 ----
  
  #ifdef Py_TRACE_REFS
! /* Head of circular doubly-linked list of all objects.  These are linked
!  * together via the _ob_prev and _ob_next members of a PyObject, which
!  * exist only in a Py_TRACE_REFS build.
!  */
  static PyObject refchain = {&refchain, &refchain};
  
! /* Insert op at the front of the list of all objects.  If force is true,
!  * op is added even if _ob_prev and _ob_next are non-NULL already.  If
!  * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
!  * force should be true if and only if op points to freshly allocated,
!  * uninitialized memory, or you've unlinked op from the list and are
!  * relinking it into the font.
!  * Note that objects are normally added to the list via _Py_NewReference,
!  * which is called by PyObject_Init.  Not all objects are initialized that
!  * way, though; exceptions include statically allocated type objects, and
!  * statically allocated singletons (like Py_True and Py_None).
!  */
  void
! _Py_AddToAllObjects(PyObject *op, int force)
  {
! #ifdef  Py_DEBUG
! 	if (!force) {
! 		/* If it's initialized memory, op must be in or out of
! 		 * the list unambiguously.
! 		 */
! 		assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
! 	}
  #endif
+ 	if (force || op->_ob_prev == NULL) {
+ 		op->_ob_next = refchain._ob_next;
+ 		op->_ob_prev = &refchain;
+ 		refchain._ob_next->_ob_prev = op;
+ 		refchain._ob_next = op;
+ 	}
+ }
+ #endif	/* Py_TRACE_REFS */
  
  #ifdef COUNT_ALLOCS
***************
*** 101,109 ****
  		type_list = tp;
  #ifdef Py_TRACE_REFS
! 		/* Also insert in the doubly-linked list of all objects. */
! 		if (tp->_ob_prev == NULL) {
! 			assert(tp->_ob_next == NULL);
! 			_Py_AddToAllObjects((PyObject *)tp);
! 		}
  #endif
  	}
--- 124,131 ----
  		type_list = tp;
  #ifdef Py_TRACE_REFS
! 		/* Also insert in the doubly-linked list of all objects,
! 		 * if not already there.
! 		 */
! 		_Py_AddToAllObjects((PyObject *)tp, 0);
  #endif
  	}
***************
*** 1964,1968 ****
  	_Py_INC_REFTOTAL;
  	op->ob_refcnt = 1;
! 	_Py_AddToAllObjects(op);
  	_Py_INC_TPALLOCS(op);
  }
--- 1986,1990 ----
  	_Py_INC_REFTOTAL;
  	op->ob_refcnt = 1;
! 	_Py_AddToAllObjects(op, 1);
  	_Py_INC_TPALLOCS(op);
  }

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.218
retrieving revision 2.219
diff -C2 -d -r2.218 -r2.219
*** typeobject.c	23 Mar 2003 05:35:36 -0000	2.218
--- typeobject.c	23 Mar 2003 17:52:28 -0000	2.219
***************
*** 3059,3066 ****
  	 * Still, not all type objects go thru PyType_Ready.
  	 */
! 	 if (type->_ob_next == NULL) {
! 	 	assert(type->_ob_prev == NULL);
! 		_Py_AddToAllObjects((PyObject *)type);
! 	}
  #endif
  
--- 3059,3063 ----
  	 * Still, not all type objects go thru PyType_Ready.
  	 */
! 	_Py_AddToAllObjects((PyObject *)type, 0);
  #endif