Switch PluginSys off KTrie (bug 5884 part 3, r=ds).
authorDavid Anderson <dvander@alliedmods.net>
Sun Aug 25 11:59:46 2013 -0700 (2013-08-25)
changeset 40650f85577d23ee
parent 4064 d7ca1e28949d
child 4066 c055944bdfd6
Switch PluginSys off KTrie (bug 5884 part 3, r=ds).
core/logic/PluginSys.cpp
core/logic/PluginSys.h
public/sm_namehashset.h
public/sm_stringhashmap.h
     1.1 --- a/core/logic/PluginSys.cpp	Sun Aug 25 11:59:45 2013 -0700
     1.2 +++ b/core/logic/PluginSys.cpp	Sun Aug 25 11:59:46 2013 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /**
     1.5 - * vim: set ts=4 :
     1.6 + * vim: set ts=4 sw=4 tw=99 noet :
     1.7   * =============================================================================
     1.8   * SourceMod
     1.9   * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
    1.10 @@ -114,7 +114,7 @@
    1.11  		sizeof(CPlugin) 
    1.12  		+ sizeof(IdentityToken_t)
    1.13  		+ (m_configs.size() * (sizeof(AutoConfig *) + sizeof(AutoConfig)))
    1.14 -		+ m_pProps.mem_usage();
    1.15 +		+ m_Props.mem_usage();
    1.16  
    1.17  	for (unsigned int i = 0; i < m_configs.size(); i++)
    1.18  	{
    1.19 @@ -169,24 +169,22 @@
    1.20  
    1.21  bool CPlugin::GetProperty(const char *prop, void **ptr, bool remove/* =false */)
    1.22  {
    1.23 -	void **ptrpp = m_pProps.retrieve(prop);
    1.24 -	bool exists = !!ptrpp;
    1.25 +	StringHashMap<void *>::Result r = m_Props.find(prop);
    1.26 +	if (!r.found())
    1.27 +		return false;
    1.28  
    1.29 -	if (exists)
    1.30 -	{
    1.31 -		if (ptr)
    1.32 -			*ptr = *ptrpp;
    1.33 -			
    1.34 -		if (remove)
    1.35 -			m_pProps.remove(prop);
    1.36 -	}
    1.37 +	if (ptr)
    1.38 +		*ptr = r->value;
    1.39  
    1.40 -	return exists;
    1.41 +	if (remove)
    1.42 +		m_Props.remove(r);
    1.43 +
    1.44 +	return true;
    1.45  }
    1.46  
    1.47  bool CPlugin::SetProperty(const char *prop, void *ptr)
    1.48  {
    1.49 -	 return m_pProps.insert(prop, ptr);
    1.50 +	 return m_Props.insert(prop, ptr);
    1.51  }
    1.52  
    1.53  IPluginRuntime *CPlugin::GetRuntime()
    1.54 @@ -886,22 +884,19 @@
    1.55  	libsys->CloseDirectory(dir);
    1.56  }
    1.57  
    1.58 -LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength)
    1.59 +LoadRes CPluginManager::_LoadPlugin(CPlugin **aResult, const char *path, bool debug, PluginType type, char error[], size_t maxlength)
    1.60  {
    1.61  	if (m_LoadingLocked)
    1.62 -	{
    1.63  		return LoadRes_NeverLoad;
    1.64 -	}
    1.65  
    1.66  	int err;
    1.67  
    1.68  	/**
    1.69  	 * Does this plugin already exist?
    1.70  	 */
    1.71 -	CPlugin **pluginpp = m_LoadLookup.retrieve(path);
    1.72 -	if (pluginpp && *pluginpp)
    1.73 +	CPlugin *pPlugin;
    1.74 +	if (m_LoadLookup.retrieve(path, &pPlugin))
    1.75  	{
    1.76 -		CPlugin *pPlugin = *pluginpp;
    1.77  		/* Check to see if we should try reloading it */
    1.78  		if (pPlugin->GetStatus() == Plugin_BadLoad
    1.79  			|| pPlugin->GetStatus() == Plugin_Error
    1.80 @@ -911,16 +906,13 @@
    1.81  		}
    1.82  		else
    1.83  		{
    1.84 -			if (_plugin)
    1.85 -			{
    1.86 -				*_plugin = pPlugin;
    1.87 -			}
    1.88 +			if (aResult)
    1.89 +				*aResult = pPlugin;
    1.90  			return LoadRes_AlreadyLoaded;
    1.91  		}
    1.92  	}
    1.93  
    1.94 -	CPlugin *pPlugin = CPlugin::CreatePlugin(path, error, maxlength);
    1.95 -
    1.96 +	pPlugin = CPlugin::CreatePlugin(path, error, maxlength);
    1.97  	assert(pPlugin != NULL);
    1.98  
    1.99  	pPlugin->m_type = PluginType_MapUpdated;
   1.100 @@ -1034,10 +1026,8 @@
   1.101  	time_t t = pPlugin->GetFileTimeStamp();
   1.102  	pPlugin->SetTimeStamp(t);
   1.103  
   1.104 -	if (_plugin)
   1.105 -	{
   1.106 -		*_plugin = pPlugin;
   1.107 -	}
   1.108 +	if (aResult)
   1.109 +		*aResult = pPlugin;
   1.110  
   1.111  	return (pPlugin->GetStatus() == Plugin_Loaded) ? LoadRes_Successful : loadFailure;
   1.112  }
   1.113 @@ -2114,13 +2104,11 @@
   1.114  				const char *ext = libsys->GetFileExtension(arg) ? "" : ".smx";
   1.115  				g_pSM->BuildPath(Path_None, pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
   1.116  
   1.117 -				CPlugin **pluginpp = m_LoadLookup.retrieve(pluginfile);
   1.118 -				if (!pluginpp || !*pluginpp)
   1.119 +				if (!m_LoadLookup.retrieve(pluginfile, &pl))
   1.120  				{
   1.121  					rootmenu->ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
   1.122  					return;
   1.123  				}
   1.124 -				pl = *pluginpp;
   1.125  			}
   1.126  
   1.127  			char name[PLATFORM_MAX_PATH];
   1.128 @@ -2206,13 +2194,11 @@
   1.129  				const char *ext = libsys->GetFileExtension(arg) ? "" : ".smx";
   1.130  				g_pSM->BuildPath(Path_None, pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
   1.131  
   1.132 -				CPlugin **pluginpp = m_LoadLookup.retrieve(pluginfile);
   1.133 -				if (!pluginpp || !*pluginpp)
   1.134 +				if (!m_LoadLookup.retrieve(pluginfile, &pl))
   1.135  				{
   1.136  					rootmenu->ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
   1.137  					return;
   1.138  				}
   1.139 -				pl = *pluginpp;
   1.140  			}
   1.141  
   1.142  			const sm_plugininfo_t *info = pl->GetPublicInfo();
   1.143 @@ -2338,13 +2324,11 @@
   1.144  				const char *ext = libsys->GetFileExtension(arg) ? "" : ".smx";
   1.145  				g_pSM->BuildPath(Path_None, pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
   1.146  
   1.147 -				CPlugin **pluginpp = m_LoadLookup.retrieve(pluginfile);
   1.148 -				if (!pluginpp || !*pluginpp)
   1.149 +				if (!m_LoadLookup.retrieve(pluginfile, &pl))
   1.150  				{
   1.151  					rootmenu->ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
   1.152  					return;
   1.153  				}
   1.154 -				pl = *pluginpp;
   1.155  			}
   1.156  
   1.157  			char name[PLATFORM_MAX_PATH];
   1.158 @@ -2604,13 +2588,8 @@
   1.159  		const char *ext = libsys->GetFileExtension(arg) ? "" : ".smx";
   1.160  		smcore.Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
   1.161  
   1.162 -		CPlugin **pluginpp = m_LoadLookup.retrieve(pluginfile);
   1.163 -		if (!pluginpp)
   1.164 -		{
   1.165 +		if (!m_LoadLookup.retrieve(pluginfile, &pl))
   1.166  			return NULL;
   1.167 -		}
   1.168 -		
   1.169 -		pl = *pluginpp;
   1.170  	}
   1.171  
   1.172  	return pl;
     2.1 --- a/core/logic/PluginSys.h	Sun Aug 25 11:59:45 2013 -0700
     2.2 +++ b/core/logic/PluginSys.h	Sun Aug 25 11:59:46 2013 -0700
     2.3 @@ -1,5 +1,5 @@
     2.4  /**
     2.5 - * vim: set ts=4 :
     2.6 + * vim: set ts=4 sw=4 tw=99 noet :
     2.7   * =============================================================================
     2.8   * SourceMod
     2.9   * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
    2.10 @@ -43,8 +43,9 @@
    2.11  #include <sh_vector.h>
    2.12  #include <sh_string.h>
    2.13  #include "common_logic.h"
    2.14 -#include <sm_trie_tpl.h>
    2.15  #include <IRootConsoleMenu.h>
    2.16 +#include <sm_stringhashmap.h>
    2.17 +#include <sm_namehashset.h>
    2.18  #include "ITranslator.h"
    2.19  #include "IGameConfigs.h"
    2.20  #include "NativeOwner.h"
    2.21 @@ -160,6 +161,11 @@
    2.22  	 * a valid (but error-stated) CPlugin will be returned.
    2.23  	 */
    2.24  	static CPlugin *CreatePlugin(const char *file, char *error, size_t maxlength);
    2.25 +
    2.26 +	static inline bool matches(const char *file, const CPlugin *plugin)
    2.27 +	{
    2.28 +		return strcmp(plugin->m_filename, file) == 0;
    2.29 +	}
    2.30  public:
    2.31  
    2.32  	/**
    2.33 @@ -254,7 +260,7 @@
    2.34  	IPhraseCollection *m_pPhrases;
    2.35  	List<String> m_RequiredLibs;
    2.36  	List<String> m_Libraries;
    2.37 -	KTrie<void *> m_pProps;
    2.38 +	StringHashMap<void *> m_Props;
    2.39  	bool m_FakeNativesMissing;
    2.40  	bool m_LibraryMissing;
    2.41  	CVector<AutoConfig *> m_configs;
    2.42 @@ -460,7 +466,7 @@
    2.43  	List<IPluginsListener *> m_listeners;
    2.44  	List<CPlugin *> m_plugins;
    2.45  	CStack<CPluginManager::CPluginIterator *> m_iters;
    2.46 -	KTrie<CPlugin *> m_LoadLookup;
    2.47 +	NameHashSet<CPlugin *> m_LoadLookup;
    2.48  	bool m_AllPluginsLoaded;
    2.49  	IdentityToken_t *m_MyIdent;
    2.50  
     3.1 --- a/public/sm_namehashset.h	Sun Aug 25 11:59:45 2013 -0700
     3.2 +++ b/public/sm_namehashset.h	Sun Aug 25 11:59:46 2013 -0700
     3.3 @@ -61,7 +61,8 @@
     3.4  
     3.5  	// Default policy type: the two types are different. Use them directly.
     3.6  	template <typename KeyType, typename KeyPolicyType>
     3.7 -	struct Policy {
     3.8 +	struct Policy
     3.9 +	{
    3.10  		typedef KeyType Payload;
    3.11  
    3.12  		static uint32_t hash(const CharsAndLength &key)
     4.1 --- a/public/sm_stringhashmap.h	Sun Aug 25 11:59:45 2013 -0700
     4.2 +++ b/public/sm_stringhashmap.h	Sun Aug 25 11:59:46 2013 -0700
     4.3 @@ -120,7 +120,7 @@
     4.4  	bool retrieve(const char *aKey, T *aResult = NULL)
     4.5  	{
     4.6  		CharsAndLength key(aKey);
     4.7 -		typename Internal::Result r = internal_.find(key);
     4.8 +		Result r = internal_.find(key);
     4.9  		if (!r.found())
    4.10  			return false;
    4.11  		if (aResult)
    4.12 @@ -137,7 +137,7 @@
    4.13  	bool contains(const char *aKey)
    4.14  	{
    4.15  		CharsAndLength key(aKey);
    4.16 -		typename Internal::Result r = internal_.find(key);
    4.17 +		Result r = internal_.find(key);
    4.18  		return r.found();
    4.19  	}
    4.20  
    4.21 @@ -173,7 +173,7 @@
    4.22  	bool remove(const char *aKey)
    4.23  	{
    4.24  		CharsAndLength key(aKey);
    4.25 -		typename Internal::Result r = internal_.find(key);
    4.26 +		Result r = internal_.find(key);
    4.27  		if (!r.found())
    4.28  			return false;
    4.29  		memory_used_ -= key.length() + 1;
    4.30 @@ -181,6 +181,11 @@
    4.31  		return true;
    4.32  	}
    4.33  
    4.34 +	void remove(Result &r)
    4.35 +	{
    4.36 +		internal_.remove(r);
    4.37 +	}
    4.38 +
    4.39  	void clear()
    4.40  	{
    4.41  		internal_.clear();