From 7fe5347210730622badcb6ed32d90114bd8cbeb4 Mon Sep 17 00:00:00 2001 From: brain Date: Sun, 4 Nov 2007 19:12:55 +0000 Subject: [PATCH] Remove our vectors of Module*/ircd_module*, replace with a map of std::pair git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8513 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/configreader.h | 5 -- include/modules.h | 14 +-- src/configreader.cpp | 5 +- src/inspircd.cpp | 3 - src/modules.cpp | 189 +++++++++++++---------------------------- 5 files changed, 66 insertions(+), 150 deletions(-) diff --git a/include/configreader.h b/include/configreader.h index 67fe2e2bf..76ca6084a 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -499,11 +499,6 @@ class CoreExport ServerConfig : public Extensible */ ClassVector Classes; - /** A list of module names (names only, no paths) - * which are currently loaded by the server. - */ - std::vector module_names; - /** A list of the classes for listening client ports */ std::vector ports; diff --git a/include/modules.h b/include/modules.h index a42292ad1..49fd0da50 100644 --- a/include/modules.h +++ b/include/modules.h @@ -1565,22 +1565,12 @@ class CoreExport ModuleManager : public classbase */ InspIRCd* Instance; + std::map > Modules; + public: EventHandlerList EventHandlers; - /** A list of ircd_module* module handles - * Note that this list is always exactly 255 in size. - * The actual number of loaded modules is available from GetModuleCount() - */ - ModuleHandleList handles; - - /** A list of Module* module classes - * Note that this list is always exactly 255 in size. - * The actual number of loaded modules is available from GetModuleCount() - */ - ModuleList modules; - /** Simple, bog-standard, boring constructor. */ ModuleManager(InspIRCd* Ins); diff --git a/src/configreader.cpp b/src/configreader.cpp index f1fedfe96..e7eb84ff8 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -668,10 +668,11 @@ bool InitModule(ServerConfig* conf, const char*) new_module_names.clear(); added_modules.clear(); removed_modules.clear(); - for (std::vector::iterator t = conf->module_names.begin(); t != conf->module_names.end(); t++) + /** FIXME **/ + /*for (std::vector::iterator t = conf->module_names.begin(); t != conf->module_names.end(); t++) { old_module_names.push_back(*t); - } + }*/ return true; } diff --git a/src/inspircd.cpp b/src/inspircd.cpp index e582a5d6a..1f46fa875 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -441,9 +441,6 @@ InspIRCd::InspIRCd(int argc, char** argv) // Get XLine to do it's thing. this->XLines->CheckELines(); this->XLines->ApplyLines(); - - this->Modules->modules.resize(255); - this->Modules->handles.resize(255); /* * Initialise SID/UID. diff --git a/src/modules.cpp b/src/modules.cpp index 67a37c573..7bc9f94c4 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -401,7 +401,7 @@ bool ModuleManager::Load(const char* filename) return false; } - if(find(Instance->Config->module_names.begin(), Instance->Config->module_names.end(), filename_str) != Instance->Config->module_names.end()) + if (Modules.find(filename_str) != Modules.end()) { Instance->Log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile); snprintf(MODERR, MAXBUF, "Module already loaded"); @@ -420,10 +420,7 @@ bool ModuleManager::Load(const char* filename) * the module file or getting a pointer to the init_module symbol. */ newhandle = new ircd_module(Instance, modfile, "init_module"); - - handles[this->ModCount+1] = newhandle; - - newmod = handles[this->ModCount+1]->CallInit(); + newmod = newhandle->CallInit(); if(newmod) { @@ -441,11 +438,7 @@ bool ModuleManager::Load(const char* filename) Instance->Log(DEFAULT,"New module introduced: %s (API version %d, Module version %d.%d.%d.%d)%s", filename, v.API, v.Major, v.Minor, v.Revision, v.Build, (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]")); } - modules[this->ModCount+1] = newmod; - - /* save the module and the module's classfactory, if - * this isnt done, random crashes can occur :/ */ - Instance->Config->module_names.push_back(filename); + Modules[filename_str] = std::make_pair(newhandle, newmod); } else { @@ -474,123 +467,70 @@ bool ModuleManager::Load(const char* filename) } this->ModCount++; - FOREACH_MOD_I(Instance,I_OnLoadModule,OnLoadModule(modules[this->ModCount],filename_str)); + FOREACH_MOD_I(Instance,I_OnLoadModule,OnLoadModule(newmod, filename_str)); - for (int n = 0; n != this->ModCount+1; ++n) - modules[n]->Prioritize(); + for (std::map >::iterator n = Modules.begin(); n != Modules.end(); ++n) + n->second.second->Prioritize(); Instance->BuildISupport(); return true; } -bool ModuleManager::EraseHandle(unsigned int j) -{ - ModuleHandleList::iterator iter; - - if (j >= handles.size()) - { - return false; - } - - iter = handles.begin() + j; - - if(*iter) - { - delete *iter; - handles.erase(iter); - handles.push_back(NULL); - } - - return true; -} - -bool ModuleManager::EraseModule(unsigned int j) +bool ModuleManager::Unload(const char* filename) { - bool success = false; - - ModuleList::iterator iter; - - if (j >= modules.size()) - { - return false; - } - - iter = modules.begin() + j; - - if (*iter) - { - delete *iter; - modules.erase(iter); - modules.push_back(NULL); - success = true; - } + std::string filename_str = filename; + std::map >::iterator modfind = Modules.find(filename); - std::vector::iterator iter2; - - if (j >= Instance->Config->module_names.size()) + if (modfind != Modules.end()) { - return false; - } + if (modfind->second.second->GetVersion().Flags & VF_STATIC) + { + Instance->Log(DEFAULT,"Failed to unload STATIC module %s",filename); + snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)"); + return false; + } + std::pair intercount = GetInterfaceInstanceCount(modfind->second.second); + if (intercount.first > 0) + { + Instance->Log(DEFAULT,"Failed to unload module %s, being used by %d other(s) via interface '%s'",filename, intercount.first, intercount.second.c_str()); + snprintf(MODERR,MAXBUF,"Module not unloadable (Still in use by %d other module%s which %s using its interface '%s') -- unload dependent modules first!", + intercount.first, + intercount.first > 1 ? "s" : "", + intercount.first > 1 ? "are" : "is", + intercount.second.c_str()); + return false; + } - iter2 = Instance->Config->module_names.begin() + j; + /* Give the module a chance to tidy out all its metadata */ + for (chan_hash::iterator c = Instance->chanlist->begin(); c != Instance->chanlist->end(); c++) + { + modfind->second.second->OnCleanup(TYPE_CHANNEL,c->second); + } + for (user_hash::iterator u = Instance->clientlist->begin(); u != Instance->clientlist->end(); u++) + { + modfind->second.second->OnCleanup(TYPE_USER,u->second); + } - Instance->Config->module_names.erase(iter2); - success = true; + /* Tidy up any dangling resolvers */ + Instance->Res->CleanResolvers(modfind->second.second); - return success; -} -bool ModuleManager::Unload(const char* filename) -{ - std::string filename_str = filename; - for (unsigned int j = 0; j != Instance->Config->module_names.size(); j++) - { - if (Instance->Config->module_names[j] == filename_str) - { - if (modules[j]->GetVersion().Flags & VF_STATIC) - { - Instance->Log(DEFAULT,"Failed to unload STATIC module %s",filename); - snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)"); - return false; - } - std::pair intercount = GetInterfaceInstanceCount(modules[j]); - if (intercount.first > 0) - { - Instance->Log(DEFAULT,"Failed to unload module %s, being used by %d other(s) via interface '%s'",filename, intercount.first, intercount.second.c_str()); - snprintf(MODERR,MAXBUF,"Module not unloadable (Still in use by %d other module%s which %s using its interface '%s') -- unload dependent modules first!", - intercount.first, - intercount.first > 1 ? "s" : "", - intercount.first > 1 ? "are" : "is", - intercount.second.c_str()); - return false; - } - /* Give the module a chance to tidy out all its metadata */ - for (chan_hash::iterator c = Instance->chanlist->begin(); c != Instance->chanlist->end(); c++) - { - modules[j]->OnCleanup(TYPE_CHANNEL,c->second); - } - for (user_hash::iterator u = Instance->clientlist->begin(); u != Instance->clientlist->end(); u++) - { - modules[j]->OnCleanup(TYPE_USER,u->second); - } + FOREACH_MOD_I(Instance,I_OnUnloadModule,OnUnloadModule(modfind->second.second, modfind->first)); - /* Tidy up any dangling resolvers */ - Instance->Res->CleanResolvers(modules[j]); + this->DetachAll(modfind->second.second); - FOREACH_MOD_I(Instance,I_OnUnloadModule,OnUnloadModule(modules[j],Instance->Config->module_names[j])); + Instance->Parser->RemoveCommands(filename); - this->DetachAll(modules[j]); + delete modfind->second.second; + delete modfind->second.first; + Modules.erase(modfind); - // found the module - Instance->Parser->RemoveCommands(filename); - this->EraseModule(j); - this->EraseHandle(j); - Instance->Log(DEFAULT,"Module %s unloaded",filename); - this->ModCount--; - Instance->BuildISupport(); - return true; - } + Instance->Log(DEFAULT,"Module %s unloaded",filename); + this->ModCount--; + Instance->BuildISupport(); + return true; } + Instance->Log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename); snprintf(MODERR,MAXBUF,"Module not loaded"); return false; @@ -600,7 +540,6 @@ bool ModuleManager::Unload(const char* filename) void ModuleManager::LoadAll() { char configToken[MAXBUF]; - Instance->Config->module_names.clear(); ModCount = -1; for(int count = 0; count < Instance->Config->ConfValueEnum(Instance->Config->config_data, "module"); count++) @@ -730,19 +669,15 @@ std::pair ModuleManager::GetInterfaceInstanceCount(Module* m) const std::string& ModuleManager::GetModuleName(Module* m) { - static std::string nothing; /* Prevent compiler warning */ + static std::string nothing; - if (!this->GetCount()) - return nothing; - - for (int i = 0; i <= this->GetCount(); i++) + for (std::map >::iterator n = Modules.begin(); n != Modules.end(); ++n) { - if (this->modules[i] == m) - { - return Instance->Config->module_names[i]; - } + if (n->second.second == m) + return n->first; } - return nothing; /* As above */ + + return nothing; } /* This is ugly, yes, but hash_map's arent designed to be @@ -842,14 +777,12 @@ bool InspIRCd::AddResolver(Resolver* r, bool cached) Module* ModuleManager::Find(const std::string &name) { - for (int i = 0; i <= this->GetCount(); i++) - { - if (Instance->Config->module_names[i] == name) - { - return this->modules[i]; - } - } - return NULL; + std::map >::iterator modfind = Modules.find(name); + + if (modfind == Modules.end()) + return NULL; + else + return modfind->second.second; } ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance) -- 2.39.5