mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-01-17 22:50:51 +00:00
call malloc a little less often
This commit is contained in:
parent
38d3a15413
commit
f31705380f
6 changed files with 56 additions and 45 deletions
|
@ -90,4 +90,18 @@ extern string_t s_file; // filename for function definition
|
|||
|
||||
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
|
||||
|
|
|
@ -59,7 +59,7 @@ static const char rcsid[] =
|
|||
#include "type.h"
|
||||
#include "qc-parse.h"
|
||||
|
||||
|
||||
static expr_t *free_exprs;
|
||||
int lineno_base;
|
||||
|
||||
etype_t qc_types[] = {
|
||||
|
@ -346,7 +346,9 @@ inc_users (expr_t *e)
|
|||
expr_t *
|
||||
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->file = s_file;
|
||||
|
@ -1625,9 +1627,9 @@ function_expr (expr_t *e1, expr_t *e2)
|
|||
expr_t *ret = new_expr ();
|
||||
|
||||
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;
|
||||
call->e.block.result = ret;
|
||||
}
|
||||
|
|
|
@ -214,15 +214,7 @@ PR_NewDef (type_t *type, const char *name, def_t *scope)
|
|||
{
|
||||
def_t *def;
|
||||
|
||||
if (!free_defs) {
|
||||
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));
|
||||
ALLOC (16384, def_t, defs, def);
|
||||
|
||||
if (name) {
|
||||
*pr.def_tail = def;
|
||||
|
@ -270,16 +262,7 @@ PR_FreeLocation (def_t *def)
|
|||
int size = type_size (def->type);
|
||||
locref_t *loc;
|
||||
|
||||
if (!free_free_locs) {
|
||||
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;
|
||||
ALLOC (1024, locref_t, free_locs, loc);
|
||||
loc->ofs = def->ofs;
|
||||
loc->next = free_locs[size];
|
||||
free_locs[size] = loc;
|
||||
|
|
|
@ -341,18 +341,21 @@ type_or_name (char *token)
|
|||
static hashtab_t *frame_tab;
|
||||
static hashtab_t *grab_tab;
|
||||
|
||||
typedef struct {
|
||||
typedef struct frame_s {
|
||||
struct frame_s *next;
|
||||
const char *name;
|
||||
int num;
|
||||
} frame_t;
|
||||
|
||||
static frame_t *free_frames;
|
||||
|
||||
static frame_t grab_list[] = {
|
||||
{"cd", 0},
|
||||
{"origin", 0},
|
||||
{"base", 0},
|
||||
{"flags", 0},
|
||||
{"scale", 0},
|
||||
{"skin", 0},
|
||||
{0, "cd", 0},
|
||||
{0, "origin", 0},
|
||||
{0, "base", 0},
|
||||
{0, "flags", 0},
|
||||
{0, "scale", 0},
|
||||
{0, "skin", 0},
|
||||
};
|
||||
|
||||
static const char *
|
||||
|
@ -362,10 +365,12 @@ frame_get_key (void *f, void *unused)
|
|||
}
|
||||
|
||||
static void
|
||||
frame_free (void *f, void *unused)
|
||||
frame_free (void *_f, void *unused)
|
||||
{
|
||||
free ((char*)((frame_t*)f)->name);
|
||||
free (f);
|
||||
frame_t *f = (frame_t *)_f;
|
||||
free ((char*)f->name);
|
||||
f->next = free_frames;
|
||||
free_frames = f;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -403,8 +408,8 @@ static int frame_number;
|
|||
void
|
||||
add_frame_macro (char *token)
|
||||
{
|
||||
frame_t *frame = malloc (sizeof (frame_t));
|
||||
SYS_CHECKMEM (frame);
|
||||
frame_t *frame;
|
||||
ALLOC (1024, frame_t, frames, frame);
|
||||
|
||||
frame->name = strdup (token);
|
||||
frame->num = frame_number++;
|
||||
|
|
|
@ -1307,11 +1307,14 @@ obj_string
|
|||
|
||||
%%
|
||||
|
||||
typedef struct {
|
||||
typedef struct def_state_s {
|
||||
struct def_state_s *next;
|
||||
def_t *def;
|
||||
int state;
|
||||
} def_state_t;
|
||||
|
||||
static def_state_t *free_def_states;
|
||||
|
||||
static const char *
|
||||
get_key (void *_d, void *unused)
|
||||
{
|
||||
|
@ -1321,7 +1324,9 @@ get_key (void *_d, void *unused)
|
|||
static void
|
||||
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
|
||||
|
@ -1332,8 +1337,8 @@ scan_scope (hashtab_t *tab, def_t *scope)
|
|||
scan_scope (tab, scope->scope);
|
||||
for (def = scope->scope_next; def; def = def->scope_next) {
|
||||
if (def->name && !def->removed) {
|
||||
def_state_t *ds = malloc (sizeof (def_state_t));
|
||||
SYS_CHECKMEM (ds);
|
||||
def_state_t *ds;
|
||||
ALLOC (1024, def_state_t, def_states, ds);
|
||||
ds->def = def;
|
||||
ds->state = def->initialized;
|
||||
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);
|
||||
(*ds)->def->initialized = (*ds)->state;
|
||||
|
||||
nds = malloc (sizeof (def_state_t));
|
||||
SYS_CHECKMEM (nds);
|
||||
ALLOC (1024, def_state_t, def_states, nds);
|
||||
nds->def = (*ds)->def;
|
||||
nds->state = (*ds)->state && d->state;
|
||||
Hash_Add (tab, nds);
|
||||
|
|
|
@ -54,7 +54,8 @@ static const char rcsid[] =
|
|||
#include "struct.h"
|
||||
#include "type.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct typedef_s {
|
||||
struct typedef_s *next;
|
||||
const char *name;
|
||||
type_t *type;
|
||||
} typedef_t;
|
||||
|
@ -94,6 +95,8 @@ def_t def_function = { &type_function, "temp" };
|
|||
|
||||
def_t def_ret, def_parms[MAX_PARMS];
|
||||
|
||||
static type_t *free_types;
|
||||
|
||||
static inline void
|
||||
chain_type (type_t *type)
|
||||
{
|
||||
|
@ -138,8 +141,7 @@ find_type (type_t *type)
|
|||
}
|
||||
|
||||
// allocate a new one
|
||||
check = malloc (sizeof (*check));
|
||||
SYS_CHECKMEM (check);
|
||||
ALLOC (1024, type_t, types, check);
|
||||
*check = *type;
|
||||
|
||||
chain_type (check);
|
||||
|
@ -148,6 +150,7 @@ find_type (type_t *type)
|
|||
}
|
||||
|
||||
static hashtab_t *typedef_hash;
|
||||
static typedef_t *free_typedefs;
|
||||
|
||||
static const char *
|
||||
typedef_get_key (void *t, void *unused)
|
||||
|
@ -167,7 +170,7 @@ new_typedef (const char *name, type_t *type)
|
|||
error (0, "%s redefined", name);
|
||||
return;
|
||||
}
|
||||
td = malloc (sizeof (typedef_t));
|
||||
ALLOC (1024, typedef_t, typedefs, td);
|
||||
td->name = name;
|
||||
td->type = type;
|
||||
Hash_Add (typedef_hash, td);
|
||||
|
|
Loading…
Reference in a new issue