[Patches] PyMem [4/8] - Parser/*

Vladimir Marangozov Vladimir.Marangozov@inrialpes.fr
Fri, 25 Feb 2000 13:28:38 +0100 (CET)


- PyMem_{Malloc, Realloc, Free} are put in a new separate file "mymalloc.c"
  which *is* the Python core malloc implementation. It's in the directory
  Parser/ since it is used by pgen.

- The files which import "pycore.h" are essentially those that belong to
  the Python library (libpython.a) -- i.e. AROBJS in Makefile.in

- Cleanup w.r.t. various mallocs.

- Added a comment about PyOS_Readline. If the input comes from somebody else
  (via the readline hook), we expect a Python memory buffer, because this
  buffer is freed subsequently by Python.


-- 
       Vladimir MARANGOZOV          | Vladimir.Marangozov@inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252

--
I confirm that, to the best of my knowledge and belief, this contribution is
free of any claims of third parties under copyright, patent or other rights
or interests ("claims").  To the extent that I have any such claims, I
hereby grant to CNRI a nonexclusive, irrevocable, royalty-free, worldwide
license to reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part of the
Python software and its related documentation, or any derivative versions
thereof, at no cost to CNRI or its licensed users, and to authorize others
to do so.

I acknowledge that CNRI may, at its sole discretion, decide whether or not
to incorporate this contribution in the Python software and its related
documentation.  I further grant CNRI permission to use my name and other
identifying information provided to CNRI by me for use in connection with
the Python software and its related documentation.

-------------------------------[ cut here ]---------------------------  
diff -cr PyCVS/Parser/Makefile.in PyMem/Parser/Makefile.in
*** PyCVS/Parser/Makefile.in	Thu Feb 24 14:49:47 2000
--- PyMem/Parser/Makefile.in	Thu Feb 24 17:19:17 2000
***************
*** 32,38 ****
  POBJS=		acceler.o grammar1.o \
  		listnode.o node.o parser.o \
  		parsetok.o tokenizer.o bitset.o \
! 		metagrammar.o
  
  AROBJS=		$(POBJS) myreadline.o
  OBJS=		$(AROBJS) intrcheck.o
--- 32,38 ----
  POBJS=		acceler.o grammar1.o \
  		listnode.o node.o parser.o \
  		parsetok.o tokenizer.o bitset.o \
! 		metagrammar.o mymalloc.o
  
  AROBJS=		$(POBJS) myreadline.o
  OBJS=		$(AROBJS) intrcheck.o
***************
*** 85,90 ****
--- 85,91 ----
  grammar1.o: grammar1.c
  intrcheck.o: intrcheck.c
  listnode.o: listnode.c
+ mymalloc.o: mymalloc.c
  myreadline.o: myreadline.c
  node.o: node.c
  parser.o: parser.c
diff -cr PyCVS/Parser/acceler.c PyMem/Parser/acceler.c
*** PyCVS/Parser/acceler.c	Thu Feb 24 14:49:47 2000
--- PyMem/Parser/acceler.c	Thu Feb 24 16:31:20 2000
***************
*** 40,45 ****
--- 40,46 ----
     are not part of the static data structure written on graminit.[ch]
     by the parser generator. */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "grammar.h"
  #include "node.h"
diff -cr PyCVS/Parser/bitset.c PyMem/Parser/bitset.c
*** PyCVS/Parser/bitset.c	Thu Feb 24 14:49:47 2000
--- PyMem/Parser/bitset.c	Thu Feb 24 16:34:49 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* Bitset primitives used by the parser generator */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "bitset.h"
  
diff -cr PyCVS/Parser/grammar1.c PyMem/Parser/grammar1.c
*** PyCVS/Parser/grammar1.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/grammar1.c	Fri Feb 25 08:46:17 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* Grammar subroutines needed by parser */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "assert.h"
  #include "grammar.h"
diff -cr PyCVS/Parser/listnode.c PyMem/Parser/listnode.c
*** PyCVS/Parser/listnode.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/listnode.c	Fri Feb 25 08:46:46 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* List a node on a file */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "token.h"
  #include "node.h"
