summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Johnson <peter@tortall.net>2001-10-29 01:10:20 +0000
committerPeter Johnson <peter@tortall.net>2001-10-29 01:10:20 +0000
commitdc1c3c70f5271ae7679ba37ae49f5857ade67443 (patch)
treeda8e2a26019845b8b09cfdcd0173bc4e54a003aa
parent7a6eaf8c0338a789e3958d49303ed465de8874c3 (diff)
downloadyasm-dc1c3c70f5271ae7679ba37ae49f5857ade67443.tar.gz
Get rid of all the extra filename strdup()'s. (Bug #14).
svn path=/trunk/yasm/; revision=300
-rw-r--r--frontends/yasm/yasm.c8
-rw-r--r--libyasm/bytecode.c4
-rw-r--r--libyasm/errwarn.c6
-rw-r--r--libyasm/expr.c13
-rw-r--r--libyasm/linemgr.c27
-rw-r--r--libyasm/linemgr.h5
-rw-r--r--libyasm/symrec.c4
-rw-r--r--modules/arch/x86/expr.c13
-rw-r--r--modules/arch/x86/x86expr.c13
-rw-r--r--src/arch/x86/expr.c13
-rw-r--r--src/arch/x86/x86expr.c13
-rw-r--r--src/bytecode.c4
-rw-r--r--src/errwarn.c6
-rw-r--r--src/expr.c13
-rw-r--r--src/globals.c27
-rw-r--r--src/globals.h5
-rw-r--r--src/linemgr.c27
-rw-r--r--src/linemgr.h5
-rw-r--r--src/main.c8
-rw-r--r--src/symrec.c4
20 files changed, 140 insertions, 78 deletions
diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c
index 6a93e7bc..b42d331b 100644
--- a/frontends/yasm/yasm.c
+++ b/frontends/yasm/yasm.c
@@ -124,7 +124,7 @@ main(int argc, char *argv[])
if (!files_open)
{
in = stdin;
- in_filename = xstrdup("<STDIN>");
+ switch_filename("<STDIN>");
}
/* Get initial BITS setting from object format */
@@ -145,8 +145,7 @@ main(int argc, char *argv[])
printf("Post-parser-finalization:\n");
sections_print(sections);
- if (in_filename)
- free(in_filename);
+ filename_delete_all();
return EXIT_SUCCESS;
}
@@ -158,7 +157,6 @@ not_an_option_handler(char *param)
{
if (files_open > 0) {
WarningNow("can open only one input file, only latest file will be processed");
- free(in_filename);
fclose(in);
}
@@ -167,7 +165,7 @@ not_an_option_handler(char *param)
ErrorNow(_("could not open file `%s'"), param);
return 1;
}
- in_filename = xstrdup(param);
+ switch_filename(param);
files_open++;
return 0;
diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c
index 8f4d91ed..498236ba 100644
--- a/libyasm/bytecode.c
+++ b/libyasm/bytecode.c
@@ -175,7 +175,7 @@ struct bytecode {
multiple copies), 0 if unknown */
/* where it came from */
- char *filename;
+ const char *filename;
unsigned int lineno;
/* other assembler state info */
@@ -426,7 +426,7 @@ bytecode_new_common(void)
bc->multiple = (expr *)NULL;
bc->len = 0;
- bc->filename = xstrdup(in_filename);
+ bc->filename = in_filename;
bc->lineno = line_number;
bc->offset = 0;
diff --git a/libyasm/errwarn.c b/libyasm/errwarn.c
index 74061c69..4f008386 100644
--- a/libyasm/errwarn.c
+++ b/libyasm/errwarn.c
@@ -76,7 +76,7 @@ typedef struct errwarn_s {
enum { WE_ERROR, WE_WARNING } type;
- char *filename;
+ const char *filename;
unsigned long line;
/* FIXME: This should not be a fixed size. But we don't have vasprintf()
* right now. */
@@ -177,7 +177,7 @@ Error(const char *fmt, ...)
we = xmalloc(sizeof(errwarn));
we->type = WE_ERROR;
- we->filename = xstrdup(in_filename);
+ we->filename = in_filename;
we->line = line_number;
}
@@ -211,7 +211,7 @@ Warning(const char *fmt, ...)
we = xmalloc(sizeof(errwarn));
we->type = WE_WARNING;
- we->filename = xstrdup(in_filename);
+ we->filename = in_filename;
we->line = line_number;
va_start(ap, fmt);
vsprintf(we->msg, fmt, ap);
diff --git a/libyasm/expr.c b/libyasm/expr.c
index a3c11d85..33180b3c 100644
--- a/libyasm/expr.c
+++ b/libyasm/expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/libyasm/linemgr.c b/libyasm/linemgr.c
index 4af39630..8f516a8e 100644
--- a/libyasm/linemgr.c
+++ b/libyasm/linemgr.c
@@ -27,9 +27,34 @@
#include <stdio.h>
+#include "globals.h"
+
+#ifdef DMALLOC
+# include <dmalloc.h>
+#endif
+
RCSID("$IdPath$");
-char *in_filename = (char *)NULL;
+const char *in_filename = (const char *)NULL;
unsigned int line_number = 1;
unsigned char mode_bits = 0;
unsigned int asm_options = 0;
+
+static ternary_tree filename_table = (ternary_tree)NULL;
+
+void
+switch_filename(const char *filename)
+{
+ char *copy = xstrdup(filename);
+ in_filename = ternary_insert(&filename_table, filename, copy, 0);
+ if (in_filename != copy)
+ xfree(copy);
+}
+
+void
+filename_delete_all(void)
+{
+ in_filename = NULL;
+ ternary_cleanup(filename_table);
+ filename_table = NULL;
+}
diff --git a/libyasm/linemgr.h b/libyasm/linemgr.h
index 373d1dd8..23376e13 100644
--- a/libyasm/linemgr.h
+++ b/libyasm/linemgr.h
@@ -22,9 +22,12 @@
#ifndef YASM_GLOBALS_H
#define YASM_GLOBALS_H
-extern char *in_filename;
+extern const char *in_filename;
extern unsigned int line_number;
extern unsigned char mode_bits;
extern unsigned int asm_options;
+void switch_filename(const char *filename);
+void filename_delete_all(void);
+
#endif
diff --git a/libyasm/symrec.c b/libyasm/symrec.c
index 03e641ae..1426fc09 100644
--- a/libyasm/symrec.c
+++ b/libyasm/symrec.c
@@ -69,7 +69,7 @@ struct symrec {
SymType type;
SymStatus status;
SymVisibility visibility;
- char *filename; /* file and line */
+ const char *filename; /* file and line */
unsigned long line; /* symbol was first declared or used on */
union {
expr *expn; /* equ value */
@@ -107,7 +107,7 @@ symrec_get_or_new(const char *name, int in_table)
rec->name = xstrdup(name);
rec->type = SYM_UNKNOWN;
- rec->filename = xstrdup(in_filename);
+ rec->filename = in_filename;
rec->line = line_number;
rec->visibility = SYM_LOCAL;
diff --git a/modules/arch/x86/expr.c b/modules/arch/x86/expr.c
index a3c11d85..33180b3c 100644
--- a/modules/arch/x86/expr.c
+++ b/modules/arch/x86/expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c
index a3c11d85..33180b3c 100644
--- a/modules/arch/x86/x86expr.c
+++ b/modules/arch/x86/x86expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/src/arch/x86/expr.c b/src/arch/x86/expr.c
index a3c11d85..33180b3c 100644
--- a/src/arch/x86/expr.c
+++ b/src/arch/x86/expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/src/arch/x86/x86expr.c b/src/arch/x86/x86expr.c
index a3c11d85..33180b3c 100644
--- a/src/arch/x86/x86expr.c
+++ b/src/arch/x86/x86expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/src/bytecode.c b/src/bytecode.c
index 8f4d91ed..498236ba 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -175,7 +175,7 @@ struct bytecode {
multiple copies), 0 if unknown */
/* where it came from */
- char *filename;
+ const char *filename;
unsigned int lineno;
/* other assembler state info */
@@ -426,7 +426,7 @@ bytecode_new_common(void)
bc->multiple = (expr *)NULL;
bc->len = 0;
- bc->filename = xstrdup(in_filename);
+ bc->filename = in_filename;
bc->lineno = line_number;
bc->offset = 0;
diff --git a/src/errwarn.c b/src/errwarn.c
index 74061c69..4f008386 100644
--- a/src/errwarn.c
+++ b/src/errwarn.c
@@ -76,7 +76,7 @@ typedef struct errwarn_s {
enum { WE_ERROR, WE_WARNING } type;
- char *filename;
+ const char *filename;
unsigned long line;
/* FIXME: This should not be a fixed size. But we don't have vasprintf()
* right now. */
@@ -177,7 +177,7 @@ Error(const char *fmt, ...)
we = xmalloc(sizeof(errwarn));
we->type = WE_ERROR;
- we->filename = xstrdup(in_filename);
+ we->filename = in_filename;
we->line = line_number;
}
@@ -211,7 +211,7 @@ Warning(const char *fmt, ...)
we = xmalloc(sizeof(errwarn));
we->type = WE_WARNING;
- we->filename = xstrdup(in_filename);
+ we->filename = in_filename;
we->line = line_number;
va_start(ap, fmt);
vsprintf(we->msg, fmt, ap);
diff --git a/src/expr.c b/src/expr.c
index a3c11d85..33180b3c 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -84,7 +84,7 @@ struct ExprItem {
*/
struct expr {
ExprOp op;
- char *filename;
+ const char *filename;
unsigned long line;
int numterms;
ExprItem terms[2]; /* structure may be extended to include more */
@@ -121,7 +121,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right)
ptr->numterms++;
}
- ptr->filename = xstrdup(in_filename);
+ ptr->filename = in_filename;
ptr->line = line_number;
return ptr;
@@ -182,7 +182,7 @@ expr_xform_neg_item(expr *e, ExprItem *ei)
/* Build -1*ei subexpression */
sube->op = EXPR_MUL;
- sube->filename = xstrdup(e->filename);
+ sube->filename = e->filename;
sube->line = e->line;
sube->numterms = 2;
sube->terms[0].type = EXPR_INT;
@@ -243,7 +243,7 @@ expr_xform_neg_helper(expr *e)
*/
ne = xmalloc(sizeof(expr));
ne->op = EXPR_MUL;
- ne->filename = xstrdup(e->filename);
+ ne->filename = e->filename;
ne->line = e->line;
ne->numterms = 2;
ne->terms[0].type = EXPR_INT;
@@ -442,7 +442,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
e->terms[i].data.expn->op == EXPR_IDENT) {
expr *sube = e->terms[i].data.expn;
e->terms[i] = sube->terms[0];
- xfree(sube->filename);
xfree(sube);
}
@@ -558,7 +557,6 @@ expr_level_op(expr *e, int fold_const, int simplify_ident)
/* delete subexpression, but *don't delete nodes* (as we've just
* copied them!)
*/
- xfree(sube->filename);
xfree(sube);
} else if (o != i) {
/* copy operand if it changed places */
@@ -656,7 +654,7 @@ expr_copy_except(const expr *e, int except)
n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));
n->op = e->op;
- n->filename = xstrdup(e->filename);
+ n->filename = e->filename;
n->line = e->line;
n->numterms = e->numterms;
for (i=0; i<e->numterms; i++) {
@@ -713,7 +711,6 @@ expr_delete_each(expr *e, void *d)
break; /* none of the other types needs to be deleted */
}
}
- xfree(e->filename);
xfree(e); /* free ourselves */
return 0; /* don't stop recursion */
}
diff --git a/src/globals.c b/src/globals.c
index 4af39630..8f516a8e 100644
--- a/src/globals.c
+++ b/src/globals.c
@@ -27,9 +27,34 @@
#include <stdio.h>
+#include "globals.h"
+
+#ifdef DMALLOC
+# include <dmalloc.h>
+#endif
+
RCSID("$IdPath$");
-char *in_filename = (char *)NULL;
+const char *in_filename = (const char *)NULL;
unsigned int line_number = 1;
unsigned char mode_bits = 0;
unsigned int asm_options = 0;
+
+static ternary_tree filename_table = (ternary_tree)NULL;
+
+void
+switch_filename(const char *filename)
+{
+ char *copy = xstrdup(filename);
+ in_filename = ternary_insert(&filename_table, filename, copy, 0);
+ if (in_filename != copy)
+ xfree(copy);
+}
+
+void
+filename_delete_all(void)
+{
+ in_filename = NULL;
+ ternary_cleanup(filename_table);
+ filename_table = NULL;
+}
diff --git a/src/globals.h b/src/globals.h
index 373d1dd8..23376e13 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -22,9 +22,12 @@
#ifndef YASM_GLOBALS_H
#define YASM_GLOBALS_H
-extern char *in_filename;
+extern const char *in_filename;
extern unsigned int line_number;
extern unsigned char mode_bits;
extern unsigned int asm_options;
+void switch_filename(const char *filename);
+void filename_delete_all(void);
+
#endif
diff --git a/src/linemgr.c b/src/linemgr.c
index 4af39630..8f516a8e 100644
--- a/src/linemgr.c
+++ b/src/linemgr.c
@@ -27,9 +27,34 @@
#include <stdio.h>
+#include "globals.h"
+
+#ifdef DMALLOC
+# include <dmalloc.h>
+#endif
+
RCSID("$IdPath$");
-char *in_filename = (char *)NULL;
+const char *in_filename = (const char *)NULL;
unsigned int line_number = 1;
unsigned char mode_bits = 0;
unsigned int asm_options = 0;
+
+static ternary_tree filename_table = (ternary_tree)NULL;
+
+void
+switch_filename(const char *filename)
+{
+ char *copy = xstrdup(filename);
+ in_filename = ternary_insert(&filename_table, filename, copy, 0);
+ if (in_filename != copy)
+ xfree(copy);
+}
+
+void
+filename_delete_all(void)
+{
+ in_filename = NULL;
+ ternary_cleanup(filename_table);
+ filename_table = NULL;
+}
diff --git a/src/linemgr.h b/src/linemgr.h
index 373d1dd8..23376e13 100644
--- a/src/linemgr.h
+++ b/src/linemgr.h
@@ -22,9 +22,12 @@
#ifndef YASM_GLOBALS_H
#define YASM_GLOBALS_H
-extern char *in_filename;
+extern const char *in_filename;
extern unsigned int line_number;
extern unsigned char mode_bits;
extern unsigned int asm_options;
+void switch_filename(const char *filename);
+void filename_delete_all(void);
+
#endif
diff --git a/src/main.c b/src/main.c
index 6a93e7bc..b42d331b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -124,7 +124,7 @@ main(int argc, char *argv[])
if (!files_open)
{
in = stdin;
- in_filename = xstrdup("<STDIN>");
+ switch_filename("<STDIN>");
}
/* Get initial BITS setting from object format */
@@ -145,8 +145,7 @@ main(int argc, char *argv[])
printf("Post-parser-finalization:\n");
sections_print(sections);
- if (in_filename)
- free(in_filename);
+ filename_delete_all();
return EXIT_SUCCESS;
}
@@ -158,7 +157,6 @@ not_an_option_handler(char *param)
{
if (files_open > 0) {
WarningNow("can open only one input file, only latest file will be processed");
- free(in_filename);
fclose(in);
}
@@ -167,7 +165,7 @@ not_an_option_handler(char *param)
ErrorNow(_("could not open file `%s'"), param);
return 1;
}
- in_filename = xstrdup(param);
+ switch_filename(param);
files_open++;
return 0;
diff --git a/src/symrec.c b/src/symrec.c
index 03e641ae..1426fc09 100644
--- a/src/symrec.c
+++ b/src/symrec.c
@@ -69,7 +69,7 @@ struct symrec {
SymType type;
SymStatus status;
SymVisibility visibility;
- char *filename; /* file and line */
+ const char *filename; /* file and line */
unsigned long line; /* symbol was first declared or used on */
union {
expr *expn; /* equ value */
@@ -107,7 +107,7 @@ symrec_get_or_new(const char *name, int in_table)
rec->name = xstrdup(name);
rec->type = SYM_UNKNOWN;
- rec->filename = xstrdup(in_filename);
+ rec->filename = in_filename;
rec->line = line_number;
rec->visibility = SYM_LOCAL;