call malloc a little less often

This commit is contained in:
Bill Currie 2002-06-05 21:13:29 +00:00
parent 38d3a15413
commit f31705380f
6 changed files with 56 additions and 45 deletions

View file

@ -90,4 +90,18 @@ extern string_t s_file; // filename for function definition
const char *strip_path (const char *filename); const char *strip_path (const char *filename);
#define ALLOC(s, t, n, v) \
do { \
if (!free_##n) { \
int i; \
free_##n = malloc ((s) * sizeof (t)); \
for (i = 0; i < (s) - 1; i++) \
free_##n[i].next = &free_##n[i + 1];\
free_##n[i].next = 0; \
} \
v = free_##n; \
free_##n = free_##n->next; \
memset (v, 0, sizeof (*v)); \
} while (0)
#endif//__qfcc_h #endif//__qfcc_h

View file

@ -59,7 +59,7 @@ static const char rcsid[] =
#include "type.h" #include "type.h"
#include "qc-parse.h" #include "qc-parse.h"
static expr_t *free_exprs;
int lineno_base; int lineno_base;
etype_t qc_types[] = { etype_t qc_types[] = {
@ -346,7 +346,9 @@ inc_users (expr_t *e)
expr_t * expr_t *
new_expr (void) new_expr (void)
{ {
expr_t *e = calloc (1, sizeof (expr_t)); expr_t *e;
ALLOC (16384, expr_t, exprs, e);
e->line = pr_source_line; e->line = pr_source_line;
e->file = s_file; e->file = s_file;
@ -1625,9 +1627,9 @@ function_expr (expr_t *e1, expr_t *e2)
expr_t *ret = new_expr (); expr_t *ret = new_expr ();
ret->type = ex_def; ret->type = ex_def;
ret->e.def = memcpy (malloc (sizeof (def_t)), &def_ret, sizeof (def_t)); ret->e.def = PR_NewDef (0, 0, 0);
*ret->e.def = def_ret;
SYS_CHECKMEM (ret->e.def);
ret->e.def->type = ftype->aux_type; ret->e.def->type = ftype->aux_type;
call->e.block.result = ret; call->e.block.result = ret;
} }

View file

@ -214,15 +214,7 @@ PR_NewDef (type_t *type, const char *name, def_t *scope)
{ {
def_t *def; def_t *def;
if (!free_defs) { ALLOC (16384, def_t, defs, def);
int i;
free_defs = malloc (16384 * sizeof (def_t));
for (i = 0; i < 16383; i++)
free_defs[i].next = &free_defs[i + 1];
}
def = free_defs;
free_defs = free_defs->next;
memset (def, 0, sizeof (*def));
if (name) { if (name) {
*pr.def_tail = def; *pr.def_tail = def;
@ -270,16 +262,7 @@ PR_FreeLocation (def_t *def)
int size = type_size (def->type); int size = type_size (def->type);
locref_t *loc; locref_t *loc;
if (!free_free_locs) { ALLOC (1024, locref_t, free_locs, loc);
free_free_locs = malloc (256 * sizeof (locref_t));
SYS_CHECKMEM (free_free_locs);
for (loc = free_free_locs; loc - free_free_locs < 255; loc++)
loc->next = loc + 1;
loc->next = 0;
}
loc = free_free_locs;
free_free_locs = loc->next;
loc->ofs = def->ofs; loc->ofs = def->ofs;
loc->next = free_locs[size]; loc->next = free_locs[size];
free_locs[size] = loc; free_locs[size] = loc;

View file

@ -341,18 +341,21 @@ type_or_name (char *token)
static hashtab_t *frame_tab; static hashtab_t *frame_tab;
static hashtab_t *grab_tab; static hashtab_t *grab_tab;
typedef struct { typedef struct frame_s {
struct frame_s *next;
const char *name; const char *name;
int num; int num;
} frame_t; } frame_t;
static frame_t *free_frames;
static frame_t grab_list[] = { static frame_t grab_list[] = {
{"cd", 0}, {0, "cd", 0},
{"origin", 0}, {0, "origin", 0},
{"base", 0}, {0, "base", 0},
{"flags", 0}, {0, "flags", 0},
{"scale", 0}, {0, "scale", 0},
{"skin", 0}, {0, "skin", 0},
}; };
static const char * static const char *
@ -362,10 +365,12 @@ frame_get_key (void *f, void *unused)
} }
static void static void
frame_free (void *f, void *unused) frame_free (void *_f, void *unused)
{ {
free ((char*)((frame_t*)f)->name); frame_t *f = (frame_t *)_f;
free (f); free ((char*)f->name);
f->next = free_frames;
free_frames = f;
} }
int int
@ -403,8 +408,8 @@ static int frame_number;
void void
add_frame_macro (char *token) add_frame_macro (char *token)
{ {
frame_t *frame = malloc (sizeof (frame_t)); frame_t *frame;
SYS_CHECKMEM (frame); ALLOC (1024, frame_t, frames, frame);
frame->name = strdup (token); frame->name = strdup (token);
frame->num = frame_number++; frame->num = frame_number++;

View file

@ -1307,11 +1307,14 @@ obj_string
%% %%
typedef struct { typedef struct def_state_s {
struct def_state_s *next;
def_t *def; def_t *def;
int state; int state;
} def_state_t; } def_state_t;
static def_state_t *free_def_states;
static const char * static const char *
get_key (void *_d, void *unused) get_key (void *_d, void *unused)
{ {
@ -1321,7 +1324,9 @@ get_key (void *_d, void *unused)
static void static void
free_key (void *_d, void *unused) free_key (void *_d, void *unused)
{ {
free (_d); def_state_t *d = (def_state_t *)_d;
d->next = free_def_states;
free_def_states = d;
} }
static void static void
@ -1332,8 +1337,8 @@ scan_scope (hashtab_t *tab, def_t *scope)
scan_scope (tab, scope->scope); scan_scope (tab, scope->scope);
for (def = scope->scope_next; def; def = def->scope_next) { for (def = scope->scope_next; def; def = def->scope_next) {
if (def->name && !def->removed) { if (def->name && !def->removed) {
def_state_t *ds = malloc (sizeof (def_state_t)); def_state_t *ds;
SYS_CHECKMEM (ds); ALLOC (1024, def_state_t, def_states, ds);
ds->def = def; ds->def = def;
ds->state = def->initialized; ds->state = def->initialized;
Hash_Add (tab, ds); Hash_Add (tab, ds);
@ -1362,8 +1367,7 @@ merge_local_inits (hashtab_t *dl_1, hashtab_t *dl_2)
d = Hash_Find (dl_2, (*ds)->def->name); d = Hash_Find (dl_2, (*ds)->def->name);
(*ds)->def->initialized = (*ds)->state; (*ds)->def->initialized = (*ds)->state;
nds = malloc (sizeof (def_state_t)); ALLOC (1024, def_state_t, def_states, nds);
SYS_CHECKMEM (nds);
nds->def = (*ds)->def; nds->def = (*ds)->def;
nds->state = (*ds)->state && d->state; nds->state = (*ds)->state && d->state;
Hash_Add (tab, nds); Hash_Add (tab, nds);

View file

@ -54,7 +54,8 @@ static const char rcsid[] =
#include "struct.h" #include "struct.h"
#include "type.h" #include "type.h"
typedef struct { typedef struct typedef_s {
struct typedef_s *next;
const char *name; const char *name;
type_t *type; type_t *type;
} typedef_t; } typedef_t;
@ -94,6 +95,8 @@ def_t def_function = { &type_function, "temp" };
def_t def_ret, def_parms[MAX_PARMS]; def_t def_ret, def_parms[MAX_PARMS];
static type_t *free_types;
static inline void static inline void
chain_type (type_t *type) chain_type (type_t *type)
{ {
@ -138,8 +141,7 @@ find_type (type_t *type)
} }
// allocate a new one // allocate a new one
check = malloc (sizeof (*check)); ALLOC (1024, type_t, types, check);
SYS_CHECKMEM (check);
*check = *type; *check = *type;
chain_type (check); chain_type (check);
@ -148,6 +150,7 @@ find_type (type_t *type)
} }
static hashtab_t *typedef_hash; static hashtab_t *typedef_hash;
static typedef_t *free_typedefs;
static const char * static const char *
typedef_get_key (void *t, void *unused) typedef_get_key (void *t, void *unused)
@ -167,7 +170,7 @@ new_typedef (const char *name, type_t *type)
error (0, "%s redefined", name); error (0, "%s redefined", name);
return; return;
} }
td = malloc (sizeof (typedef_t)); ALLOC (1024, typedef_t, typedefs, td);
td->name = name; td->name = name;
td->type = type; td->type = type;
Hash_Add (typedef_hash, td); Hash_Add (typedef_hash, td);