Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80d4e2a

Browse files
author
Andrew MacIntyre
committed
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.
1 parent 4104db3 commit 80d4e2a

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

Parser/acceler.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ PyGrammar_RemoveAccelerators(grammar *g)
4444
s = d->d_state;
4545
for (j = 0; j < d->d_nstates; j++, s++) {
4646
if (s->s_accel)
47-
PyMem_DEL(s->s_accel);
47+
PyObject_FREE(s->s_accel);
4848
s->s_accel = NULL;
4949
}
5050
}
@@ -68,7 +68,7 @@ fixstate(grammar *g, state *s)
6868
int *accel;
6969
int nl = g->g_ll.ll_nlabels;
7070
s->s_accept = 0;
71-
accel = PyMem_NEW(int, nl);
71+
accel = (int *) PyObject_MALLOC(nl * sizeof(int));
7272
for (k = 0; k < nl; k++)
7373
accel[k] = -1;
7474
a = s->s_arc;
@@ -124,7 +124,7 @@ fixstate(grammar *g, state *s)
124124
k++;
125125
if (k < nl) {
126126
int i;
127-
s->s_accel = PyMem_NEW(int, nl-k);
127+
s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
128128
if (s->s_accel == NULL) {
129129
fprintf(stderr, "no mem to add parser accelerators\n");
130130
exit(1);
@@ -134,5 +134,5 @@ fixstate(grammar *g, state *s)
134134
for (i = 0; k < nl; i++, k++)
135135
s->s_accel[i] = accel[k];
136136
}
137-
PyMem_DEL(accel);
137+
PyObject_FREE(accel);
138138
}

Parser/node.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
node *
88
PyNode_New(int type)
99
{
10-
node *n = PyMem_NEW(node, 1);
10+
node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
1111
if (n == NULL)
1212
return NULL;
1313
n->n_type = type;
@@ -92,7 +92,8 @@ PyNode_AddChild(register node *n1, int type, char *str, int lineno)
9292
return E_OVERFLOW;
9393
if (current_capacity < required_capacity) {
9494
n = n1->n_child;
95-
PyMem_RESIZE(n, node, required_capacity);
95+
n = (node *) PyObject_REALLOC(n,
96+
required_capacity * sizeof(node));
9697
if (n == NULL)
9798
return E_NOMEM;
9899
n1->n_child = n;
@@ -116,7 +117,7 @@ PyNode_Free(node *n)
116117
{
117118
if (n != NULL) {
118119
freechildren(n);
119-
PyMem_DEL(n);
120+
PyObject_FREE(n);
120121
}
121122
}
122123

@@ -127,7 +128,7 @@ freechildren(node *n)
127128
for (i = NCH(n); --i >= 0; )
128129
freechildren(CHILD(n, i));
129130
if (n->n_child != NULL)
130-
PyMem_DEL(n->n_child);
131+
PyObject_FREE(n->n_child);
131132
if (STR(n) != NULL)
132-
PyMem_DEL(STR(n));
133+
PyObject_FREE(STR(n));
133134
}

Parser/parsetok.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
133133
else
134134
started = 1;
135135
len = b - a; /* XXX this may compute NULL - NULL */
136-
str = PyMem_NEW(char, len + 1);
136+
str = (char *) PyObject_MALLOC(len + 1);
137137
if (str == NULL) {
138138
fprintf(stderr, "no mem for next token\n");
139139
err_ret->error = E_NOMEM;
@@ -157,7 +157,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
157157
PyParser_AddToken(ps, (int)type, str, tok->lineno,
158158
&(err_ret->expected))) != E_OK) {
159159
if (err_ret->error != E_DONE)
160-
PyMem_DEL(str);
160+
PyObject_FREE(str);
161161
break;
162162
}
163163
}
@@ -178,7 +178,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
178178
err_ret->offset = tok->cur - tok->buf;
179179
if (tok->buf != NULL) {
180180
size_t len = tok->inp - tok->buf;
181-
err_ret->text = PyMem_NEW(char, len + 1);
181+
err_ret->text = (char *) PyObject_MALLOC(len + 1);
182182
if (err_ret->text != NULL) {
183183
if (len > 0)
184184
strncpy(err_ret->text, tok->buf, len);

Python/compile.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ com_free(struct compiling *c)
719719
Py_XDECREF(c->c_cellvars);
720720
Py_XDECREF(c->c_lnotab);
721721
if (c->c_future)
722-
PyMem_Free((void *)c->c_future);
722+
PyObject_FREE((void *)c->c_future);
723723
}
724724

725725
static void
@@ -2020,15 +2020,15 @@ com_factor(struct compiling *c, node *n)
20202020
return;
20212021
}
20222022
if (childtype == MINUS) {
2023-
char *s = PyMem_Malloc(strlen(STR(pnum)) + 2);
2023+
char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
20242024
if (s == NULL) {
20252025
com_error(c, PyExc_MemoryError, "");
20262026
com_addbyte(c, 255);
20272027
return;
20282028
}
20292029
s[0] = '-';
20302030
strcpy(s + 1, STR(pnum));
2031-
PyMem_Free(STR(pnum));
2031+
PyObject_FREE(STR(pnum));
20322032
STR(pnum) = s;
20332033
}
20342034
com_atom(c, patom);
@@ -4116,7 +4116,7 @@ PyNode_CompileSymtable(node *n, char *filename)
41164116

41174117
st = symtable_init();
41184118
if (st == NULL) {
4119-
PyMem_Free((void *)ff);
4119+
PyObject_FREE((void *)ff);
41204120
return NULL;
41214121
}
41224122
st->st_future = ff;
@@ -4129,7 +4129,7 @@ PyNode_CompileSymtable(node *n, char *filename)
41294129

41304130
return st;
41314131
fail:
4132-
PyMem_Free((void *)ff);
4132+
PyObject_FREE((void *)ff);
41334133
st->st_future = NULL;
41344134
PySymtable_Free(st);
41354135
return NULL;
@@ -4722,7 +4722,7 @@ symtable_init()
47224722
{
47234723
struct symtable *st;
47244724

4725-
st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4725+
st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable));
47264726
if (st == NULL)
47274727
return NULL;
47284728
st->st_pass = 1;
@@ -4749,7 +4749,7 @@ PySymtable_Free(struct symtable *st)
47494749
Py_XDECREF(st->st_symbols);
47504750
Py_XDECREF(st->st_stack);
47514751
Py_XDECREF(st->st_cur);
4752-
PyMem_Free((void *)st);
4752+
PyObject_FREE((void *)st);
47534753
}
47544754

47554755
/* When the compiler exits a scope, it must should update the scope's

0 commit comments

Comments
 (0)