[Python-checkins] CVS: python/dist/src/Modules gcmodule.c,2.21,2.22

Tim Peters tim_one@users.sourceforge.net
Sat, 06 Oct 2001 14:27:36 -0700


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

Modified Files:
	gcmodule.c 
Log Message:
_PyObject_VAR_SIZE:  always round up to a multiple-of-pointer-size value.
As Guido suggested, this makes the new subclassing code substantially
simpler.  But the mechanics of doing it w/ C macro semantics are a mess,
and _PyObject_VAR_SIZE has a new calling sequence now.

Question:  The PyObject_NEW_VAR macro appears to be part of the public API.
Regardless of what it expands to, the notion that it has to round up the
memory it allocates is new, and extensions containing the old
PyObject_NEW_VAR macro expansion (which was embedded in the
PyObject_NEW_VAR expansion) won't do this rounding.  But the rounding
isn't actually *needed* except for new-style instances with dict pointers
after a variable-length blob of embedded data.  So my guess is that we do
not need to bump the API version for this (as the rounding isn't needed
for anything an extension can do unless it's recompiled anyway).  What's
your guess?


Index: gcmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/gcmodule.c,v
retrieving revision 2.21
retrieving revision 2.22
diff -C2 -d -r2.21 -r2.22
*** gcmodule.c	2001/10/06 19:04:00	2.21
--- gcmodule.c	2001/10/06 21:27:34	2.22
***************
*** 799,810 ****
  
  PyObject *
! _PyObject_GC_Malloc(PyTypeObject *tp, int nitems, size_t padding)
  {
  	PyObject *op;
  #ifdef WITH_CYCLE_GC
! 	const size_t basic = (size_t)_PyObject_VAR_SIZE(tp, nitems);
! 	const size_t nbytes = sizeof(PyGC_Head) + basic + padding;
  
! 	PyGC_Head *g = PyObject_MALLOC(nbytes);
  	if (g == NULL)
  		return (PyObject *)PyErr_NoMemory();
--- 799,813 ----
  
  PyObject *
! _PyObject_GC_Malloc(PyTypeObject *tp, int nitems)
  {
  	PyObject *op;
+ 	size_t basicsize;
  #ifdef WITH_CYCLE_GC
! 	size_t nbytes;
! 	PyGC_Head *g;
  
! 	_PyObject_VAR_SIZE(basicsize, tp, nitems);
! 	nbytes = sizeof(PyGC_Head) + basicsize;
! 	g = PyObject_MALLOC(nbytes);
  	if (g == NULL)
  		return (PyObject *)PyErr_NoMemory();
***************
*** 822,826 ****
  	op = FROM_GC(g);
  #else
! 	op = PyObject_MALLOC(_PyObject_VAR_SIZE(tp, nitems) + padding);
  	if (op == NULL)
  		return (PyObject *)PyErr_NoMemory();
--- 825,830 ----
  	op = FROM_GC(g);
  #else
! 	_PyObject_VAR_SIZE(basicsize, tp, nitems);
! 	op = PyObject_MALLOC(basicsize);
  	if (op == NULL)
  		return (PyObject *)PyErr_NoMemory();
***************
*** 833,863 ****
  _PyObject_GC_New(PyTypeObject *tp)
  {
! 	PyObject *op = _PyObject_GC_Malloc(tp, 0, 0);
  	return PyObject_INIT(op, tp);
  }
  
  PyVarObject *
! _PyObject_GC_NewVar(PyTypeObject *tp, int size)
  {
! 	PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, size, 0);
! 	return PyObject_INIT_VAR(op, tp, size);
  }
  
  PyVarObject *
! _PyObject_GC_Resize(PyVarObject *op, int size)
  {
  #ifdef WITH_CYCLE_GC
  	PyGC_Head *g = AS_GC(op);
! 	g = PyObject_REALLOC(g, _PyObject_VAR_SIZE(op->ob_type, size) +
! 						sizeof(PyGC_Head));
  	if (g == NULL)
  		return (PyVarObject *)PyErr_NoMemory();
  	op = (PyVarObject *) FROM_GC(g);
  #else
! 	op = PyObject_REALLOC(op, _PyObject_VAR_SIZE(op->ob_type, size));
  	if (op == NULL)
  		return (PyVarObject *)PyErr_NoMemory();
  #endif
! 	op->ob_size = size;
  	return op;
  }
--- 837,870 ----
  _PyObject_GC_New(PyTypeObject *tp)
  {
! 	PyObject *op = _PyObject_GC_Malloc(tp, 0);
  	return PyObject_INIT(op, tp);
  }
  
  PyVarObject *
! _PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
  {
! 	PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, nitems);
! 	return PyObject_INIT_VAR(op, tp, nitems);
  }
  
  PyVarObject *
! _PyObject_GC_Resize(PyVarObject *op, int nitems)
  {
+ 	size_t basicsize;
  #ifdef WITH_CYCLE_GC
  	PyGC_Head *g = AS_GC(op);
! 
! 	_PyObject_VAR_SIZE(basicsize, op->ob_type, nitems);
! 	g = PyObject_REALLOC(g,  sizeof(PyGC_Head) + basicsize);
  	if (g == NULL)
  		return (PyVarObject *)PyErr_NoMemory();
  	op = (PyVarObject *) FROM_GC(g);
  #else
! 	_PyObject_VAR_SIZE(basicsize, op->ob_type, nitems);
! 	op = PyObject_REALLOC(op, basicsize);
  	if (op == NULL)
  		return (PyVarObject *)PyErr_NoMemory();
  #endif
! 	op->ob_size = nitems;
  	return op;
  }