From 7b5142a2fd0939e5209e1d375843b8e9d94eb114 Mon Sep 17 00:00:00 2001 From: "Gary V. Vaughan" Date: Fri, 20 Sep 2013 21:20:05 +0700 Subject: modules: simplify module lookup by name. * m4/m4private.h (m4:namemap): New field for hash table to lookup module structures by name string. * m4/m4.c (hashfn): Hash function for plain strings. (m4_create): Initialise namemap field to a hash table using hashfn for inserting and looking up keys. * m4/module.c (m4__module_find): Replace the fussy libltdl twiddling with a hash lookup in m4:namemap. Add a new context parameter. Adjust all callers. Signed-off-by: Gary V. Vaughan --- m4/m4.c | 15 +++++++++++++++ m4/m4private.h | 3 ++- m4/module.c | 18 +++++------------- src/freeze.c | 4 ++-- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/m4/m4.c b/m4/m4.c index 04977995..d9166655 100644 --- a/m4/m4.c +++ b/m4/m4.c @@ -20,9 +20,21 @@ #include +#include "bitrotate.h" #include "m4private.h" #define DEFAULT_NESTING_LIMIT 1024 +#define DEFAULT_NAMEMAP_SIZE 61 + +static size_t +hashfn (const void *ptr) +{ + const char *s = (const char *) ptr; + size_t val = DEFAULT_NAMEMAP_SIZE; + while (*s) + val = rotl_sz (val, 7) + to_uchar (*s++); + return val; +} m4 * @@ -33,6 +45,9 @@ m4_create (void) context->symtab = m4_symtab_create (0); context->syntax = m4_syntax_create (); + context->namemap = + m4_hash_new (DEFAULT_NAMEMAP_SIZE, hashfn, (m4_hash_cmp_func *) strcmp); + context->debug_file = stderr; obstack_init (&context->trace_messages); diff --git a/m4/m4private.h b/m4/m4private.h index dbda70ca..926df58f 100644 --- a/m4/m4private.h +++ b/m4/m4private.h @@ -60,6 +60,7 @@ struct m4 { m4_symbol_table * symtab; m4_syntax_table * syntax; m4_module * modules; + m4_hash * namemap; const char * current_file; /* Current input file. */ int current_line; /* Current input line. */ @@ -190,7 +191,7 @@ struct m4_module extern void m4__module_init (m4 *context); extern m4_module * m4__module_open (m4 *context, const char *name, m4_obstack *obs); -extern m4_module * m4__module_find (const char *name); +extern m4_module * m4__module_find (m4 *context, const char *name); /* --- SYMBOL TABLE MANAGEMENT --- */ diff --git a/m4/module.c b/m4/module.c index dce0f5bf..05b7bdef 100644 --- a/m4/module.c +++ b/m4/module.c @@ -87,7 +87,7 @@ void * m4_module_import (m4 *context, const char *module_name, const char *symbol_name, m4_obstack *obs) { - m4_module * module = m4__module_find (module_name); + m4_module * module = m4__module_find (context, module_name); void * symbol_address = NULL; /* Try to load the module if it is not yet available (errors are @@ -256,19 +256,10 @@ m4_module_next (m4 *context, m4_module *module) /* Return the first loaded module that passes the registered interface test and is called NAME. */ m4_module * -m4__module_find (const char *name) +m4__module_find (m4 *context, const char *name) { - lt_dlhandle handle; - m4_module *module; - assert (iface_id); - - handle = lt_dlhandle_fetch (iface_id, name); - if (!handle) - return NULL; - module = (m4_module *) lt_dlcaller_get_data (iface_id, handle); - if (module) - assert (module->handle == handle); - return module; + m4_module **pmodule = (m4_module **) m4_hash_lookup (context->namemap, name); + return pmodule ? *pmodule : NULL; } @@ -402,6 +393,7 @@ m4__module_open (m4 *context, const char *name, m4_obstack *obs) module->next = context->modules; context->modules = module; + m4_hash_insert (context->namemap, xstrdup (name), module); /* clear out any stale errors, since we have to use lt_dlerror to distinguish between success and diff --git a/src/freeze.c b/src/freeze.c index d2e9ea62..3b1fdd95 100644 --- a/src/freeze.c +++ b/src/freeze.c @@ -711,7 +711,7 @@ ill-formed frozen file, invalid builtin %s encountered"), ill-formed frozen file, invalid module %s encountered"), quotearg_style_mem (locale_quoting_style, string[2], number[2])); - module = m4__module_find (string[2]); + module = m4__module_find (context, string[2]); } token = m4_builtin_find_by_name (context, module, string[1]); @@ -966,7 +966,7 @@ ill-formed frozen file, version 2 directive `%c' encountered"), 'T'); ill-formed frozen file, invalid module %s encountered"), quotearg_style_mem (locale_quoting_style, string[2], number[2])); - module = m4__module_find (string[2]); + module = m4__module_find (context, string[2]); } m4_set_symbol_value_text (token, xmemdup0 (string[1], number[1]), -- cgit v1.2.1