Commit 781bfa96 authored by Michaël Cadilhac's avatar Michaël Cadilhac
Browse files

2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>

	* src/emit.c,
	* src/rule.c,
	* src/emit_kids.c,
	* src/rule.h,
	* src/emit_files.c,
	* src/parser.y,
	* src/main.c,
	* src/emit_cost.c: Be consistent in memory use in order to erase
	memory leaks.
parent bb7046b2
2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* src/emit.c,
* src/rule.c,
* src/emit_kids.c,
* src/rule.h,
* src/emit_files.c,
* src/parser.y,
* src/main.c,
* src/emit_cost.c: Be consistent in memory use in order to erase
memory leaks.
2005-09-09 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* src/monoburg.h: Add a `with_line' variable which tells if
......
......@@ -432,13 +432,17 @@ void emit_label_func ()
static char *compute_nonterms (Tree *tree)
{
if (!tree)
return "";
return g_strdup ("");
if (tree->nonterm) {
return g_strdup_printf ("MB_NTERM_%s, ", tree->nonterm->name);
} else {
return g_strconcat (compute_nonterms (tree->left),
compute_nonterms (tree->right), NULL);
char *s_left = compute_nonterms (tree->left);
char *s_right = compute_nonterms (tree->right);
char *s_result = g_strconcat (s_left, s_right, NULL);
g_free (s_left);
g_free (s_right);
return s_result;
}
}
......@@ -520,6 +524,8 @@ void emit_vardefs ()
if (j == c) {
output ("static const guint16 mono_burg_nts_%d [] = { %s0 };\n", c, s);
sa [c++] = s;
} else {
g_free (s);
}
}
output ("\n");
......@@ -533,6 +539,11 @@ void emit_vardefs ()
output ("\tmono_burg_nts_%d, ", si [i++]);
emit_rule_string (rule, "");
}
while (c--)
g_free (sa [c]);
g_free (sa);
g_free (si);
output ("};\n\n");
}
......
......@@ -106,6 +106,7 @@ void emit_costs (char *st, Tree *t)
if (t->right) {
tn = g_strconcat (st, "right->", NULL);
emit_costs (tn, t->right);
g_free (tn);
}
} else
output ("%scost[MB_NTERM_%s] + ", st, t->nonterm->name);
......
......@@ -67,4 +67,7 @@ void emit_code_file ()
emit_kids ();
while (n_namespace--)
output ("}\n");
free_rules ();
free_terms ();
free_nonterms ();
}
......@@ -95,6 +95,8 @@ void emit_kids ()
si [i++] = j;
if (j == c)
sa [c++] = k;
else
g_free (k);
}
for (i = 0; i < c; i++) {
......@@ -104,6 +106,10 @@ void emit_kids ()
output ("%s", sa [i]);
output ("\t\tbreak;\n");
}
while (c--)
g_free (sa [c]);
g_free (sa);
g_free (si);
output ("\tdefault:\n\t\tg_assert_not_reached ();\n");
output ("\t}\n");
......@@ -122,23 +128,35 @@ static char *compute_kids (char *ts, Tree *tree, int *n)
return g_strdup_printf ("\t\tkids[%d] = %s;\n", (*n)++, ts);
} else if (tree->op && tree->op->arity) {
char *res2 = NULL;
char *tmp;
char *result;
if (dag_mode) {
res = compute_kids (g_strdup_printf ("%s->left", ts),
tree->left, n);
if (tree->op->arity == 2)
res2 = compute_kids (g_strdup_printf ("%s->right", ts),
tree->right, n);
tmp = g_strdup_printf ("%s->left", ts);
res = compute_kids (tmp, tree->left, n);
g_free (tmp);
if (tree->op->arity == 2) {
tmp = g_strdup_printf ("%s->right", ts);
res2 = compute_kids (tmp, tree->right, n);
g_free (tmp);
}
} else {
res = compute_kids (g_strdup_printf ("MBTREE_LEFT(%s)", ts),
tree->left, n);
tmp = g_strdup_printf ("MBTREE_LEFT(%s)", ts);
res = compute_kids (tmp, tree->left, n);
g_free (tmp);
if (tree->op->arity == 2)
res2 = compute_kids (g_strdup_printf ("MBTREE_RIGHT(%s)", ts),
tree->right, n);
{
tmp = g_strdup_printf ("MBTREE_RIGHT(%s)", ts);
res2 = compute_kids (tmp, tree->right, n);
g_free (tmp);
}
}
return g_strconcat (res, res2, NULL);
result = g_strconcat (res, res2, NULL);
g_free (res);
g_free (res2);
return result;
}
return "";
return g_strdup ("");
}
......@@ -269,5 +269,25 @@ int main (int argc, char **argv)
if (cfile)
fclose (cfd);
/* Memory: ``Free me, I'm famous !''. */
g_hash_table_destroy (definedvars);
{
GList *include_dir;
for (include_dir = include_dirs->next;
include_dir; include_dir = include_dir->next) {
g_free (include_dir->data);
include_dir = include_dir->next;
}
}
g_list_free (include_dirs);
#ifndef NDEBUG
g_list_pop_allocator ();
g_allocator_free (list_allocator);
#endif
/* Remove log handler. */
g_log_remove_handler (NULL, handler_id);
g_free (g_get_prgname ());
return 0;
}
......@@ -118,7 +118,7 @@ tree : IDENT optvarname {
;
optcost : /* empty */ {$$ = NULL; }
| STRING
| STRING { $$ = g_strdup ($1); }
| INTEGER { $$ = g_strdup_printf ("%d", $1); }
;
......
......@@ -45,27 +45,25 @@ Rule *make_rule (char *id, Tree *tree)
void rule_add (Rule *rule, char *code, char *cost, char *cfunc)
{
if (!cfunc && !cost)
cost = g_strdup_printf ("%d", default_cost);
rule_list = g_list_append (rule_list, rule);
rule->cost = g_strdup (cost);
rule->cfunc = g_strdup (cfunc);
rule->code = g_strdup (code);
rule->cfunc = cfunc;
rule->code = code;
if (cfunc) {
if (cost)
yyerror ("duplicated costs (constant costs and cost function)");
else {
if (dag_mode)
rule->cost = g_strdup_printf ("mono_burg_cost_%d (p, data)",
g_list_length (rule_list));
else
rule->cost = g_strdup_printf ("mono_burg_cost_%d (tree, data)",
g_list_length (rule_list));
}
if (dag_mode)
rule->cost = g_strdup_printf ("mono_burg_cost_%d (p, data)",
g_list_length (rule_list));
else
rule->cost = g_strdup_printf ("mono_burg_cost_%d (tree, data)",
g_list_length (rule_list));
} else if (cost) {
rule->cost = cost;
} else {
rule->cost = g_strdup_printf ("%d", default_cost);
}
rule->lhs->rules = g_list_append (rule->lhs->rules, rule);
if (rule->tree->op)
......@@ -146,6 +144,7 @@ Tree *create_tree (char *id, char *varname, Tree *left, Tree *right)
tree->op = term;
tree->left = left;
tree->right = right;
g_free (id);
} else {
tree->nonterm = nonterm (id);
}
......@@ -155,7 +154,7 @@ Tree *create_tree (char *id, char *varname, Tree *left, Tree *right)
check_varname (varname, tree->left);
if (tree->right)
check_varname (varname, tree->right);
tree->varname = g_strdup (varname);
tree->varname = varname;
} else {
tree->varname = NULL;
}
......@@ -177,7 +176,7 @@ void create_term_prefix (char *id)
if (!predefined_terms)
yyerror ("%termprefix is only available with -p option");
prefix_list = g_list_prepend (prefix_list, g_strdup (id));
prefix_list = g_list_prepend (prefix_list, id);
}
Term *create_term (char *id, int num)
......@@ -197,7 +196,7 @@ Term *create_term (char *id, int num)
term = g_new0 (Term, 1);
term->name = g_strdup (id);
term->name = id;
term->number = num;
term->arity = -1;
......@@ -216,11 +215,14 @@ static NonTerm *nonterm (char *id)
nonterm_hash = g_hash_table_new (g_str_hash, g_str_equal);
if ((nterm = g_hash_table_lookup (nonterm_hash, id)))
{
g_free (id);
return nterm;
}
nterm = g_new0 (NonTerm, 1);
nterm->name = g_strdup (id);
nterm->name = id;
nonterm_list = g_list_append (nonterm_list, nterm);
nterm->number = g_list_length (nonterm_list);
......@@ -239,3 +241,89 @@ void start_nonterm (char *id)
start_def = TRUE;
nonterm (id);
}
void free_tree (Tree *tree)
{
if (!tree)
return;
g_free (tree->varname);
free_tree (tree->left);
free_tree (tree->right);
g_free (tree);
}
static void free_prefix ()
{
GList *prefix;
for (prefix = prefix_list; prefix; prefix = prefix->next) {
g_free (prefix->data);
}
g_list_free (prefix);
}
static void free_rules_prevent_refree (Rule *rule, GList *rule_list)
{
char *cost = rule->cost;
char *cfunc = rule->cfunc;
char *code = rule->code;
Tree *tree = rule->tree;
for (; rule_list; rule_list = rule_list->next) {
rule = (Rule *) rule_list->data;
if (cost == rule->cost)
rule->cost = NULL;
if (cfunc == rule->cfunc)
rule->cfunc = NULL;
if (code == rule->code)
rule->code = NULL;
if (tree == rule->tree)
rule->tree = NULL;
}
}
void free_rules ()
{
GList *rule;
Rule *p_rule;
for (rule = rule_list; rule; rule = rule->next ) {
p_rule = (Rule *) rule->data;
free_rules_prevent_refree (p_rule, rule->next);
g_free (p_rule->cost);
g_free (p_rule->cfunc);
g_free (p_rule->code);
free_tree (p_rule->tree);
g_free (p_rule);
}
g_list_free (rule_list);
free_prefix ();
}
void free_terms ()
{
GList *term;
Term *p_term;
for (term = term_list; term; term = term->next) {
p_term = (Term *) term->data;
g_free (p_term->name);
g_list_free (p_term->rules);
g_free (p_term);
}
g_list_free (term_list);
g_hash_table_destroy (term_hash);
}
void free_nonterms ()
{
GList *nonterm = nonterm_list;
NonTerm *p_nonterm;
for (nonterm = nonterm_list; nonterm; nonterm = nonterm->next) {
p_nonterm = (NonTerm *) nonterm->data;
g_free (p_nonterm->name);
g_list_free (p_nonterm->chain);
g_list_free (p_nonterm->rules);
g_free (p_nonterm);
}
g_list_free (nonterm_list);
g_hash_table_destroy (nonterm_hash);
}
......@@ -75,4 +75,8 @@ void start_nonterm (char *id);
Rule *make_rule (char *id, Tree *tree);
void rule_add (Rule *rule, char *code, char *cost, char *cfunc);
void free_rules ();
void free_terms ();
void free_nonterms ();
#endif /* __MONO_RULE_H__ */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment