[Python-checkins] r51012 - in python/branches/bcannon-sandboxing: Include/objimpl.h Include/pymem.h Objects/trackedmalloc.c Parser/acceler.c Parser/bitset.c Parser/firstsets.c Parser/grammar.c Parser/node.c Parser/parser.c Parser/parsetok.c Parser/pgen.c

brett.cannon python-checkins at python.org
Tue Aug 1 03:21:33 CEST 2006


Author: brett.cannon
Date: Tue Aug  1 03:21:31 2006
New Revision: 51012

Modified:
   python/branches/bcannon-sandboxing/Include/objimpl.h
   python/branches/bcannon-sandboxing/Include/pymem.h
   python/branches/bcannon-sandboxing/Objects/trackedmalloc.c
   python/branches/bcannon-sandboxing/Parser/acceler.c
   python/branches/bcannon-sandboxing/Parser/bitset.c
   python/branches/bcannon-sandboxing/Parser/firstsets.c
   python/branches/bcannon-sandboxing/Parser/grammar.c
   python/branches/bcannon-sandboxing/Parser/node.c
   python/branches/bcannon-sandboxing/Parser/parser.c
   python/branches/bcannon-sandboxing/Parser/parsetok.c
   python/branches/bcannon-sandboxing/Parser/pgen.c
Log:
Move Parser/ over to tracked memory usage.

Introduced PyMem_RAW_*() macros and forced PyObject_MALLOC() and friend over to
PyObject_T_MALLOC().


Modified: python/branches/bcannon-sandboxing/Include/objimpl.h
==============================================================================
--- python/branches/bcannon-sandboxing/Include/objimpl.h	(original)
+++ python/branches/bcannon-sandboxing/Include/objimpl.h	Tue Aug  1 03:21:31 2006
@@ -108,16 +108,27 @@
 PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
 PyAPI_FUNC(void) PyObject_Free(void *);
 
-
-/* Macros */
-#ifdef WITH_PYMALLOC
-#if defined(PYMALLOC_DEBUG)	/* WITH_PYMALLOC && PYMALLOC_DEBUG */
+#ifdef PYMALLOC_DEBUG
 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
 PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
+#endif
+
+
+/* Macros */
+#ifdef WITH_PYMALLOC
+
+#ifdef Py_TRACK_MEMORY
+
+#define PyObject_MALLOC(nbytes)	PyObject_T_MALLOC("", nbytes)
+#define PyObject_REALLOC(ptr, nbytes)	PyObject_T_REALLOC("", ptr, nbytes)
+#define	PyObject_FREE(ptr)	PyObject_T_FREE("", ptr)
+
+#elif defined(PYMALLOC_DEBUG)	/* WITH_PYMALLOC && PYMALLOC_DEBUG */
+
 #define PyObject_MALLOC		_PyObject_DebugMalloc
 #define PyObject_Malloc		_PyObject_DebugMalloc
 #define PyObject_REALLOC	_PyObject_DebugRealloc

Modified: python/branches/bcannon-sandboxing/Include/pymem.h
==============================================================================
--- python/branches/bcannon-sandboxing/Include/pymem.h	(original)
+++ python/branches/bcannon-sandboxing/Include/pymem.h	Tue Aug  1 03:21:31 2006
@@ -73,6 +73,16 @@
 
 #endif	/* PYMALLOC_DEBUG */
 
