Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moved filename caching into the preprocessor.
Now the assembler, compiler and preprocessor can all share it.
  • Loading branch information
icculus committed Feb 13, 2009
1 parent 7575a37 commit 9bf4217
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 62 deletions.
60 changes: 1 addition & 59 deletions mojoshader_assembler.c
Expand Up @@ -18,15 +18,6 @@
#endif


// Simple linked list to cache source filenames, so we don't have to copy
// the same string over and over for each opcode.
typedef struct FilenameCache
{
char *filename;
struct FilenameCache *next;
} FilenameCache;


typedef struct SourcePos
{
const char *filename;
Expand Down Expand Up @@ -64,7 +55,6 @@ typedef struct Context
uint32 ctab_allocation;
size_t output_len;
size_t output_allocation;
FilenameCache *filename_cache;
} Context;


Expand Down Expand Up @@ -266,59 +256,12 @@ static Token nexttoken(Context *ctx)
} // nexttoken


static const char *cache_filename(Context *ctx, const char *fname)
{
if (fname == NULL)
return NULL;

// !!! FIXME: this could be optimized into a hash table, but oh well.
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
if (strcmp(item->filename, fname) == 0)
return item->filename;
item = item->next;
} // while

// new cache item.
item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
if (item == NULL)
return NULL;

item->filename = (char *) Malloc(ctx, strlen(fname) + 1);
if (item->filename == NULL)
{
Free(ctx, item);
return NULL;
} // if

strcpy(item->filename, fname);
item->next = ctx->filename_cache;
ctx->filename_cache = item;

return item->filename;
} // cache_filename


static void free_filename_cache(Context *ctx)
{
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
FilenameCache *next = item->next;
Free(ctx, item->filename);
Free(ctx, item);
item = next;
} // while
} // free_filename_cache


static inline void add_token_sourcepos(Context *ctx, const size_t idx)
{
unsigned int pos = 0;
const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
ctx->token_to_source[idx].line = pos;
ctx->token_to_source[idx].filename = cache_filename(ctx, fname);
ctx->token_to_source[idx].filename = fname; // cached in preprocessor!
} // add_token_sourcepos


Expand Down Expand Up @@ -1516,7 +1459,6 @@ static void destroy_context(Context *ctx)
MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
void *d = ctx->malloc_data;
free_error_list(f, d, ctx->errors);
free_filename_cache(ctx);
if (ctx->preprocessor != NULL)
preprocessor_end(ctx->preprocessor);
if (ctx->output != NULL)
Expand Down
2 changes: 1 addition & 1 deletion mojoshader_internal.h
Expand Up @@ -377,7 +377,7 @@ typedef struct Preprocessor Preprocessor;

typedef struct IncludeState
{
char *filename;
const char *filename;
int included;
const char *source_base;
const char *source;
Expand Down
63 changes: 61 additions & 2 deletions mojoshader_preprocessor.c
Expand Up @@ -23,13 +23,22 @@ typedef struct DefineHash
struct DefineHash *next;
} DefineHash;

// Simple linked list to cache source filenames, so we don't have to copy
// the same string over and over for each opcode.
typedef struct FilenameCache
{
char *filename;
struct FilenameCache *next;
} FilenameCache;

typedef struct Context
{
int isfail;
int out_of_memory;
char failstr[256];
IncludeState *include_stack;
DefineHash *define_hashtable[256];
FilenameCache *filename_cache;
MOJOSHADER_includeOpen open_callback;
MOJOSHADER_includeClose close_callback;
MOJOSHADER_malloc malloc;
Expand Down Expand Up @@ -325,6 +334,54 @@ static void free_all_defines(Context *ctx)
} // find_define


// filename cache stuff...

static const char *cache_filename(Context *ctx, const char *fname)
{
if (fname == NULL)
return NULL;

// !!! FIXME: this could be optimized into a hash table, but oh well.
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
if (strcmp(item->filename, fname) == 0)
return item->filename;
item = item->next;
} // while

// new cache item.
item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
if (item == NULL)
return NULL;

item->filename = StrDup(ctx, fname);
if (item->filename == NULL)
{
Free(ctx, item);
return NULL;
} // if

item->next = ctx->filename_cache;
ctx->filename_cache = item;

return item->filename;
} // cache_filename


static void free_filename_cache(Context *ctx)
{
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
FilenameCache *next = item->next;
Free(ctx, item->filename);
Free(ctx, item);
item = next;
} // while
} // free_filename_cache


static int push_source(Context *ctx, const char *fname, const char *source,
unsigned int srclen, int included)
{
Expand All @@ -335,7 +392,7 @@ static int push_source(Context *ctx, const char *fname, const char *source,

if (fname != NULL)
{
state->filename = StrDup(ctx, fname);
state->filename = cache_filename(ctx, fname);
if (state->filename == NULL)
{
Free(ctx, state);
Expand Down Expand Up @@ -369,8 +426,9 @@ static void pop_source(Context *ctx)
ctx->free, ctx->malloc_data);
} // if

// state->filename is a pointer to the filename cache; don't free it here!

ctx->include_stack = state->next;
Free(ctx, state->filename);
Free(ctx, state);
} // pop_source

Expand Down Expand Up @@ -435,6 +493,7 @@ void preprocessor_end(Preprocessor *_ctx)
pop_source(ctx);

free_all_defines(ctx);
free_filename_cache(ctx);

Free(ctx, ctx);
} // preprocessor_end
Expand Down

0 comments on commit 9bf4217

Please sign in to comment.