[Python-checkins] python/dist/src/Parser acceler.c,2.18,2.19 node.c,2.19,2.20 parsetok.c,2.31,2.32

aimacintyre@users.sourceforge.net aimacintyre@users.sourceforge.net
Sat, 03 Aug 2002 23:26:52 -0700


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

Modified Files:
	acceler.c node.c parsetok.c 
Log Message:
SF patch #578297:

Change the parser and compiler to use PyMalloc.

Only the files implementing processes that will request memory 
allocations small enough for PyMalloc to be a win have been 
changed, which are:-
 - Python/compile.c
 - Parser/acceler.c
 - Parser/node.c
 - Parser/parsetok.c

This augments the aggressive overallocation strategy implemented by 
Tim Peters in PyNode_AddChild() [Parser/node.c], in reducing the 
impact of platform malloc()/realloc()/free() corner case behaviour. 
Such corner cases are known to be triggered by test_longexp and 
test_import.

Jeremy Hylton, in accepting this patch, recommended this as a 
bugfix candidate for 2.2.  While the changes to Python/compile.c 
and Parser/node.c backport easily (and could go in), the changes 
to Parser/acceler.c and Parser/parsetok.c require other not 
insignificant changes as a result of the differences in the memory 
APIs between 2.3 and 2.2, which I'm not in a position to work 
through at the moment.  This is a pity, as the Parser/parsetok.c 
changes are the most important after the Parser/node.c changes, due 
to the size of the memory requests involved and their frequency.


Index: acceler.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Parser/acceler.c,v
retrieving revision 2.18
retrieving revision 2.19
diff -C2 -d -r2.18 -r2.19
*** acceler.c	11 Jul 2002 15:43:37 -0000	2.18
--- acceler.c	4 Aug 2002 06:26:49 -0000	2.19
***************
*** 45,49 ****
  		for (j = 0; j < d->d_nstates; j++, s++) {
  			if (s->s_accel)
! 				PyMem_DEL(s->s_accel);
  			s->s_accel = NULL;
  		}
--- 45,49 ----
  		for (j = 0; j < d->d_nstates; j++, s++) {
  			if (s->s_accel)
! 				PyObject_FREE(s->s_accel);
  			s->s_accel = NULL;
  		}
***************
*** 69,73 ****
  	int nl = g->g_ll.ll_nlabels;
  	s->s_accept = 0;
! 	accel = PyMem_NEW(int, nl);
  	for (k = 0; k < nl; k++)
  		accel[k] = -1;
--- 69,73 ----
  	int nl = g->g_ll.ll_nlabels;
  	s->s_accept = 0;
! 	accel = (int *) PyObject_MALLOC(nl * sizeof(int));
  	for (k = 0; k < nl; k++)
  		accel[k] = -1;
***************
*** 125,129 ****
  	if (k < nl) {
  		int i;
! 		s->s_accel = PyMem_NEW(int, nl-k);
  		if (s->s_accel == NULL) {
  			fprintf(stderr, "no mem to add parser accelerators\n");
--- 125,129 ----
  	if (k < nl) {
  		int i;
! 		s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
  		if (s->s_accel == NULL) {
  			fprintf(stderr, "no mem to add parser accelerators\n");
***************
*** 135,138 ****
  			s->s_accel[i] = accel[k];
  	}
! 	PyMem_DEL(accel);
  }
--- 135,138 ----
  			s->s_accel[i] = accel[k];
  	}
! 	PyObject_FREE(accel);
  }

Index: node.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Parser/node.c,v
retrieving revision 2.19
retrieving revision 2.20
diff -C2 -d -r2.19 -r2.20
*** node.c	15 Jul 2002 17:58:03 -0000	2.19
--- node.c	4 Aug 2002 06:26:49 -0000	2.20
***************
*** 8,12 ****
  PyNode_New(int type)
  {
! 	node *n = PyMem_NEW(node, 1);
  	if (n == NULL)
  		return NULL;
--- 8,12 ----
  PyNode_New(int type)
  {
! 	node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
  	if (n == NULL)
  		return NULL;
***************
*** 93,97 ****
  	if (current_capacity < required_capacity) {
  		n = n1->n_child;
! 		PyMem_RESIZE(n, node, required_capacity);
  		if (n == NULL)
  			return E_NOMEM;
--- 93,98 ----
  	if (current_capacity < required_capacity) {
  		n = n1->n_child;
! 		n = (node *) PyObject_REALLOC(n,
! 					      required_capacity * sizeof(node));
  		if (n == NULL)
  			return E_NOMEM;
***************
*** 117,121 ****
  	if (n != NULL) {
  		freechildren(n);
! 		PyMem_DEL(n);
  	}
  }
--- 118,122 ----
  	if (n != NULL) {
  		freechildren(n);
! 		PyObject_FREE(n);
  	}
  }
***************
*** 128,133 ****
  		freechildren(CHILD(n, i));
  	if (n->n_child != NULL)
! 		PyMem_DEL(n->n_child);
  	if (STR(n) != NULL)
! 		PyMem_DEL(STR(n));
  }
--- 129,134 ----
  		freechildren(CHILD(n, i));
  	if (n->n_child != NULL)
! 		PyObject_FREE(n->n_child);
  	if (STR(n) != NULL)
! 		PyObject_FREE(STR(n));
  }

Index: parsetok.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Parser/parsetok.c,v
retrieving revision 2.31
retrieving revision 2.32
diff -C2 -d -r2.31 -r2.32
*** parsetok.c	9 Jul 2002 09:23:27 -0000	2.31
--- parsetok.c	4 Aug 2002 06:26:49 -0000	2.32
***************
*** 134,138 ****
  			started = 1;
  		len = b - a; /* XXX this may compute NULL - NULL */
! 		str = PyMem_NEW(char, len + 1);
  		if (str == NULL) {
  			fprintf(stderr, "no mem for next token\n");
--- 134,138 ----
  			started = 1;
  		len = b - a; /* XXX this may compute NULL - NULL */
! 		str = (char *) PyObject_MALLOC(len + 1);
  		if (str == NULL) {
  			fprintf(stderr, "no mem for next token\n");
***************
*** 158,162 ****
  				       &(err_ret->expected))) != E_OK) {
  			if (err_ret->error != E_DONE)
! 				PyMem_DEL(str);
  			break;
  		}
--- 158,162 ----
  				       &(err_ret->expected))) != E_OK) {
  			if (err_ret->error != E_DONE)
! 				PyObject_FREE(str);
  			break;
  		}
***************
*** 179,183 ****
  		if (tok->buf != NULL) {
  			size_t len = tok->inp - tok->buf;
! 			err_ret->text = PyMem_NEW(char, len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)
--- 179,183 ----
  		if (tok->buf != NULL) {
  			size_t len = tok->inp - tok->buf;
! 			err_ret->text = (char *) PyObject_MALLOC(len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)