[Python-checkins] python/dist/src/Include object.h,2.104,2.105 pydebug.h,2.19,2.20

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Mon, 08 Jul 2002 19:57:03 -0700


Update of /cvsroot/python/python/dist/src/Include
In directory usw-pr-cvs1:/tmp/cvs-serv13269/python/Include

Modified Files:
	object.h pydebug.h 
Log Message:
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield:  added
more trivial lexical helper macros so that uses of these guys expand
to nothing at all when they're not enabled.  This should help sub-
standard compilers that can't do a good job of optimizing away the
previous "(void)0" expressions.

Py_DECREF:  There's only one definition of this now.  Yay!  That
was that last one in the family defined multiple times in an #ifdef
maze.

Py_FatalError():  Changed the char* signature to const char*.

_Py_NegativeRefcount():  New helper function for the Py_REF_DEBUG
expansion of Py_DECREF.  Calling an external function cuts down on
the volume of generated code.  The previous inline expansion of abort()
didn't work as intended on Windows (the program often kept going, and
the error msg scrolled off the screen unseen).  _Py_NegativeRefcount
calls Py_FatalError instead, which captures our best knowledge of
how to abort effectively across platforms.


Index: object.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/object.h,v
retrieving revision 2.104
retrieving revision 2.105
diff -C2 -d -r2.104 -r2.105
*** object.h	7 Jul 2002 19:59:50 -0000	2.104
--- object.h	9 Jul 2002 02:57:01 -0000	2.105
***************
*** 67,70 ****
--- 67,72 ----
   * there's probably a leak.  Remember, though, that in interactive mode the
   * special name "_" holds a reference to the last result displayed!
+  * Py_REF_DEBUG also checks after every decref to verify that the refcount
+  * hasn't gone negative, and causes an immediate fatal error if it has.
   */
  #define Py_REF_DEBUG
***************
*** 537,543 ****
  #ifdef Py_REF_DEBUG
  extern DL_IMPORT(long) _Py_RefTotal;
! #define _PyMAYBE_BUMP_REFTOTAL	_Py_RefTotal++
  #else
! #define _PyMAYBE_BUMP_REFTOTAL	(void)0
  #endif
  
--- 539,557 ----
  #ifdef Py_REF_DEBUG
  extern DL_IMPORT(long) _Py_RefTotal;
! extern DL_IMPORT(void) _Py_NegativeRefcount(const char *fname,
! 					    int lineno, PyObject *op);
! #define _PyMAYBE_BUMP_REFTOTAL		_Py_RefTotal++
! #define _PyMAYBE_DROP_REFTOTAL		_Py_RefTotal--
! #define _PyMAYBE_REFTOTAL_COMMA		,
! #define _PyMAYBE_CHECK_REFCNT(OP)				\
! {	if ((OP)->ob_refcnt < 0)				\
! 		_Py_NegativeRefcount(__FILE__, __LINE__,	\
! 				     (PyObject *)(OP));		\
! }
  #else
! #define _PyMAYBE_BUMP_REFTOTAL
! #define _PyMAYBE_DROP_REFTOTAL
! #define _PyMAYBE_REFTOTAL_COMMA
! #define _PyMAYBE_CHECK_REFCNT(OP)	;
  #endif
  
***************
*** 546,552 ****
  #define _PyMAYBE_BUMP_COUNT(OP)		inc_count((OP)->ob_type)
  #define _PyMAYBE_BUMP_FREECOUNT(OP)	(OP)->ob_type->tp_frees++
  #else
! #define _PyMAYBE_BUMP_COUNT(OP)		(void)0
! #define _PyMAYBE_BUMP_FREECOUNT(OP)	(void)0
  #endif
  
--- 560,570 ----
  #define _PyMAYBE_BUMP_COUNT(OP)		inc_count((OP)->ob_type)
  #define _PyMAYBE_BUMP_FREECOUNT(OP)	(OP)->ob_type->tp_frees++
+ #define _PyMAYBE_BUMP_COUNT_COMMA	,
+ #define _PyMAYBE_BUMP_FREECOUNT_COMMA	,
  #else
