avoid using the hunk in the progs engine (except for the override-able load and alloc functions) so tools (qfprogs and qwaq) don't need to create it.

register only the first instance of a progs provided string

This is an imperfect revision of history.
This commit is contained in:
Bill Currie 2004-11-02 04:59:00 +00:00 committed by Jeff Teunissen
parent ca46503607
commit 33bc7858f7
5 changed files with 76 additions and 40 deletions

View file

@ -346,7 +346,7 @@ struct progs_s {
struct dstring_mem_s *ds_mem;
strref_t *free_string_refs;
strref_t *static_strings;
strref_t **dynamic_strings;
strref_t **string_map;
strref_t *return_strings[PR_RS_SLOTS];
int rs_slot;
unsigned dyn_str_size;

View file

@ -146,11 +146,8 @@ PR_Load_Source_File (progs_t *pr, const char *fname)
if (!f)
return 0;
for (dir = source_paths; *dir && !f->text; dir++) {
int len;
len = strlen (*dir) + strlen (fname) + 2;
path = Hunk_TempAlloc (len);
sprintf (path, "%s%s%s", *dir, **dir ? "/" : "", fname);
f->text = pr->load_file (pr, path);
f->text = pr->load_file (pr, va ("%s%s%s", *dir, **dir ? "/" : "",
fname));
}
if (!f->text) {
pr->file_error (pr, path);
@ -192,7 +189,6 @@ PR_LoadDebug (progs_t *pr)
char *sym_path;
const char *path_end, *sym_file;
unsigned int i;
int start = Hunk_LowMark ();
ddef_t *def;
pr_type_t *str = 0;
@ -219,13 +215,13 @@ PR_LoadDebug (progs_t *pr)
pr->debugfile = PR_GetString (pr, str->string_var);
sym_file = QFS_SkipPath (pr->debugfile);
path_end = QFS_SkipPath (pr->progs_name);
sym_path = Hunk_TempAlloc (strlen (sym_file) + (path_end - pr->progs_name)
+ 1);
sym_path = malloc (strlen (sym_file) + (path_end - pr->progs_name) + 1);
strncpy (sym_path, pr->progs_name, path_end - pr->progs_name);
strcpy (sym_path + (path_end - pr->progs_name), sym_file);
pr->debug = pr->load_file (pr, sym_path);
if (!pr->debug) {
Sys_Printf ("can't load %s for debug info\n", sym_path);
free (sym_path);
return 1;
}
pr->debug->version = LittleLong (pr->debug->version);
@ -235,8 +231,8 @@ PR_LoadDebug (progs_t *pr)
(pr->debug->version >> 24) & 0xff,
(pr->debug->version >> 12) & 0xfff,
pr->debug->version & 0xfff);
Hunk_FreeToLowMark (start);
pr->debug = 0;
free (sym_path);
return 1;
}
pr->debug->crc = LittleShort (pr->debug->crc);
@ -247,10 +243,11 @@ PR_LoadDebug (progs_t *pr)
pr->progs_name,
pr->debug->crc,
pr->crc);
Hunk_FreeToLowMark (start);
pr->debug = 0;
free (sym_path);
return 1;
}
free (sym_path);
pr->debug->you_tell_me_and_we_will_both_know = LittleShort
(pr->debug->you_tell_me_and_we_will_both_know);
pr->debug->auxfunctions = LittleLong (pr->debug->auxfunctions);
@ -355,7 +352,6 @@ PR_Get_Source_File (progs_t *pr, pr_lineno_t *lineno)
const char *
PR_Get_Source_Line (progs_t *pr, unsigned int addr)
{
char *str;
const char *fname;
unsigned int line;
file_t *file;
@ -374,16 +370,11 @@ PR_Get_Source_Line (progs_t *pr, unsigned int addr)
file = PR_Load_Source_File (pr, fname);
str = Hunk_TempAlloc (strlen (fname) + 12);
sprintf (str, "%s:%d", fname, line);
if (!file || line > file->num_lines)
return str;
return va ("%s:%d", fname, line);
str = Hunk_TempAlloc (strlen (str) + file->lines[line - 1].len + 2);
sprintf (str, "%s:%d:%.*s", fname, line,
(int)file->lines[line - 1].len, file->lines[line - 1].text);
return str;
return va ("%s:%d:%.*s", fname, line, (int)file->lines[line - 1].len,
file->lines[line - 1].text);
}
ddef_t *

View file

