[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.90,2.91

Tim Peters tim_one@users.sourceforge.net
Sat, 06 Oct 2001 12:04:03 -0700


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

Modified Files:
	typeobject.c 
Log Message:
Repaired the debug Windows deaths in test_descr, by allocating enough
pad memory to properly align the __dict__ pointer in all cases.

gcmodule.c/objimpl.h, _PyObject_GC_Malloc:
+ Added a "padding" argument so that this flavor of malloc can allocate
  enough bytes for alignment padding (it can't know this is needed, but
  its callers do).

typeobject.c, PyType_GenericAlloc:
+ Allocated enough bytes to align the __dict__ pointer.
+ Sped and simplified the round-up-to-PTRSIZE logic.
+ Added blank lines so I could parse the if/else blocks <0.7 wink>.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.90
retrieving revision 2.91
diff -C2 -d -r2.90 -r2.91
*** typeobject.c	2001/10/05 20:51:39	2.90
--- typeobject.c	2001/10/06 19:04:01	2.91
***************
*** 193,222 ****
  #define PTRSIZE (sizeof(PyObject *))
  
! 	int size;
  	PyObject *obj;
  
! 	/* Inline PyObject_New() so we can zero the memory */
! 	size = _PyObject_VAR_SIZE(type, nitems);
! 	/* Round up size, if necessary, so we fully zero out __dict__ */
! 	if (type->tp_itemsize % PTRSIZE != 0) {
! 		size += PTRSIZE - 1;
! 		size /= PTRSIZE;
! 		size *= PTRSIZE;
! 	}
! 	if (PyType_IS_GC(type)) {
! 		obj = _PyObject_GC_Malloc(type, nitems);
  	}
! 	else {
  		obj = PyObject_MALLOC(size);
! 	}
  	if (obj == NULL)
  		return PyErr_NoMemory();
  	memset(obj, '\0', size);
  	if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
  		Py_INCREF(type);
  	if (type->tp_itemsize == 0)
  		PyObject_INIT(obj, type);
  	else
  		(void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
  	if (PyType_IS_GC(type))
  		_PyObject_GC_TRACK(obj);
--- 193,232 ----
  #define PTRSIZE (sizeof(PyObject *))
  
! 	size_t size = (size_t)_PyObject_VAR_SIZE(type, nitems);
! 	size_t padding = 0;
  	PyObject *obj;
  
! 	/* Round up size, if necessary, so that the __dict__ pointer
! 	   following the variable part is properly aligned for the platform.
! 	   This is needed only for types with a vrbl number of items
! 	   before the __dict__ pointer == types that record the dict offset
! 	   as a negative offset from the end of the object.  If tp_dictoffset
! 	   is 0, there is no __dict__; if positive, tp_dict was declared in a C
! 	   struct so the compiler already took care of aligning it. */
!         if (type->tp_dictoffset < 0) {
! 		padding = PTRSIZE - size % PTRSIZE;
! 		if (padding == PTRSIZE)
! 			padding = 0;
! 		size += padding;
  	}
! 
! 	if (PyType_IS_GC(type))
! 		obj = _PyObject_GC_Malloc(type, nitems, padding);
! 	else
  		obj = PyObject_MALLOC(size);
! 
  	if (obj == NULL)
  		return PyErr_NoMemory();
+ 
  	memset(obj, '\0', size);
+ 
  	if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
  		Py_INCREF(type);
+ 
  	if (type->tp_itemsize == 0)
  		PyObject_INIT(obj, type);
  	else
  		(void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
+ 
  	if (PyType_IS_GC(type))
  		_PyObject_GC_TRACK(obj);