! #define _PyMAYBE_BUMP_COUNT(OP)
! #define _PyMAYBE_BUMP_FREECOUNT(OP)
! #define _PyMAYBE_BUMP_COUNT_COMMA
! #define _PyMAYBE_BUMP_FREECOUNT_COMMA
  #endif
  
***************
*** 563,598 ****
   * inline.
   */
! #define _Py_NewReference(op) (		\
! 	_PyMAYBE_BUMP_COUNT(op),	\
! 	_PyMAYBE_BUMP_REFTOTAL, 	\
  	(op)->ob_refcnt = 1)
  
! #define _Py_ForgetReference(op) (_PyMAYBE_BUMP_FREECOUNT(op))
  
! #define _Py_Dealloc(op) (		\
! 	_Py_ForgetReference(op),	\
  	(*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
- 
  #endif /* !Py_TRACE_REFS */
  
! #define Py_INCREF(op) (			\
! 	_PyMAYBE_BUMP_REFTOTAL,		\
  	(op)->ob_refcnt++)
  
! #ifdef Py_REF_DEBUG
! /* under Py_REF_DEBUG: also log negative ref counts after Py_DECREF() !! */
! #define Py_DECREF(op)							\
!        if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ;			\
!        else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op));	\
!        else ((void)fprintf(stderr, "%s:%i negative ref count %i\n",	\
! 		           __FILE__, __LINE__, (op)->ob_refcnt), abort())
! 
! #else
! #define Py_DECREF(op) \
! 	if (--(op)->ob_refcnt != 0) \
! 		; \
! 	else \
  		_Py_Dealloc((PyObject *)(op))
- #endif /* !Py_REF_DEBUG */
  
  /* Macros to use in case the object pointer may be NULL: */
--- 581,606 ----
   * inline.
   */
! #define _Py_NewReference(op) (					\
! 	_PyMAYBE_BUMP_COUNT(op) _PyMAYBE_BUMP_COUNT_COMMA	\
! 	_PyMAYBE_BUMP_REFTOTAL _PyMAYBE_REFTOTAL_COMMA		\
  	(op)->ob_refcnt = 1)
  
! #define _Py_ForgetReference(op) _PyMAYBE_BUMP_FREECOUNT(op)
  
! #define _Py_Dealloc(op) (						\
! 	_PyMAYBE_BUMP_FREECOUNT(op) _PyMAYBE_BUMP_FREECOUNT_COMMA	\
  	(*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
  #endif /* !Py_TRACE_REFS */
  
! #define Py_INCREF(op) (						\
! 	_PyMAYBE_BUMP_REFTOTAL _PyMAYBE_REFTOTAL_COMMA		\
  	(op)->ob_refcnt++)
  
! #define Py_DECREF(op)						\
! 	if (_PyMAYBE_DROP_REFTOTAL _PyMAYBE_REFTOTAL_COMMA	\
! 	    --(op)->ob_refcnt != 0)				\
! 		_PyMAYBE_CHECK_REFCNT(op)			\
! 	else							\
  		_Py_Dealloc((PyObject *)(op))
  
  /* Macros to use in case the object pointer may be NULL: */

Index: pydebug.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/pydebug.h,v
retrieving revision 2.19
retrieving revision 2.20
diff -C2 -d -r2.19 -r2.20
*** pydebug.h	6 Dec 2001 06:23:25 -0000	2.19
--- pydebug.h	9 Jul 2002 02:57:01 -0000	2.20
***************
*** 27,31 ****
  #define Py_GETENV(s) (Py_IgnoreEnvironmentFlag ? NULL : getenv(s))
  
! DL_IMPORT(void) Py_FatalError(char *message);
  
  #ifdef __cplusplus
--- 27,31 ----
  #define Py_GETENV(s) (Py_IgnoreEnvironmentFlag ? NULL : getenv(s))
  
! DL_IMPORT(void) Py_FatalError(const char *message);
  
  #ifdef __cplusplus