From 9a3ac8f6b887a79f490030ee7a254a2b7787b67e Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 5 Sep 2016 11:30:21 -0400 Subject: [PATCH] Imgtool: Changed the Imgtool module list to be std::list --- src/tools/imgtool/imgtool.cpp | 21 ++++--- src/tools/imgtool/imgtool.h | 1 + src/tools/imgtool/library.cpp | 114 ++++++++-------------------------- src/tools/imgtool/library.h | 19 +++--- src/tools/imgtool/main.cpp | 8 +-- src/tools/imgtool/modules.cpp | 4 +- 6 files changed, 53 insertions(+), 114 deletions(-) diff --git a/src/tools/imgtool/imgtool.cpp b/src/tools/imgtool/imgtool.cpp index 3e97a26b47b..64a84a74e0c 100644 --- a/src/tools/imgtool/imgtool.cpp +++ b/src/tools/imgtool/imgtool.cpp @@ -255,6 +255,15 @@ const imgtool_module *imgtool_find_module(const char *modulename) } +/*------------------------------------------------- + imgtool_find_module - looks up a module +-------------------------------------------------*/ + +const imgtool::library::modulelist &imgtool_get_modules() +{ + return global_imgtool_library->modules(); +} + /*------------------------------------------------- imgtool_get_module_features - retrieves a @@ -385,7 +394,6 @@ imgtoolerr_t imgtool_identify_file(const char *fname, imgtool_module **modules, { imgtoolerr_t err = IMGTOOLERR_SUCCESS; imgtool::library *library = global_imgtool_library.get(); - imgtool_module *module = nullptr; imgtool_module *insert_module; imgtool_module *temp_module; size_t i = 0; @@ -418,15 +426,15 @@ imgtoolerr_t imgtool_identify_file(const char *fname, imgtool_module **modules, extension++; /* iterate through all modules */ - while((module = library->iterate(module)) != nullptr) + for (const auto &module : library->modules()) { if (!extension || image_find_extension(module->extensions, extension)) { - err = evaluate_module(fname, module, &val); + err = evaluate_module(fname, module.get(), &val); if (err) goto done; - insert_module = module; + insert_module = module.get(); for (i = 0; (val > 0.0f) && (i < count); i++) { if (val > values[i]) @@ -927,7 +935,6 @@ int imgtool_validitychecks(void) { int error = 0; imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS; - const imgtool_module *module = nullptr; imgtool_module_features features; int created_library = FALSE; @@ -937,9 +944,9 @@ int imgtool_validitychecks(void) created_library = TRUE; } - while((module = global_imgtool_library->iterate(module)) != nullptr) + for (const auto &module : global_imgtool_library->modules()) { - features = imgtool_get_module_features(module); + features = imgtool_get_module_features(module.get()); if (!module->name) { diff --git a/src/tools/imgtool/imgtool.h b/src/tools/imgtool/imgtool.h index d98a208602a..65b843ec5b8 100644 --- a/src/tools/imgtool/imgtool.h +++ b/src/tools/imgtool/imgtool.h @@ -81,6 +81,7 @@ struct imgtool_partition_features void imgtool_init(bool omit_untested, void (*warning)(const char *message)); void imgtool_exit(void); const imgtool_module *imgtool_find_module(const char *modulename); +const imgtool::library::modulelist &imgtool_get_modules(); imgtool_module_features imgtool_get_module_features(const imgtool_module *module); void imgtool_warn(const char *format, ...) ATTR_PRINTF(1,2); char *imgtool_basename(char *filename); diff --git a/src/tools/imgtool/library.cpp b/src/tools/imgtool/library.cpp index da1c496450c..eb6013d5f04 100644 --- a/src/tools/imgtool/library.cpp +++ b/src/tools/imgtool/library.cpp @@ -10,6 +10,7 @@ ****************************************************************************/ #include +#include #include "imgtool.h" #include "library.h" @@ -27,8 +28,6 @@ library::library() m_pool = pool_alloc_lib(nullptr); if (!m_pool) throw std::bad_alloc(); - m_first = nullptr; - m_last = nullptr; } @@ -48,18 +47,12 @@ library::~library() void library::add_class(const imgtool_class *imgclass) { - imgtool_module *module; char *s1, *s2; // allocate the module and place it in the chain - module = (imgtool_module *)imgtool_library_malloc(sizeof(*module)); + m_modules.emplace_back(std::unique_ptr(new imgtool_module)); + imgtool_module *module = m_modules.back().get(); memset(module, 0, sizeof(*module)); - module->previous = m_last; - if (m_last) - m_last->next = module; - else - m_first = module; - m_last = module; // extensions have a weird format s1 = imgtool_get_info_string(imgclass, IMGTOOLINFO_STR_FILE_EXTENSIONS); @@ -139,26 +132,11 @@ void library::add(imgtool_get_info get_info) // unlink //------------------------------------------------- -const imgtool_module *library::unlink(const char *module) +void library::unlink(const char *module_name) { - imgtool_module *m; - imgtool_module **previous; - imgtool_module **next; - - for (m = m_first; m; m = m->next) - { - if (!core_stricmp(m->name, module)) - { - previous = m->previous ? &m->previous->next : &m_first; - next = m->next ? &m->next->previous : &m_last; - *previous = m->next; - *next = m->previous; - m->previous = nullptr; - m->next = nullptr; - return m; - } - } - return nullptr; + modulelist::iterator iter = find(module_name); + if (iter != m_modules.end()) + m_modules.erase(iter); } @@ -188,40 +166,24 @@ int library::module_compare(const imgtool_module *m1, const imgtool_module *m2, void library::sort(sort_type sort) { - imgtool_module *m1; - imgtool_module *m2; - imgtool_module *target; - imgtool_module **before; - imgtool_module **after; - - for (m1 = m_first; m1; m1 = m1->next) + auto compare = [&](const std::unique_ptr &a, const std::unique_ptr &b) { - target = m1; - for (m2 = m1->next; m2; m2 = m2->next) - { - while(module_compare(target, m2, sort) > 0) - target = m2; - } + return module_compare(a.get(), b.get(), sort) < 0; + }; + m_modules.sort(compare); +} - if (target != m1) - { - // unlink the target - before = target->previous ? &target->previous->next : &m_first; - after = target->next ? &target->next->previous : &m_last; - *before = target->next; - *after = target->previous; - // now place the target before m1 - target->previous = m1->previous; - target->next = m1; - before = m1->previous ? &m1->previous->next : &m_first; - *before = target; - m1->previous = target; +//------------------------------------------------- +// find +//------------------------------------------------- - // since we changed the order, we have to replace ourselves - m1 = target; - } - } +library::modulelist::iterator library::find(const char *module_name) +{ + return std::find_if( + m_modules.begin(), + m_modules.end(), + [&](std::unique_ptr &module) { return !strcmp(module->name, module_name); }); } @@ -231,36 +193,10 @@ void library::sort(sort_type sort) const imgtool_module *library::findmodule(const char *module_name) { - const imgtool_module *module; - - module = m_first; - while(module && module_name && strcmp(module->name, module_name)) - module = module->next; - return module; -} - - -//------------------------------------------------- -// iterate -//------------------------------------------------- - -imgtool_module *library::iterate(const imgtool_module *module) -{ - return module ? module->next : m_first; -} - - -//------------------------------------------------- -// index -//------------------------------------------------- - -imgtool_module *library::index(int i) -{ - imgtool_module *module; - module = m_first; - while(module && i--) - module = module->next; - return module; + modulelist::iterator iter = find(module_name); + return iter != m_modules.end() + ? iter->get() + : nullptr; } diff --git a/src/tools/imgtool/library.h b/src/tools/imgtool/library.h index 6c2212454d1..72bade1b24d 100644 --- a/src/tools/imgtool/library.h +++ b/src/tools/imgtool/library.h @@ -18,6 +18,7 @@ #define LIBRARY_H #include +#include #include "corestr.h" #include "opresolv.h" @@ -330,9 +331,6 @@ char *imgtool_temp_str(void); struct imgtool_module { - imgtool_module *previous; - imgtool_module *next; - imgtool_class imgclass; const char *name; @@ -379,6 +377,8 @@ namespace imgtool { class library { public: + typedef std::list > modulelist; + enum class sort_type { NAME, @@ -392,7 +392,7 @@ public: void add(imgtool_get_info get_info); // seeks out and removes a module from an imgtool library - const imgtool_module *unlink(const char *module); + void unlink(const char *module_name); // sorts an imgtool library void sort(sort_type sort); @@ -401,13 +401,14 @@ public: const imgtool_module *findmodule(const char *module_name); // module iteration - imgtool_module *iterate(const imgtool_module *module); - imgtool_module *index(int i); + const modulelist &modules() { return m_modules; } private: - object_pool * m_pool; - imgtool_module * m_first; - imgtool_module * m_last; + object_pool * m_pool; + modulelist m_modules; + + // internal lookup and iteration + modulelist::iterator find(const char *module_name); // helpers void add_class(const imgtool_class *imgclass); diff --git a/src/tools/imgtool/main.cpp b/src/tools/imgtool/main.cpp index a57e6d8154a..0c7db16a1d3 100644 --- a/src/tools/imgtool/main.cpp +++ b/src/tools/imgtool/main.cpp @@ -707,15 +707,11 @@ done: static int cmd_listformats(const struct command *c, int argc, char *argv[]) { - const imgtool_module *mod; - fprintf(stdout, "Image formats supported by imgtool:\n\n"); - mod = imgtool_find_module(nullptr); - while(mod) + for (const auto &module : imgtool_get_modules()) { - fprintf(stdout, " %-25s%s\n", mod->name, mod->description); - mod = mod->next; + fprintf(stdout, " %-25s%s\n", module->name, module->description); } return 0; diff --git a/src/tools/imgtool/modules.cpp b/src/tools/imgtool/modules.cpp index 5c9768c4423..80a0e0d2742 100644 --- a/src/tools/imgtool/modules.cpp +++ b/src/tools/imgtool/modules.cpp @@ -30,7 +30,6 @@ static void (*const modules[])(const imgtool_class *imgclass, UINT32 state, unio imgtoolerr_t imgtool_create_cannonical_library(bool omit_untested, std::unique_ptr &library) { size_t i; - imgtool_module *module; /* list of modules that we drop */ static const char *const irrelevant_modules[] = @@ -55,8 +54,7 @@ imgtoolerr_t imgtool_create_cannonical_library(bool omit_untested, std::unique_p // if we are omitting untested, go through and block out the functionality in question if (omit_untested) { - module = nullptr; - while((module = library->iterate(module)) != nullptr) + for (auto &module : library->modules()) { if (module->writing_untested) {