@ -306,16 +306,11 @@ PR_ExecuteProgram (progs_t * pr, func_t fnum)
QuatAdd (OPA.quat_var, OPB.quat_var, OPC.quat_var);
break;
case OP_ADD_S:
{
const char *a = PR_GetString (pr, OPA.string_var);
const char *b = PR_GetString (pr, OPB.string_var);
int lena = strlen (a);
int size = lena + strlen (b) + 1;
char *c = Hunk_TempAlloc (size);
strcpy (c, a);
strcpy (c + lena, b);
OPC.string_var = PR_SetTempString (pr, c);
}
OPC.string_var = PR_CatStrings (pr,
PR_GetString (pr,
OPA.string_var),
PR_GetString (pr,
OPB.string_var));
break;
case OP_SUB_F:
OPC.float_var = OPA.float_var - OPB.float_var;

View file

@ -194,7 +194,7 @@ ED_NewString (progs_t *pr, const char *string)
int i, l;
l = strlen (string) + 1;
new = Hunk_TempAlloc (l);
new = alloca (l);
new_p = new;
for (i = 0; i < l; i++) {

View file

@ -107,12 +107,12 @@ new_string_ref (progs_t *pr)
pr->dyn_str_size++;
size = pr->dyn_str_size * sizeof (strref_t *);
pr->dynamic_strings = realloc (pr->dynamic_strings, size);
if (!pr->dynamic_strings)
pr->string_map = realloc (pr->string_map, size);
if (!pr->string_map)
PR_Error (pr, "out of memory");
if (!(pr->free_string_refs = calloc (1024, sizeof (strref_t))))
PR_Error (pr, "out of memory");
pr->dynamic_strings[pr->dyn_str_size - 1] = pr->free_string_refs;
pr->string_map[pr->dyn_str_size - 1] = pr->free_string_refs;
for (i = 0, sr = pr->free_string_refs; i < 1023; i++, sr++)
sr->next = sr + 1;
sr->next = 0;
@ -196,7 +196,7 @@ PR_LoadStrings (progs_t *pr)
} else {
pr->strref_hash = Hash_NewTable (1021, strref_get_key, strref_free,
pr);
pr->dynamic_strings = 0;
pr->string_map = 0;
pr->free_string_refs = 0;
pr->dyn_str_size = 0;
}
@ -207,11 +207,16 @@ PR_LoadStrings (progs_t *pr)
count = 0;
str = pr->pr_strings;
while (str < end) {
pr->static_strings[count].string = str;
if (!Hash_Find (pr->strref_hash, str)) {
pr->static_strings[count].type = str_static;
pr->static_strings[count].s.string = str;
Hash_Add (pr->strref_hash, &pr->static_strings[count]);
count++;
}
str += strlen (str) + 1;
Hash_Add (pr->strref_hash, &pr->static_strings[count]);
count++;
}
pr->static_strings = realloc (pr->static_strings,
count * sizeof (strref_t));
pr->num_strings = count;
return 1;
}
@ -226,7 +231,7 @@ get_strref (progs_t *pr, int num)
if (row >= pr->dyn_str_size)
return 0;
return &pr->dynamic_strings[row][num];
return &pr->string_map[row][num];
} else {
return 0;
}
@ -278,6 +283,18 @@ PR_GetDString (progs_t *pr, int num)
PR_RunError (pr, "Invalid string offset: %d", num);
}
static inline char *
pr_stralloc (progs_t *pr, int len)
{
return PR_Zone_Malloc (pr, len + 1);
}
static inline void
pr_strfree (progs_t *pr, char *s)
{
PR_Zone_Free (pr, s);
}
static inline char *
pr_strdup (progs_t *pr, const char *s)
{
@ -290,7 +307,11 @@ pr_strdup (progs_t *pr, const char *s)
int
PR_SetString (progs_t *pr, const char *s)
{
strref_t *sr = Hash_Find (pr->strref_hash, s);
strref_t *sr;
if (!s)
s = "";
sr = Hash_Find (pr->strref_hash, s);
if (!sr) {
sr = new_string_ref (pr);
@ -338,6 +359,35 @@ PR_SetReturnString (progs_t *pr, const char *s)
return string_index (pr, sr);
}
static inline int
pr_settempstring (progs_t *pr, char *s)
{
strref_t *sr;
sr = new_string_ref (pr);
sr->type = str_temp;
sr->s.string = s;
sr->next = pr->pr_xtstr;
pr->pr_xtstr = sr;
return string_index (pr, sr);
}
int
PR_CatStrings (progs_t *pr, const char *a, const char *b)
{
int lena;
char *c;
lena = strlen (a);
c = pr_stralloc (pr, lena + strlen (b));
strcpy (c, a);
strcpy (c + lena, b);
return pr_settempstring (pr, c);
}
int
PR_SetTempString (progs_t *pr, const char *s)
{