+#ifdef PYMALLOC
+#define PyMem_RAW_MALLOC	PyObject_Malloc
+#define PyMem_RAW_REALLOC	PyObject_Realloc
+#define PyMem_RAW_FREE		PyObject_Free
+#else
+#define PyMem_RAW_MALLOC(n)	malloc((n) ? (n) : 1)
+#define PyMem_RAW_REALLOC(p, n)	realloc((p), (n) ? (n) : 1)
+#define PyMem_RAW_FREE		free
+#endif
+
 /*
  * Type-oriented memory interface
  * ==============================

Modified: python/branches/bcannon-sandboxing/Objects/trackedmalloc.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/trackedmalloc.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/trackedmalloc.c	Tue Aug  1 03:21:31 2006
@@ -103,7 +103,8 @@
 {
     struct mem_item *cur_mem = mem_head;
 
-    what = what ? what : UNKNOWN_WHAT;
+    if (!what || (strcmp(what, "") == 0))
+	what = UNKNOWN_WHAT;
 
     while (cur_mem->next) {
 	cur_mem = cur_mem->next;

Modified: python/branches/bcannon-sandboxing/Parser/acceler.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/acceler.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/acceler.c	Tue Aug  1 03:21:31 2006
@@ -44,7 +44,7 @@
 		s = d->d_state;
 		for (j = 0; j < d->d_nstates; j++, s++) {
 			if (s->s_accel)
-				PyObject_FREE(s->s_accel);
+				PyObject_Free(s->s_accel);
 			s->s_accel = NULL;
 		}
 	}
@@ -68,7 +68,7 @@
 	int *accel;
 	int nl = g->g_ll.ll_nlabels;
 	s->s_accept = 0;
-	accel = (int *) PyObject_MALLOC(nl * sizeof(int));
+	accel = (int *) PyObject_Malloc(nl * sizeof(int));
 	if (accel == NULL) {
 		fprintf(stderr, "no mem to build parser accelerators\n");
 		exit(1);
@@ -111,7 +111,7 @@
 		k++;
 	if (k < nl) {
 		int i;
-		s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
+		s->s_accel = (int *) PyObject_Malloc((nl-k) * sizeof(int));
 		if (s->s_accel == NULL) {
 			fprintf(stderr, "no mem to add parser accelerators\n");
 			exit(1);
@@ -121,5 +121,5 @@
 		for (i = 0; k < nl; i++, k++)
 			s->s_accel[i] = accel[k];
 	}
-	PyObject_FREE(accel);
+	PyObject_Free(accel);
 }

Modified: python/branches/bcannon-sandboxing/Parser/bitset.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/bitset.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/bitset.c	Tue Aug  1 03:21:31 2006
@@ -8,7 +8,7 @@
 newbitset(int nbits)
 {
 	int nbytes = NBYTES(nbits);
-	bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) *  nbytes);
+	bitset ss = (char *)PyObject_Malloc(sizeof(BYTE) *  nbytes);
 	
 	if (ss == NULL)
 		Py_FatalError("no mem for bitset");
@@ -22,7 +22,7 @@
 void
 delbitset(bitset ss)
 {
-	PyObject_FREE(ss);
+	PyObject_Free(ss);
 }
 
 int

Modified: python/branches/bcannon-sandboxing/Parser/firstsets.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/firstsets.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/firstsets.c	Tue Aug  1 03:21:31 2006
@@ -59,7 +59,7 @@
 	nbits = g->g_ll.ll_nlabels;
 	result = newbitset(nbits);
 	
-	sym = (int *)PyObject_MALLOC(sizeof(int));
+	sym = (int *)PyObject_Malloc(sizeof(int));
 	if (sym == NULL)
 		Py_FatalError("no mem for new sym in calcfirstset");
 	nsyms = 1;
@@ -73,7 +73,7 @@
 				break;
 		}
 		if (j >= nsyms) { /* New label */
-			sym = (int *)PyObject_REALLOC(sym, 
+			sym = (int *)PyObject_Realloc(sym, 
                                                 sizeof(int) * (nsyms + 1));
 			if (sym == NULL)
 				Py_FatalError(
@@ -109,5 +109,5 @@
 		printf(" }\n");
 	}
 
-	PyObject_FREE(sym);
+	PyObject_Free(sym);
 }

