Skip to content

Commit 9ac11a7

Browse files
committed
properly free memory in pgen
1 parent 254ad58 commit 9ac11a7

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

Include/grammar.h

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct {
6969
/* FUNCTIONS */
7070

7171
grammar *newgrammar(int start);
72+
void freegrammar(grammar *g);
7273
dfa *adddfa(grammar *g, int type, const char *name);
7374
int addstate(dfa *d);
7475
void addarc(dfa *d, int from, int to, int lbl);

Parser/grammar.c

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ newgrammar(int start)
2828
return g;
2929
}
3030

31+
void
32+
freegrammar(grammar *g)
33+
{
34+
int i;
35+
for (i = 0; i < g->g_ndfas; i++) {
36+
free(g->g_dfa[i].d_name);
37+
for (int j = 0; j < g->g_dfa[i].d_nstates; j++)
38+
PyObject_FREE(g->g_dfa[i].d_state[j].s_arc);
39+
PyObject_FREE(g->g_dfa[i].d_state);
40+
}
41+
PyObject_FREE(g->g_dfa);
42+
for (i = 0; i < g->g_ll.ll_nlabels; i++)
43+
free(g->g_ll.ll_label[i].lb_str);
44+
PyObject_FREE(g->g_ll.ll_label);
45+
PyObject_FREE(g);
46+
}
47+
3148
dfa *
3249
adddfa(grammar *g, int type, const char *name)
3350
{

Parser/pgen.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ newnfagrammar(void)
117117
return gr;
118118
}
119119

120+
static void
121+
freenfagrammar(nfagrammar *gr)
122+
{
123+
for (int i = 0; i < gr->gr_nnfas; i++) {
124+
PyObject_FREE(gr->gr_nfa[i]->nf_state);
125+
}
126+
PyObject_FREE(gr->gr_nfa);
127+
PyObject_FREE(gr);
128+
}
129+
120130
static nfa *
121131
addnfa(nfagrammar *gr, char *name)
122132
{
@@ -488,7 +498,11 @@ makedfa(nfagrammar *gr, nfa *nf, dfa *d)
488498

489499
convert(d, xx_nstates, xx_state);
490500

491-
/* XXX cleanup */
501+
for (int i = 0; i < xx_nstates; i++) {
502+
for (int j = 0; j < xx_state[i].ss_narcs; j++)
503+
delbitset(xx_state[i].ss_arc[j].sa_bitset);
504+
PyObject_FREE(xx_state[i].ss_arc);
505+
}
492506
PyObject_FREE(xx_state);
493507
}
494508

@@ -669,7 +683,7 @@ pgen(node *n)
669683
g = maketables(gr);
670684
translatelabels(g);
671685
addfirstsets(g);
672-
PyObject_FREE(gr);
686+
freenfagrammar(gr);
673687
return g;
674688
}
675689

Parser/pgenmain.c

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ main(int argc, char **argv)
8080
printf("Writing %s ...\n", graminit_h);
8181
printnonterminals(g, fp);
8282
fclose(fp);
83+
freegrammar(g);
8384
Py_Exit(0);
8485
return 0; /* Make gcc -Wall happy */
8586
}

0 commit comments

Comments
 (0)