Only in PyMem/Parser/: mymalloc.c
diff -cr PyCVS/Parser/myreadline.c PyMem/Parser/myreadline.c
*** PyCVS/Parser/myreadline.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/myreadline.c	Thu Feb 24 18:37:41 2000
***************
*** 40,45 ****
--- 40,46 ----
  */
  
  #include "Python.h"
+ #include "pycore.h"
  
  int (*PyOS_InputHook)() = NULL;
  
***************
*** 89,95 ****
  	int n;
  	char *p;
  	n = 100;
! 	if ((p = malloc(n)) == NULL)
  		return NULL;
  	fflush(stdout);
  	if (prompt)
--- 90,96 ----
  	int n;
  	char *p;
  	n = 100;
! 	if ((p = PyMem_MALLOC(n)) == NULL)
  		return NULL;
  	fflush(stdout);
  	if (prompt)
***************
*** 99,105 ****
  	case 0: /* Normal case */
  		break;
  	case 1: /* Interrupt */
! 		free(p);
  		return NULL;
  	case -1: /* EOF */
  	case -2: /* Error */
--- 100,106 ----
  	case 0: /* Normal case */
  		break;
  	case 1: /* Interrupt */
! 		PyMem_FREE(p);
  		return NULL;
  	case -1: /* EOF */
  	case -2: /* Error */
***************
*** 117,135 ****
  	n = strlen(p);
  	while (n > 0 && p[n-1] != '\n') {
  		int incr = n+2;
! 		p = realloc(p, n + incr);
  		if (p == NULL)
  			return NULL;
  		if (my_fgets(p+n, incr, stdin) != 0)
  			break;
  		n += strlen(p+n);
  	}
! 	return realloc(p, n+1);
  }
  
  
  /* By initializing this function pointer, systems embedding Python can
!    override the readline function. */
  
  char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  
--- 118,138 ----
  	n = strlen(p);
  	while (n > 0 && p[n-1] != '\n') {
  		int incr = n+2;
! 		p = PyMem_REALLOC(p, n + incr);
  		if (p == NULL)
  			return NULL;
  		if (my_fgets(p+n, incr, stdin) != 0)
  			break;
  		n += strlen(p+n);
  	}
! 	return PyMem_REALLOC(p, n+1);
  }
  
  
  /* By initializing this function pointer, systems embedding Python can
!    override the readline function.
! 
!    Note: Python expects in return a buffer allocated with PyMem_Malloc. */
  
  char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  
diff -cr PyCVS/Parser/node.c PyMem/Parser/node.c
*** PyCVS/Parser/node.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/node.c	Thu Feb 24 16:31:59 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* Parse tree node implementation */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "node.h"
  
diff -cr PyCVS/Parser/parser.c PyMem/Parser/parser.c
*** PyCVS/Parser/parser.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/parser.c	Thu Feb 24 16:32:44 2000
***************
*** 35,40 ****
--- 35,41 ----
  
  /* XXX To do: error recovery */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "assert.h"
  #include "token.h"
diff -cr PyCVS/Parser/parsetok.c PyMem/Parser/parsetok.c
*** PyCVS/Parser/parsetok.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/parsetok.c	Fri Feb 25 07:40:36 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* Parser-tokenizer link implementation */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  #include "tokenizer.h"
  #include "node.h"
***************
*** 192,198 ****
  		err_ret->offset = tok->cur - tok->buf;
  		if (tok->buf != NULL) {
  			int len = tok->inp - tok->buf;
! 			err_ret->text = malloc(len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)
  					strncpy(err_ret->text, tok->buf, len);
--- 193,199 ----
  		err_ret->offset = tok->cur - tok->buf;
  		if (tok->buf != NULL) {
  			int len = tok->inp - tok->buf;
! 			err_ret->text = PyMem_NEW(char, len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)
  					strncpy(err_ret->text, tok->buf, len);
diff -cr PyCVS/Parser/pgenmain.c PyMem/Parser/pgenmain.c
*** PyCVS/Parser/pgenmain.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/pgenmain.c	Fri Feb 25 07:41:32 2000
***************
*** 139,145 ****
  					putc(' ', stderr);
  			}
  			fprintf(stderr, "^\n");
! 			free(err.text);
  		}
  		Py_Exit(1);
  	}