Modified: python/branches/bcannon-sandboxing/Parser/grammar.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/grammar.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/grammar.c	Tue Aug  1 03:21:31 2006
@@ -20,7 +20,7 @@
 {
 	grammar *g;
 	
-	g = (grammar *)PyObject_MALLOC(sizeof(grammar));
+	g = (grammar *)PyObject_Malloc(sizeof(grammar));
 	if (g == NULL)
 		Py_FatalError("no mem for new grammar");
 	g->g_ndfas = 0;
@@ -37,7 +37,7 @@
 {
 	dfa *d;
 	
-	g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa, 
+	g->g_dfa = (dfa *)PyObject_Realloc(g->g_dfa, 
                                             sizeof(dfa) * (g->g_ndfas + 1));
 	if (g->g_dfa == NULL)
 		Py_FatalError("no mem to resize dfa in adddfa");
@@ -56,7 +56,7 @@
 {
 	state *s;
 	
-	d->d_state = (state *)PyObject_REALLOC(d->d_state,
+	d->d_state = (state *)PyObject_Realloc(d->d_state,
 				      sizeof(state) * (d->d_nstates + 1));
 	if (d->d_state == NULL)
 		Py_FatalError("no mem to resize state in addstate");
@@ -80,7 +80,7 @@
 	assert(0 <= to && to < d->d_nstates);
 	
 	s = &d->d_state[from];
-	s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
+	s->s_arc = (arc *)PyObject_Realloc(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
 	if (s->s_arc == NULL)
 		Py_FatalError("no mem to resize arc list in addarc");
 	a = &s->s_arc[s->s_narcs++];
@@ -99,7 +99,7 @@
 			strcmp(ll->ll_label[i].lb_str, str) == 0)
 			return i;
 	}
-	ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
+	ll->ll_label = (label *)PyObject_Realloc(ll->ll_label,
 					sizeof(label) * (ll->ll_nlabels + 1));
 	if (ll->ll_label == NULL)
 		Py_FatalError("no mem to resize labellist in addlabel");

Modified: python/branches/bcannon-sandboxing/Parser/node.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/node.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/node.c	Tue Aug  1 03:21:31 2006
@@ -7,7 +7,7 @@
 node *
 PyNode_New(int type)
 {
-	node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
+	node *n = (node *) PyObject_Malloc(1 * sizeof(node));
 	if (n == NULL)
 		return NULL;
 	n->n_type = type;
@@ -92,7 +92,7 @@
 		return E_OVERFLOW;
 	if (current_capacity < required_capacity) {
 		n = n1->n_child;
-		n = (node *) PyObject_REALLOC(n,
+		n = (node *) PyObject_Realloc(n,
 					      required_capacity * sizeof(node));
 		if (n == NULL)
 			return E_NOMEM;
@@ -118,7 +118,7 @@
 {
 	if (n != NULL) {
 		freechildren(n);
-		PyObject_FREE(n);
+		PyObject_Free(n);
 	}
 }
 
@@ -129,7 +129,7 @@
 	for (i = NCH(n); --i >= 0; )
 		freechildren(CHILD(n, i));
 	if (n->n_child != NULL)
-		PyObject_FREE(n->n_child);
+		PyObject_Free(n->n_child);
 	if (STR(n) != NULL)
-		PyObject_FREE(STR(n));
+		PyObject_Free(STR(n));
 }

Modified: python/branches/bcannon-sandboxing/Parser/parser.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/parser.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/parser.c	Tue Aug  1 03:21:31 2006
@@ -75,7 +75,7 @@
 	
 	if (!g->g_accel)
 		PyGrammar_AddAccelerators(g);
-	ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
+	ps = (parser_state *)PyMem_RAW_MALLOC(sizeof(parser_state));
 	if (ps == NULL)
 		return NULL;
 	ps->p_grammar = g;
@@ -84,7 +84,7 @@
 #endif
 	ps->p_tree = PyNode_New(start);
 	if (ps->p_tree == NULL) {
-		PyMem_FREE(ps);
+		PyMem_RAW_FREE(ps);
 		return NULL;
 	}
 	s_reset(&ps->p_stack);
@@ -98,7 +98,7 @@
 	/* NB If you want to save the parse tree,
 	   you must set p_tree to NULL before calling delparser! */
 	PyNode_Free(ps->p_tree);
-	PyMem_FREE(ps);
+	PyMem_RAW_FREE(ps);
 }
 
 

Modified: python/branches/bcannon-sandboxing/Parser/parsetok.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/parsetok.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/parsetok.c	Tue Aug  1 03:21:31 2006
@@ -154,7 +154,7 @@
 		else
 			started = 1;
 		len = b - a; /* XXX this may compute NULL - NULL */
-		str = (char *) PyObject_MALLOC(len + 1);
+		str = (char *) PyObject_Malloc(len + 1);
 		if (str == NULL) {
 			fprintf(stderr, "no mem for next token\n");
 			err_ret->error = E_NOMEM;
@@ -195,7 +195,7 @@
 		     PyParser_AddToken(ps, (int)type, str, tok->lineno, col_offset,
 				       &(err_ret->expected))) != E_OK) {
 			if (err_ret->error != E_DONE) {
-				PyObject_FREE(str);
+				PyObject_Free(str);
 				err_ret->token = type;
 			}				
 			break;
@@ -220,7 +220,7 @@
 			assert(tok->cur - tok->buf < INT_MAX);
 			err_ret->offset = (int)(tok->cur - tok->buf);
 			len = tok->inp - tok->buf;
-			err_ret->text = (char *) PyObject_MALLOC(len + 1);
+			err_ret->text = (char *) PyObject_Malloc(len + 1);
 			if (err_ret->text != NULL) {
 				if (len > 0)
 					strncpy(err_ret->text, tok->buf, len);

Modified: python/branches/bcannon-sandboxing/Parser/pgen.c
==============================================================================
--- python/branches/bcannon-sandboxing/Parser/pgen.c	(original)
+++ python/branches/bcannon-sandboxing/Parser/pgen.c	Tue Aug  1 03:21:31 2006
@@ -49,7 +49,7 @@
 {
 	nfastate *st;
 	
-	nf->nf_state = (nfastate *)PyObject_REALLOC(nf->nf_state, 
+	nf->nf_state = (nfastate *)PyObject_Realloc(nf->nf_state, 
                                     sizeof(nfastate) * (nf->nf_nstates + 1));
 	if (nf->nf_state == NULL)
 		Py_FatalError("out of mem");
@@ -66,7 +66,7 @@
 	nfaarc *ar;
 	
 	st = &nf->nf_state[from];
-	st->st_arc = (nfaarc *)PyObject_REALLOC(st->st_arc,
+	st->st_arc = (nfaarc *)PyObject_Realloc(st->st_arc,
 				      sizeof(nfaarc) * (st->st_narcs + 1));
 	if (st->st_arc == NULL)
 		Py_FatalError("out of mem");
@@ -81,7 +81,7 @@
 	nfa *nf;
 	static int type = NT_OFFSET; /* All types will be disjunct */
 	
-	nf = (nfa *)PyObject_MALLOC(sizeof(nfa));
+	nf = (nfa *)PyObject_Malloc(sizeof(nfa));
 	if (nf == NULL)
 		Py_FatalError("no mem for new nfa");
 	nf->nf_type = type++;
@@ -106,7 +106,7 @@
 {
 	nfagrammar *gr;
 	
-	gr = (nfagrammar *)PyObject_MALLOC(sizeof(nfagrammar));
+	gr = (nfagrammar *)PyObject_Malloc(sizeof(nfagrammar));
 	if (gr == NULL)
 		Py_FatalError("no mem for new nfa grammar");
 	gr->gr_nnfas = 0;
@@ -123,7 +123,7 @@
 	nfa *nf;
 	
 	nf = newnfa(name);
-	gr->gr_nfa = (nfa **)PyObject_REALLOC(gr->gr_nfa,
+	gr->gr_nfa = (nfa **)PyObject_Realloc(gr->gr_nfa,
 				      sizeof(nfa) * (gr->gr_nnfas + 1));
 	if (gr->gr_nfa == NULL)
 		Py_FatalError("out of mem");
@@ -395,7 +395,7 @@
 	
 	ss = newbitset(nbits);
 	addclosure(ss, nf, nf->nf_start);
-	xx_state = (ss_state *)PyObject_MALLOC(sizeof(ss_state));
+	xx_state = (ss_state *)PyObject_Malloc(sizeof(ss_state));
 	if (xx_state == NULL)
 		Py_FatalError("no mem for xx_state in makedfa");
 	xx_nstates = 1;
@@ -435,7 +435,7 @@
 				}
 				/* Add new arc for this state */
 				size = sizeof(ss_arc) * (yy->ss_narcs + 1);
-				yy->ss_arc = (ss_arc *)PyObject_REALLOC(
+				yy->ss_arc = (ss_arc *)PyObject_Realloc(
                                                             yy->ss_arc, size);
 				if (yy->ss_arc == NULL)
 					Py_FatalError("out of mem");
@@ -459,7 +459,7 @@
 				}
 			}
 			size = sizeof(ss_state) * (xx_nstates + 1);
-			xx_state = (ss_state *)PyObject_REALLOC(xx_state, 
+			xx_state = (ss_state *)PyObject_Realloc(xx_state, 
                                                                     size);
 			if (xx_state == NULL)
 				Py_FatalError("out of mem");


More information about the Python-checkins mailing list