[Python-checkins] CVS: python/dist/src/Objects tupleobject.c,2.46,2.47

Neil Schemenauer python-dev@python.org
Thu, 5 Oct 2000 12:36:52 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory slayer.i.sourceforge.net:/tmp/cvs-serv6491/Objects

Modified Files:
	tupleobject.c 
Log Message:
Simplify _PyTuple_Resize by not using the tuple free list and dropping
support for the last_is_sticky flag.  A few hard to find bugs may be
fixed by this patch since the old code was buggy. 


Index: tupleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/tupleobject.c,v
retrieving revision 2.46
retrieving revision 2.47
diff -C2 -r2.46 -r2.47
*** tupleobject.c	2000/09/15 07:32:39	2.46
--- tupleobject.c	2000/10/05 19:36:49	2.47
***************
*** 425,433 ****
     it changes the size of a tuple.  We get away with this only if there
     is only one module referencing the object.  You can also think of it
!    as creating a new tuple object and destroying the old one, only
!    more efficiently.  In any case, don't use this if the tuple may
!    already be known to some other part of the code...
!    If last_is_sticky is set, the tuple will grow or shrink at the
!    front, otherwise it will grow or shrink at the end. */
  
  int
--- 425,432 ----
     it changes the size of a tuple.  We get away with this only if there
     is only one module referencing the object.  You can also think of it
!    as creating a new tuple object and destroying the old one, only more
!    efficiently.  In any case, don't use this if the tuple may already be
!    known to some other part of the code.  The last_is_sticky is not used
!    and must always be false. */
  
  int
***************
*** 440,446 ****
  
  	v = (PyTupleObject *) *pv;
! 	if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) {
  		*pv = 0;
! 		Py_DECREF(v);
  		PyErr_BadInternalCall();
  		return -1;
--- 439,446 ----
  
  	v = (PyTupleObject *) *pv;
! 	if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1 ||
!              last_is_sticky) {
  		*pv = 0;
! 		Py_XDECREF(v);
  		PyErr_BadInternalCall();
  		return -1;
***************
*** 449,543 ****
  	if (sizediff == 0)
  		return 0;
  	/* XXX UNREF/NEWREF interface should be more symmetrical */
  #ifdef Py_REF_DEBUG
  	--_Py_RefTotal;
  #endif
! 	_Py_ForgetReference((PyObject *)v);
! 	if (last_is_sticky && sizediff < 0) {
! 		/* shrinking:
! 		   move entries to the front and zero moved entries */
! 		for (i = 0; i < newsize; i++) {
! 			Py_XDECREF(v->ob_item[i]);
! 			v->ob_item[i] = v->ob_item[i - sizediff];
! 			v->ob_item[i - sizediff] = NULL;
! 		}
! 	}
  	for (i = newsize; i < v->ob_size; i++) {
  		Py_XDECREF(v->ob_item[i]);
  		v->ob_item[i] = NULL;
- 	}
- #if MAXSAVESIZE > 0
- 	if (newsize == 0 && free_tuples[0]) {
- 		num_free_tuples[0]--;
- 		sv = free_tuples[0];
- 		sv->ob_size = 0;
- 		Py_INCREF(sv);
- #ifdef COUNT_ALLOCS
- 		tuple_zero_allocs++;
- #endif
- 		tupledealloc(v);
- 		*pv = (PyObject*) sv;
- 		return 0;
  	}
! 	if (0 < newsize && newsize < MAXSAVESIZE &&
! 	    (sv = free_tuples[newsize]) != NULL)
! 	{
! 		free_tuples[newsize] = (PyTupleObject *) sv->ob_item[0];
! 		num_free_tuples[newsize]--;
! #ifdef COUNT_ALLOCS
! 		fast_tuple_allocs++;
! #endif
! #ifdef Py_TRACE_REFS 
! 		sv->ob_type = &PyTuple_Type; 
! #endif 
! 		for (i = 0; i < newsize; ++i){
! 			sv->ob_item[i] = v->ob_item[i];
! 			v->ob_item[i] = NULL;
! 		}
! 		sv->ob_size = v->ob_size;
! 		tupledealloc(v);
! 		*pv = (PyObject *) sv;
! 	} else 
! #endif		
! 	{
! #ifdef WITH_CYCLE_GC
! 		PyGC_Head *g = PyObject_AS_GC((PyObject *)v);
! 		PyObject_GC_Fini((PyObject *)v);
! 		g = (PyGC_Head *)
! 			PyObject_REALLOC((char *)g, sizeof(PyTupleObject) 
! 					+ PyGC_HEAD_SIZE
! 					+ newsize * sizeof(PyObject *));
! 		if (g == NULL) {
! 			sv = NULL;
! 		} else {
! 			sv = (PyTupleObject *)PyObject_FROM_GC(g);
! 		}
! #else
! 		sv = (PyTupleObject *)
! 			PyObject_REALLOC((char *)v, sizeof(PyTupleObject) 
! 					+ PyGC_HEAD_SIZE
! 					+ newsize * sizeof(PyObject *));
! #endif
! 		*pv = (PyObject *) sv;
! 		if (sv == NULL) {
! 			PyObject_GC_Init((PyObject *)v);
! 			v = (PyTupleObject *) PyObject_AS_GC(v);
! 			PyObject_DEL(v);
! 			PyErr_NoMemory();
! 			return -1;
! 		}
  	}
! 	_Py_NewReference((PyObject *)sv);
  	for (i = sv->ob_size; i < newsize; i++)
  		sv->ob_item[i] = NULL;
- 	if (last_is_sticky && sizediff > 0) {
- 		/* growing: move entries to the end and zero moved entries */
- 		for (i = newsize - 1; i >= sizediff; i--) {
- 			sv->ob_item[i] = sv->ob_item[i - sizediff];
- 			sv->ob_item[i - sizediff] = NULL;
- 		}
- 	}
- 	PyObject_GC_Init(sv);
  	sv->ob_size = newsize;
  	return 0;
  }
--- 449,481 ----
  	if (sizediff == 0)
  		return 0;
+ 
  	/* XXX UNREF/NEWREF interface should be more symmetrical */
  #ifdef Py_REF_DEBUG
  	--_Py_RefTotal;
  #endif
! 	_Py_ForgetReference((PyObject *) v);
  	for (i = newsize; i < v->ob_size; i++) {
  		Py_XDECREF(v->ob_item[i]);
  		v->ob_item[i] = NULL;
  	}
! 	PyObject_GC_Fini(v);
! 	v = (PyTupleObject *) PyObject_AS_GC(v);
! 	sv = (PyTupleObject *) PyObject_REALLOC((char *)v,
! 						sizeof(PyTupleObject)
! 						+ PyGC_HEAD_SIZE
! 						+ newsize * sizeof(PyObject *));
! 	if (sv == NULL) {
! 		*pv = NULL;
! 		PyObject_DEL(v);
! 		PyErr_NoMemory();
! 		return -1;
  	}
! 	sv = (PyTupleObject *) PyObject_FROM_GC(sv);
! 	_Py_NewReference((PyObject *) sv);
  	for (i = sv->ob_size; i < newsize; i++)
  		sv->ob_item[i] = NULL;
  	sv->ob_size = newsize;
+ 	*pv = (PyObject *) sv;
+ 	PyObject_GC_Init(sv);
  	return 0;
  }