From f145178fc0be6adc09336d5ede559fa98dc23618 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 29 Aug 2007 00:47:05 +0000 Subject: [PATCH] greatly optimized the process of mapping plugin natives from Core --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401388 --- core/systems/PluginSys.cpp | 33 ++++++++++++++++++++++++--------- core/systems/PluginSys.h | 2 +- core/vm/sp_vm_basecontext.cpp | 16 ++++++++++++++++ core/vm/sp_vm_basecontext.h | 1 + public/sourcepawn/sp_vm_api.h | 10 +++++++++- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index 87138a88..9e2a52db 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -769,6 +769,7 @@ CPluginManager::CPluginManager() m_AllPluginsLoaded = false; m_MyIdent = NULL; m_pNativeLookup = sm_trie_create(); + m_pCoreNatives = sm_trie_create(); } CPluginManager::~CPluginManager() @@ -780,6 +781,7 @@ CPluginManager::~CPluginManager() */ sm_trie_destroy(m_LoadLookup); sm_trie_destroy(m_pNativeLookup); + sm_trie_destroy(m_pCoreNatives); CStack::iterator iter; for (iter=m_iters.begin(); iter!=m_iters.end(); iter++) @@ -1325,18 +1327,28 @@ bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxleng void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin) { - List::iterator iter; + IPluginContext *pContext = pPlugin->GetBaseContext(); + sp_context_t *ctx = pContext->GetContext(); + - for (iter=m_natives.begin(); iter!=m_natives.end(); iter++) + uint32_t natives = pContext->GetNativesNum(); + sp_native_t *native; + SPVM_NATIVE_FUNC pfn; + for (uint32_t i=0; iGetBaseContext(); - unsigned int i=0; - /* Attempt to bind every native! */ - while (natives[i].func != NULL) + if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE) { - ctx->BindNative(&natives[i++]); + continue; } + if (native->status == SP_NATIVE_BOUND) + { + continue; + } + if (!sm_trie_retrieve(m_pCoreNatives, native->name, (void **)&pfn)) + { + continue; + } + pContext->BindNativeToIndex(i, pfn); } } @@ -1854,7 +1866,10 @@ void CPluginManager::OnHandleDestroy(HandleType_t type, void *object) void CPluginManager::RegisterNativesFromCore(sp_nativeinfo_t *natives) { - m_natives.push_back(natives); + for (unsigned int i = 0; natives[i].func != NULL; i++) + { + sm_trie_insert(m_pCoreNatives, natives[i].name, natives[i].func); + } } IPlugin *CPluginManager::PluginFromHandle(Handle_t handle, HandleError *err) diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index 2b297874..44fcd109 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -485,12 +485,12 @@ private: private: List m_listeners; List m_plugins; - List m_natives; CStack m_iters; CPluginInfoDatabase m_PluginInfo; Trie *m_LoadLookup; bool m_AllPluginsLoaded; IdentityToken_t *m_MyIdent; + Trie *m_pCoreNatives; /* Dynamic native stuff */ List m_Natives; diff --git a/core/vm/sp_vm_basecontext.cpp b/core/vm/sp_vm_basecontext.cpp index 898fae1a..9b2efa26 100644 --- a/core/vm/sp_vm_basecontext.cpp +++ b/core/vm/sp_vm_basecontext.cpp @@ -642,6 +642,22 @@ int BaseContext::BindNative(const sp_nativeinfo_t *native) return SP_ERROR_NONE; } +int BaseContext::BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native) +{ + int err; + sp_native_t *native; + + if ((err = GetNativeByIndex(index, &native)) != SP_ERROR_NONE) + { + return err; + } + + ctx->natives[index].pfn = native; + ctx->natives[index].status = SP_NATIVE_BOUND; + + return SP_ERROR_NONE; +} + int BaseContext::BindNativeToAny(SPVM_NATIVE_FUNC native) { uint32_t nativesnum, i; diff --git a/core/vm/sp_vm_basecontext.h b/core/vm/sp_vm_basecontext.h index 0ba37040..322ca35d 100644 --- a/core/vm/sp_vm_basecontext.h +++ b/core/vm/sp_vm_basecontext.h @@ -89,6 +89,7 @@ namespace SourcePawn cell_t *GetNullRef(SP_NULL_TYPE type); int LocalToStringNULL(cell_t local_addr, char **addr); #endif + int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native); public: //IPluginDebugInfo int LookupFile(ucell_t addr, const char **filename); int LookupFunction(ucell_t addr, const char **name); diff --git a/public/sourcepawn/sp_vm_api.h b/public/sourcepawn/sp_vm_api.h index 6ae819a3..3f54d777 100644 --- a/public/sourcepawn/sp_vm_api.h +++ b/public/sourcepawn/sp_vm_api.h @@ -44,7 +44,7 @@ #define SOURCEPAWN_ENGINE_API_VERSION 1 /** SourcePawn VM API Version */ -#define SOURCEPAWN_VM_API_VERSION 4 +#define SOURCEPAWN_VM_API_VERSION 5 #if !defined SOURCEMOD_BUILD #define SOURCEMOD_BUILD @@ -591,6 +591,14 @@ namespace SourcePawn */ virtual int LocalToStringNULL(cell_t local_addr, char **addr) =0; #endif + + /** + * @brief Binds a single native to a given native index. + * + * @param index Index to bind at. + * @param native Native function to bind. + */ + virtual int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native) =0; };