greatly optimized the process of mapping plugin natives from Core
authorDavid Anderson <dvander@alliedmods.net>
Wed Aug 29 00:47:05 2007 +0000 (2007-08-29)
changeset 13825399d6f24417
parent 1381 a200ec1310f0
child 1383 9e05176c1e92
greatly optimized the process of mapping plugin natives from Core
core/systems/PluginSys.cpp
core/systems/PluginSys.h
core/vm/sp_vm_basecontext.cpp
core/vm/sp_vm_basecontext.h
public/sourcepawn/sp_vm_api.h
     1.1 --- a/core/systems/PluginSys.cpp	Wed Aug 29 00:28:19 2007 +0000
     1.2 +++ b/core/systems/PluginSys.cpp	Wed Aug 29 00:47:05 2007 +0000
     1.3 @@ -769,6 +769,7 @@
     1.4  	m_AllPluginsLoaded = false;
     1.5  	m_MyIdent = NULL;
     1.6  	m_pNativeLookup = sm_trie_create();
     1.7 +	m_pCoreNatives = sm_trie_create();
     1.8  }
     1.9  
    1.10  CPluginManager::~CPluginManager()
    1.11 @@ -780,6 +781,7 @@
    1.12  	 */
    1.13  	sm_trie_destroy(m_LoadLookup);
    1.14  	sm_trie_destroy(m_pNativeLookup);
    1.15 +	sm_trie_destroy(m_pCoreNatives);
    1.16  
    1.17  	CStack<CPluginManager::CPluginIterator *>::iterator iter;
    1.18  	for (iter=m_iters.begin(); iter!=m_iters.end(); iter++)
    1.19 @@ -1325,18 +1327,28 @@
    1.20  
    1.21  void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin)
    1.22  {
    1.23 -	List<sp_nativeinfo_t *>::iterator iter;
    1.24 +	IPluginContext *pContext = pPlugin->GetBaseContext();
    1.25 +	sp_context_t *ctx = pContext->GetContext();
    1.26 +	
    1.27  
    1.28 -	for (iter=m_natives.begin(); iter!=m_natives.end(); iter++)
    1.29 +	uint32_t natives = pContext->GetNativesNum();
    1.30 +	sp_native_t *native;
    1.31 +	SPVM_NATIVE_FUNC pfn;
    1.32 +	for (uint32_t i=0; i<natives; i++)
    1.33  	{
    1.34 -		sp_nativeinfo_t *natives = (*iter);
    1.35 -		IPluginContext *ctx = pPlugin->GetBaseContext();
    1.36 -		unsigned int i=0;
    1.37 -		/* Attempt to bind every native! */
    1.38 -		while (natives[i].func != NULL)
    1.39 +		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
    1.40 +		{
    1.41 +			continue;
    1.42 +		}
    1.43 +		if (native->status == SP_NATIVE_BOUND)
    1.44  		{
    1.45 -			ctx->BindNative(&natives[i++]);
    1.46 +			continue;
    1.47  		}
    1.48 +		if (!sm_trie_retrieve(m_pCoreNatives, native->name, (void **)&pfn))
    1.49 +		{
    1.50 +			continue;
    1.51 +		}
    1.52 +		pContext->BindNativeToIndex(i, pfn);
    1.53  	}
    1.54  }
    1.55  
    1.56 @@ -1854,7 +1866,10 @@
    1.57  
    1.58  void CPluginManager::RegisterNativesFromCore(sp_nativeinfo_t *natives)
    1.59  {
    1.60 -	m_natives.push_back(natives);
    1.61 +	for (unsigned int i = 0; natives[i].func != NULL; i++)
    1.62 +	{
    1.63 +		sm_trie_insert(m_pCoreNatives, natives[i].name, natives[i].func);
    1.64 +	}
    1.65  }
    1.66  
    1.67  IPlugin *CPluginManager::PluginFromHandle(Handle_t handle, HandleError *err)
     2.1 --- a/core/systems/PluginSys.h	Wed Aug 29 00:28:19 2007 +0000
     2.2 +++ b/core/systems/PluginSys.h	Wed Aug 29 00:47:05 2007 +0000
     2.3 @@ -485,12 +485,12 @@
     2.4  private:
     2.5  	List<IPluginsListener *> m_listeners;
     2.6  	List<CPlugin *> m_plugins;
     2.7 -	List<sp_nativeinfo_t *> m_natives;
     2.8  	CStack<CPluginManager::CPluginIterator *> m_iters;
     2.9  	CPluginInfoDatabase m_PluginInfo;
    2.10  	Trie *m_LoadLookup;
    2.11  	bool m_AllPluginsLoaded;
    2.12  	IdentityToken_t *m_MyIdent;
    2.13 +	Trie *m_pCoreNatives;
    2.14  
    2.15  	/* Dynamic native stuff */
    2.16  	List<FakeNative *> m_Natives;
     3.1 --- a/core/vm/sp_vm_basecontext.cpp	Wed Aug 29 00:28:19 2007 +0000
     3.2 +++ b/core/vm/sp_vm_basecontext.cpp	Wed Aug 29 00:47:05 2007 +0000
     3.3 @@ -642,6 +642,22 @@
     3.4  	return SP_ERROR_NONE;
     3.5  }
     3.6  
     3.7 +int BaseContext::BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native)
     3.8 +{
     3.9 +	int err;
    3.10 +	sp_native_t *native;
    3.11 +
    3.12 +	if ((err = GetNativeByIndex(index, &native)) != SP_ERROR_NONE)
    3.13 +	{
    3.14 +		return err;
    3.15 +	}
    3.16 +
    3.17 +	ctx->natives[index].pfn = native;
    3.18 +	ctx->natives[index].status = SP_NATIVE_BOUND;
    3.19 +
    3.20 +	return SP_ERROR_NONE;
    3.21 +}
    3.22 +
    3.23  int BaseContext::BindNativeToAny(SPVM_NATIVE_FUNC native)
    3.24  {
    3.25  	uint32_t nativesnum, i;
     4.1 --- a/core/vm/sp_vm_basecontext.h	Wed Aug 29 00:28:19 2007 +0000
     4.2 +++ b/core/vm/sp_vm_basecontext.h	Wed Aug 29 00:47:05 2007 +0000
     4.3 @@ -89,6 +89,7 @@
     4.4  		cell_t *GetNullRef(SP_NULL_TYPE type);
     4.5  		int LocalToStringNULL(cell_t local_addr, char **addr);
     4.6  #endif
     4.7 +		int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native);
     4.8  	public: //IPluginDebugInfo
     4.9  		int LookupFile(ucell_t addr, const char **filename);
    4.10  		int LookupFunction(ucell_t addr, const char **name);
     5.1 --- a/public/sourcepawn/sp_vm_api.h	Wed Aug 29 00:28:19 2007 +0000
     5.2 +++ b/public/sourcepawn/sp_vm_api.h	Wed Aug 29 00:47:05 2007 +0000
     5.3 @@ -44,7 +44,7 @@
     5.4  #define SOURCEPAWN_ENGINE_API_VERSION	1
     5.5  
     5.6  /** SourcePawn VM API Version */
     5.7 -#define SOURCEPAWN_VM_API_VERSION		4
     5.8 +#define SOURCEPAWN_VM_API_VERSION		5
     5.9  
    5.10  #if !defined SOURCEMOD_BUILD
    5.11  #define SOURCEMOD_BUILD
    5.12 @@ -591,6 +591,14 @@
    5.13  		 */
    5.14  		virtual int LocalToStringNULL(cell_t local_addr, char **addr) =0;
    5.15  #endif
    5.16 +
    5.17 +		/**
    5.18 +		 * @brief Binds a single native to a given native index.
    5.19 +		 *
    5.20 +		 * @param index			Index to bind at.
    5.21 +		 * @param native		Native function to bind.
    5.22 +		 */
    5.23 +		virtual int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native) =0;
    5.24  	};
    5.25  
    5.26