From af658bcf69e62f75ec6ea084ccd84fe052e8e367 Mon Sep 17 00:00:00 2001 From: "Gary V. Vaughan" Date: Fri, 20 Sep 2013 18:17:02 +0700 Subject: modules: store loaded modules in context struct. * m4/m4private.h (struct m4_module): Add a next pointer. (struct m4): Add a module list pointer. * m4/module.c (m4__module_open): Initialise the next pointer and update the list head when a new module is successfully opened. (m4_module_next): Replace the ugly libltdl twiddling with a straight forward module list traversal one-liner! Add a context parameter. Adjust all callers. Signed-off-by: Gary V. Vaughan --- m4/builtin.c | 12 ++++++------ m4/m4module.h | 6 +++--- m4/m4private.h | 2 ++ m4/module.c | 24 ++++++------------------ modules/gnu.c | 8 ++++---- src/freeze.c | 14 +++++++------- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/m4/builtin.c b/m4/builtin.c index c5107534..b495e5cb 100644 --- a/m4/builtin.c +++ b/m4/builtin.c @@ -39,9 +39,9 @@ compare_builtin_name_CB (const void *name, const void *b) symbol value, suitable for use in the symbol table or for an argument to m4_push_builtin. */ m4_symbol_value * M4_GNUC_PURE -m4_builtin_find_by_name (m4_module *module, const char *name) +m4_builtin_find_by_name (m4 *context, m4_module *module, const char *name) { - m4_module *cur = module ? module : m4_module_next (NULL); + m4_module *cur = module ? module : m4_module_next (context, NULL); m4__builtin *bp; do @@ -55,7 +55,7 @@ m4_builtin_find_by_name (m4_module *module, const char *name) return token; } } - while (!module && (cur = m4_module_next (cur))); + while (!module && (cur = m4_module_next (context, cur))); return NULL; } @@ -65,9 +65,9 @@ m4_builtin_find_by_name (m4_module *module, const char *name) malloc'd symbol value, suitable for use in the symbol table or for an argument to m4_push_builtin. */ m4_symbol_value * M4_GNUC_PURE -m4_builtin_find_by_func (m4_module *module, m4_builtin_func *func) +m4_builtin_find_by_func (m4 *context, m4_module *module, m4_builtin_func *func) { - m4_module *cur = module ? module : m4_module_next (NULL); + m4_module *cur = module ? module : m4_module_next (context, NULL); size_t i; do @@ -81,7 +81,7 @@ m4_builtin_find_by_func (m4_module *module, m4_builtin_func *func) return token; } } - while (!module && (cur = m4_module_next (cur))); + while (!module && (cur = m4_module_next (context, cur))); return 0; } diff --git a/m4/m4module.h b/m4/m4module.h index 92710a7e..038a428b 100644 --- a/m4/m4module.h +++ b/m4/m4module.h @@ -241,7 +241,7 @@ extern void m4_install_builtins (m4*, m4_module *, const m4_builtin*); extern void m4_install_macros (m4*, m4_module *, const m4_macro*); extern const char * m4_get_module_name (const m4_module *); -extern m4_module * m4_module_next (m4_module *); +extern m4_module * m4_module_next (m4*, m4_module *); @@ -330,8 +330,8 @@ extern void m4_set_symbol_value_placeholder (m4_symbol_value *, /* --- BUILTIN MANAGEMENT --- */ -extern m4_symbol_value *m4_builtin_find_by_name (m4_module *, const char *); -extern m4_symbol_value *m4_builtin_find_by_func (m4_module *, +extern m4_symbol_value *m4_builtin_find_by_name (m4 *, m4_module *, const char *); +extern m4_symbol_value *m4_builtin_find_by_func (m4 *, m4_module *, m4_builtin_func *); diff --git a/m4/m4private.h b/m4/m4private.h index 916330ca..dbda70ca 100644 --- a/m4/m4private.h +++ b/m4/m4private.h @@ -59,6 +59,7 @@ typedef unsigned int bool_bitfield; struct m4 { m4_symbol_table * symtab; m4_syntax_table * syntax; + m4_module * modules; const char * current_file; /* Current input file. */ int current_line; /* Current input line. */ @@ -183,6 +184,7 @@ struct m4_module m4__builtin *builtins; /* Sorted array of builtins. */ m4_macro *macros; /* Unsorted array of macros. */ size_t builtins_len; /* Number of builtins. */ + m4_module *next; }; extern void m4__module_init (m4 *context); diff --git a/m4/module.c b/m4/module.c index db26198a..dce0f5bf 100644 --- a/m4/module.c +++ b/m4/module.c @@ -246,26 +246,11 @@ m4__module_interface (lt_dlhandle handle, const char *id_string) } -/* Return successive loaded modules that pass the interface test registered - with the interface id. */ +/* Return successive loaded modules. */ m4_module * -m4_module_next (m4_module *module) +m4_module_next (m4 *context, m4_module *module) { - lt_dlhandle handle = module ? module->handle : NULL; - assert (iface_id); - - /* Resident modules still show up in the lt_dlhandle_iterate loop - after they have been unloaded from m4. */ - do - { - handle = lt_dlhandle_iterate (iface_id, handle); - if (!handle) - return NULL; - module = (m4_module *) lt_dlcaller_get_data (iface_id, handle); - } - while (!module); - assert (module->handle == handle); - return module; + return module ? module->next : context->modules; } /* Return the first loaded module that passes the registered interface test @@ -414,6 +399,9 @@ m4__module_open (m4 *context, const char *name, m4_obstack *obs) module = (m4_module *) xzalloc (sizeof *module); module->name = xstrdup (name); module->handle = handle; + module->next = context->modules; + + context->modules = module; /* clear out any stale errors, since we have to use lt_dlerror to distinguish between success and diff --git a/modules/gnu.c b/modules/gnu.c index 5c9b0eed..96c75475 100644 --- a/modules/gnu.c +++ b/modules/gnu.c @@ -417,7 +417,7 @@ M4BUILTIN_HANDLER (builtin) name = M4ARG (2); len = M4ARGLEN (2); if (len == strlen (name)) - value = m4_builtin_find_by_name (NULL, name); + value = m4_builtin_find_by_name (context, NULL, name); if (value) { m4_push_builtin (context, obs, value); @@ -435,7 +435,7 @@ M4BUILTIN_HANDLER (builtin) name = M4ARG (1); len = M4ARGLEN (1); if (len == strlen (name)) - value = m4_builtin_find_by_name (NULL, name); + value = m4_builtin_find_by_name (context, NULL, name); if (value == NULL) { if (m4_is_debug_bit (context, M4_DEBUG_TRACE_DEREF)) @@ -1007,7 +1007,7 @@ M4BUILTIN_HANDLER (m4modules) { /* The expansion of this builtin is a comma separated list of loaded modules. */ - m4_module *module = m4_module_next (NULL); + m4_module *module = m4_module_next (context, NULL); if (module) do @@ -1015,7 +1015,7 @@ M4BUILTIN_HANDLER (m4modules) m4_shipout_string (context, obs, m4_get_module_name (module), SIZE_MAX, true); - if ((module = m4_module_next (module))) + if ((module = m4_module_next (context, module))) obstack_1grow (obs, ','); } while (module); diff --git a/src/freeze.c b/src/freeze.c index 5ac913d8..d2e9ea62 100644 --- a/src/freeze.c +++ b/src/freeze.c @@ -33,7 +33,7 @@ static void produce_mem_dump (FILE *, const char *, size_t); static void produce_resyntax_dump (m4 *, FILE *); static void produce_syntax_dump (FILE *, m4_syntax_table *, char); -static void produce_module_dump (FILE *, m4_module *); +static void produce_module_dump (m4 *, FILE *, m4_module *); static void produce_symbol_dump (m4 *, FILE *, m4_symbol_table *); static void *dump_symbol_CB (m4_symbol_table *, const char *, size_t, m4_symbol *, void *); @@ -148,17 +148,17 @@ produce_debugmode_state (FILE *file, int flags) } /* The modules must be dumped in the order in which they will be - reloaded from the frozen file. libltdl stores handles in a push + reloaded from the frozen file. We store handles in a push down stack, so we need to dump them in the reverse order to that. */ static void -produce_module_dump (FILE *file, m4_module *module) +produce_module_dump (m4 *context, FILE *file, m4_module *module) { const char *name = m4_get_module_name (module); size_t len = strlen (name); - module = m4_module_next (module); + module = m4_module_next (context, module); if (module) - produce_module_dump (file, module); + produce_module_dump (context, file, module); xfprintf (file, "M%zu\n", len); produce_mem_dump (file, name, len); @@ -322,7 +322,7 @@ produce_frozen_state (m4 *context, const char *name) produce_debugmode_state (file, m4_get_debug_level_opt (context)); /* Dump all loaded modules. */ - produce_module_dump (file, m4_module_next (NULL)); + produce_module_dump (context, file, m4_module_next (context, NULL)); /* Dump all symbols. */ produce_symbol_dump (context, file, M4SYMTAB); @@ -713,7 +713,7 @@ ill-formed frozen file, invalid module %s encountered"), string[2], number[2])); module = m4__module_find (string[2]); } - token = m4_builtin_find_by_name (module, string[1]); + token = m4_builtin_find_by_name (context, module, string[1]); if (token == NULL) { -- cgit v1.2.1