clean up some duplicated code by using some template macros

This commit is contained in:
Bill Currie 2002-07-15 21:45:32 +00:00
parent b9a237b13d
commit 463c5724db

View file

@ -54,11 +54,16 @@ static const char rcsid[] =
#include "reloc.h" #include "reloc.h"
#include "strpool.h" #include "strpool.h"
typedef struct { #define Xgroup(X)\
qfo_def_t *defs; typedef struct {\
int num_defs; qfo_##X##_t *X##s;\
int max_defs; int num_##X##s;\
} defgroup_t; int max_##X##s;\
} X##group_t;
Xgroup(def) // defgroup_t
Xgroup(reloc) // relocgroup_t
Xgroup(func) // funcgroup_t
static hashtab_t *extern_defs; static hashtab_t *extern_defs;
static hashtab_t *defined_defs; static hashtab_t *defined_defs;
@ -68,17 +73,9 @@ static defspace_t *data;
static defspace_t *far_data; static defspace_t *far_data;
static strpool_t *strings; static strpool_t *strings;
static strpool_t *type_strings; static strpool_t *type_strings;
static struct { static relocgroup_t relocs;
qfo_reloc_t *relocs;
int num_relocs;
int max_relocs;
} relocs;
static defgroup_t global_defs, local_defs, defs; static defgroup_t global_defs, local_defs, defs;
static struct { static funcgroup_t funcs;
qfo_func_t *funcs;
int num_funcs;
int max_funcs;
} funcs;
static struct { static struct {
pr_lineno_t *lines; pr_lineno_t *lines;
int num_lines; int num_lines;
@ -91,19 +88,24 @@ static int reloc_base;
static int func_base; static int func_base;
static int line_base; static int line_base;
static void #define Xgroup_add(X)\
defgroup_add_defs (defgroup_t *defgroup, qfo_def_t *defs, int num_defs) static void \
{ X##group_add_##X##s (X##group_t *X##group, qfo_##X##_t *X##s, int num_##X##s)\
if (defgroup->num_defs + num_defs > defgroup->max_defs) { {\
defgroup->max_defs = RUP (defgroup->num_defs + num_defs, 16384); if (X##group->num_##X##s + num_##X##s > X##group->max_##X##s) {\
defgroup->defs = realloc (defgroup->defs, X##group->max_##X##s = RUP (X##group->num_##X##s + num_##X##s, 16384);\
defgroup->max_defs * sizeof (qfo_def_t)); X##group->X##s = realloc (X##group->X##s,\
} X##group->max_##X##s * sizeof (qfo_##X##_t));\
memcpy (defgroup->defs + defgroup->num_defs, defs, }\
num_defs * sizeof (qfo_def_t)); memcpy (X##group->X##s + X##group->num_##X##s, X##s,\
defgroup->num_defs += num_defs; num_##X##s * sizeof (qfo_##X##_t));\
X##group->num_##X##s += num_##X##s;\
} }
Xgroup_add(def) // defgroup_add_defs
Xgroup_add(reloc) // relocgroup_add_relocs
Xgroup_add(func) // funcgroup_add_funcs
static const char * static const char *
defs_get_key (void *_def, void *unused) defs_get_key (void *_def, void *unused)
{ {
@ -126,14 +128,7 @@ add_relocs (qfo_t *qfo)
{ {
int i; int i;
if (relocs.num_relocs + qfo->num_relocs > relocs.max_relocs) { relocgroup_add_relocs (&relocs, qfo->relocs, qfo->num_relocs);
relocs.max_relocs = RUP (relocs.num_relocs + qfo->num_relocs, 16384);
relocs.relocs = realloc (relocs.relocs,
relocs.max_relocs * sizeof (qfo_reloc_t));
}
relocs.num_relocs += qfo->num_relocs;
memcpy (relocs.relocs + reloc_base, qfo->relocs,
qfo->num_relocs * sizeof (qfo_reloc_t));
for (i = reloc_base; i < relocs.num_relocs; i++) { for (i = reloc_base; i < relocs.num_relocs; i++) {
qfo_reloc_t *reloc = relocs.relocs + i; qfo_reloc_t *reloc = relocs.relocs + i;
switch ((reloc_type)reloc->type) { switch ((reloc_type)reloc->type) {
@ -156,6 +151,8 @@ add_relocs (qfo_t *qfo)
reloc->def += func_base; reloc->def += func_base;
break; break;
case rel_def_def: case rel_def_def:
reloc->ofs += data_base;
break;
case rel_def_string: case rel_def_string:
reloc->ofs += data_base; reloc->ofs += data_base;
data->data[reloc->ofs].string_var = data->data[reloc->ofs].string_var =
@ -260,14 +257,7 @@ add_funcs (qfo_t *qfo)
{ {
int i; int i;
if (funcs.num_funcs + qfo->num_funcs > funcs.max_funcs) { funcgroup_add_funcs (&funcs, qfo->funcs, qfo->num_funcs);
funcs.max_funcs = RUP (funcs.num_funcs + qfo->num_funcs, 16384);
funcs.funcs = realloc (funcs.funcs,
funcs.max_funcs * sizeof (qfo_func_t));
}
funcs.num_funcs += qfo->num_funcs;
memcpy (funcs.funcs + func_base, qfo->funcs,
qfo->num_funcs * sizeof (qfo_func_t));
for (i = func_base; i < funcs.num_funcs; i++) { for (i = func_base; i < funcs.num_funcs; i++) {
qfo_func_t *func = funcs.funcs + i; qfo_func_t *func = funcs.funcs + i;
func->name = strpool_addstr (strings, qfo->strings + func->name); func->name = strpool_addstr (strings, qfo->strings + func->name);