--- 139,145 ----
  					putc(' ', stderr);
  			}
  			fprintf(stderr, "^\n");
! 			PyMem_DEL(err.text);
  		}
  		Py_Exit(1);
  	}
***************
*** 196,202 ****
  	char *prompt;
  {
  	int n = 1000;
! 	char *p = malloc(n);
  	char *q;
  	if (p == NULL)
  		return NULL;
--- 196,202 ----
  	char *prompt;
  {
  	int n = 1000;
! 	char *p = PyMem_Malloc(n);
  	char *q;
  	if (p == NULL)
  		return NULL;
***************
*** 209,215 ****
  	n = strlen(p);
  	if (n > 0 && p[n-1] != '\n')
  		p[n-1] = '\n';
! 	return realloc(p, n+1);
  }
  
  #ifdef HAVE_STDARG_PROTOTYPES
--- 209,215 ----
  	n = strlen(p);
  	if (n > 0 && p[n-1] != '\n')
  		p[n-1] = '\n';
! 	return PyMem_Realloc(p, n+1);
  }
  
  #ifdef HAVE_STDARG_PROTOTYPES
diff -cr PyCVS/Parser/tokenizer.c PyMem/Parser/tokenizer.c
*** PyCVS/Parser/tokenizer.c	Thu Feb 24 14:49:48 2000
--- PyMem/Parser/tokenizer.c	Thu Feb 24 18:52:41 2000
***************
*** 31,36 ****
--- 31,37 ----
  
  /* Tokenizer implementation */
  
+ #include "pycore.h"
  #include "pgenheaders.h"
  
  #include <ctype.h>
***************
*** 219,244 ****
  			if (new == NULL)
  				tok->done = E_INTR;
  			else if (*new == '\0') {
! 				free(new);
  				tok->done = E_EOF;
  			}
  			else if (tok->start != NULL) {
  				int start = tok->start - tok->buf;
  				int oldlen = tok->cur - tok->buf;
  				int newlen = oldlen + strlen(new);
! 				char *buf = realloc(tok->buf, newlen+1);
  				tok->lineno++;
  				if (buf == NULL) {
! 					free(tok->buf);
  					tok->buf = NULL;
! 					free(new);
  					tok->done = E_NOMEM;
  					return EOF;
  				}
  				tok->buf = buf;
  				tok->cur = tok->buf + oldlen;
  				strcpy(tok->buf + oldlen, new);
! 				free(new);
  				tok->inp = tok->buf + newlen;
  				tok->end = tok->inp + 1;
  				tok->start = tok->buf + start;
--- 220,246 ----
  			if (new == NULL)
  				tok->done = E_INTR;
  			else if (*new == '\0') {
! 				PyMem_FREE(new);
  				tok->done = E_EOF;
  			}
  			else if (tok->start != NULL) {
  				int start = tok->start - tok->buf;
  				int oldlen = tok->cur - tok->buf;
  				int newlen = oldlen + strlen(new);
! 				char *buf = tok->buf;
! 				PyMem_RESIZE(buf, char, newlen+1);
  				tok->lineno++;
  				if (buf == NULL) {
! 					PyMem_DEL(tok->buf);
  					tok->buf = NULL;
! 					PyMem_FREE(new);
  					tok->done = E_NOMEM;
  					return EOF;
  				}
  				tok->buf = buf;
  				tok->cur = tok->buf + oldlen;
  				strcpy(tok->buf + oldlen, new);
! 				PyMem_FREE(new);
  				tok->inp = tok->buf + newlen;
  				tok->end = tok->inp + 1;
  				tok->start = tok->buf + start;
***************
*** 246,252 ****
  			else {
  				tok->lineno++;
  				if (tok->buf != NULL)
! 					free(tok->buf);
  				tok->buf = new;
  				tok->cur = tok->buf;
  				tok->inp = strchr(tok->buf, '\0');
--- 248,254 ----
  			else {
  				tok->lineno++;
  				if (tok->buf != NULL)
! 					PyMem_DEL(tok->buf);
  				tok->buf = new;
  				tok->cur = tok->buf;
  				tok->inp = strchr(tok->buf, '\0');