finished massive reorganization - IPluginFunction is now part of the VM, NOT the plugin system! This is how it should have been in the first place...

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40332
This commit is contained in:
David Anderson
2007-01-19 08:22:44 +00:00
parent a25f2f7be6
commit cd735aec71
16 changed files with 526 additions and 435 deletions
-259
View File
@@ -1,259 +0,0 @@
#include <stdio.h>
#include "PluginSys.h"
/********************
* FUNCTION CALLING *
********************/
void CFunction::Set(funcid_t funcid, CPlugin *plugin)
{
m_funcid = funcid;
m_pPlugin = plugin;
m_curparam = 0;
m_errorstate = SP_ERROR_NONE;
}
int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
{
IPluginContext *ctx = m_pPlugin->m_ctx.base;
while (num_params--)
{
ctx->PushCell(params[num_params]);
}
return ctx->Execute(m_funcid, result);
}
IPlugin *CFunction::GetParentPlugin()
{
return m_pPlugin;
}
CFunction::CFunction(funcid_t funcid, CPlugin *plugin) :
m_funcid(funcid), m_pPlugin(plugin), m_curparam(0),
m_errorstate(SP_ERROR_NONE)
{
}
int CFunction::PushCell(cell_t cell)
{
if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_info[m_curparam].marked = false;
m_params[m_curparam] = cell;
m_curparam++;
return SP_ERROR_NONE;
}
int CFunction::PushCellByRef(cell_t *cell, int flags)
{
if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
return PushArray(cell, 1, NULL, flags);
}
int CFunction::PushFloat(float number)
{
cell_t val = *(cell_t *)&number;
return PushCell(val);
}
int CFunction::PushFloatByRef(float *number, int flags)
{
return PushCellByRef((cell_t *)number, flags);
}
int CFunction::PushArray(cell_t *inarray, unsigned int cells, cell_t **phys_addr, int copyback)
{
if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
IPluginContext *ctx = m_pPlugin->m_ctx.base;
ParamInfo *info = &m_info[m_curparam];
int err;
if ((err=ctx->HeapAlloc(cells, &info->local_addr, &info->phys_addr)) != SP_ERROR_NONE)
{
return SetError(err);
}
info->flags = inarray ? copyback : 0;
info->marked = true;
info->size = cells * sizeof(cell_t);
m_params[m_curparam] = info->local_addr;
m_curparam++;
if (inarray)
{
memcpy(info->phys_addr, inarray, sizeof(cell_t) * cells);
info->orig_addr = inarray;
} else {
info->orig_addr = info->phys_addr;
}
if (phys_addr)
{
*phys_addr = info->phys_addr;
}
return true;
}
int CFunction::PushString(const char *string)
{
return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
}
int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
{
return _PushString(buffer, sz_flags, cp_flags, length);
}
int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
{
if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
IPluginContext *base = m_pPlugin->m_ctx.base;
ParamInfo *info = &m_info[m_curparam];
size_t cells = (len + sizeof(cell_t) - 1) / sizeof(cell_t);
int err;
if ((err=base->HeapAlloc(cells, &info->local_addr, &info->phys_addr)) != SP_ERROR_NONE)
{
return SetError(err);
}
info->marked = true;
m_params[m_curparam] = info->local_addr;
m_curparam++; /* Prevent a leak */
if (!(sz_flags & SM_PARAM_STRING_COPY))
{
goto skip_localtostr;
}
if (sz_flags & SM_PARAM_STRING_UTF8)
{
if ((err=base->StringToLocalUTF8(info->local_addr, len, string, NULL)) != SP_ERROR_NONE)
{
return SetError(err);
}
} else {
if ((err=base->StringToLocal(info->local_addr, len, string)) != SP_ERROR_NONE)
{
return SetError(err);
}
}
skip_localtostr:
info->flags = cp_flags;
info->orig_addr = (cell_t *)string;
info->size = len;
return SP_ERROR_NONE;
}
void CFunction::Cancel()
{
if (!m_curparam)
{
return;
}
IPluginContext *base = m_pPlugin->m_ctx.base;
while (m_curparam--)
{
if (m_info[m_curparam].marked)
{
base->HeapRelease(m_info[m_curparam].local_addr);
m_info[m_curparam].marked = false;
}
}
m_errorstate = SP_ERROR_NONE;
}
int CFunction::Execute(cell_t *result)
{
int err;
if (m_errorstate != SP_ERROR_NONE)
{
err = m_errorstate;
Cancel();
return err;
}
//This is for re-entrancy!
cell_t temp_params[SP_MAX_EXEC_PARAMS];
ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
unsigned int numparams = m_curparam;
bool docopies = true;
if (numparams)
{
//Save the info locally, then reset it for re-entrant calls.
memcpy(temp_params, m_params, numparams * sizeof(cell_t));
memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
}
m_curparam = 0;
if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
{
docopies = false;
}
IPluginContext *base = m_pPlugin->m_ctx.base;
while (numparams--)
{
if (!temp_info[numparams].marked)
{
continue;
}
if (docopies && temp_info[numparams].flags)
{
if (temp_info[numparams].orig_addr)
{
if (temp_info[numparams].size == sizeof(cell_t))
{
*temp_info[numparams].orig_addr = *temp_info[numparams].phys_addr;
} else {
memcpy(temp_info[numparams].orig_addr,
temp_info[numparams].phys_addr,
temp_info[numparams].size);
}
}
}
base->HeapPop(temp_info[numparams].local_addr);
temp_info[numparams].marked = false;
}
return err;
}
cell_t *CFunction::GetAddressOfPushedParam(unsigned int param)
{
if (m_errorstate != SP_ERROR_NONE
|| param >= m_curparam
|| !m_info[param].marked)
{
return NULL;
}
return m_info[param].phys_addr;
}
+7 -10
View File
@@ -71,7 +71,7 @@ void CForwardManager::OnPluginLoaded(IPlugin *plugin)
for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
{
fwd = (*iter);
IPluginFunction *pFunc = plugin->GetFunctionByName(fwd->GetForwardName());
IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
if (pFunc)
{
fwd->AddFunction(pFunc);
@@ -256,7 +256,8 @@ int CForward::Execute(cell_t *result, IForwardFilter *filter)
for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
{
func = (*iter);
if (func->GetParentPlugin()->GetStatus() == Plugin_Paused)
/* Ugh... */
if (!func->GetParentContext()->IsRunnable())
{
continue;
}
@@ -565,15 +566,10 @@ void CForward::Cancel()
m_errstate = SP_ERROR_NONE;
}
bool CForward::AddFunction(sp_context_t *ctx, funcid_t index)
bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
{
IPlugin *pPlugin = g_PluginSys.FindPluginByContext(ctx);
if (!pPlugin)
{
return false;
}
IPluginFunction *pFunc = pContext->GetFunctionById(index);
IPluginFunction *pFunc = pPlugin->GetFunctionById(index);
if (!pFunc)
{
return false;
@@ -614,10 +610,11 @@ unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
FuncIter iter;
IPluginFunction *func;
unsigned int removed = 0;
IPluginContext *pContext = plugin->GetBaseContext();
for (iter=m_functions.begin(); iter!=m_functions.end();)
{
func = (*iter);
if (func->GetParentPlugin() == plugin)
if (func->GetParentContext() == pContext)
{
iter = m_functions.erase(iter);
removed++;
+1 -1
View File
@@ -50,7 +50,7 @@ public: //IChangeableForward
virtual bool RemoveFunction(IPluginFunction *func);
virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
virtual bool AddFunction(IPluginFunction *func);
virtual bool AddFunction(sp_context_t *ctx, funcid_t index);
virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
public:
static CForward *CreateForward(const char *name,
ExecType et,
+8 -134
View File
@@ -23,9 +23,6 @@ CPlugin::CPlugin(const char *file)
m_status = Plugin_Uncompiled;
m_serial = ++MySerial;
m_plugin = NULL;
m_funcsnum = 0;
m_priv_funcs = NULL;
m_pub_funcs = NULL;
m_errormsg[256] = '\0';
snprintf(m_filename, sizeof(m_filename), "%s", file);
m_handle = 0;
@@ -60,26 +57,6 @@ CPlugin::~CPlugin()
m_ctx.co = NULL;
}
if (m_pub_funcs)
{
for (uint32_t i=0; i<m_plugin->info.publics_num; i++)
{
g_PluginSys.ReleaseFunctionToPool(m_pub_funcs[i]);
}
delete [] m_pub_funcs;
m_pub_funcs = NULL;
}
if (m_priv_funcs)
{
for (unsigned int i=0; i<m_funcsnum; i++)
{
g_PluginSys.ReleaseFunctionToPool(m_priv_funcs[i]);
}
delete [] m_priv_funcs;
m_priv_funcs = NULL;
}
if (m_plugin)
{
g_pSourcePawn->FreeFromMemory(m_plugin);
@@ -190,30 +167,9 @@ bool CPlugin::FinishMyCompile(char *error, size_t maxlength)
}
m_ctx.base = new BaseContext(m_ctx.ctx);
m_ctx.base->SetRunnable(false);
m_ctx.ctx->user[SM_CONTEXTVAR_MYSELF] = (void *)this;
m_funcsnum = m_ctx.vm->FunctionCount(m_ctx.ctx);
/**
* Note: Since the m_plugin member will never change,
* it is safe to assume the function count will never change
*/
if (m_funcsnum && m_priv_funcs == NULL)
{
m_priv_funcs = new CFunction *[m_funcsnum];
memset(m_priv_funcs, 0, sizeof(CFunction *) * m_funcsnum);
} else {
m_priv_funcs = NULL;
}
if (m_plugin->info.publics_num && m_pub_funcs == NULL)
{
m_pub_funcs = new CFunction *[m_plugin->info.publics_num];
memset(m_pub_funcs, 0, sizeof(CFunction *) * m_plugin->info.publics_num);
} else {
m_pub_funcs = NULL;
}
m_status = Plugin_Created;
m_ctx.co = NULL;
@@ -230,67 +186,11 @@ void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...)
va_start(ap, error_fmt);
vsnprintf(m_errormsg, sizeof(m_errormsg), error_fmt, ap);
va_end(ap);
}
IPluginFunction *CPlugin::GetFunctionById(funcid_t func_id)
{
CFunction *pFunc = NULL;
funcid_t save = func_id;
if (func_id & 1)
if (m_ctx.base)
{
func_id >>= 1;
if (func_id >= m_plugin->info.publics_num)
{
return NULL;
}
pFunc = m_pub_funcs[func_id];
if (!pFunc)
{
pFunc = g_PluginSys.GetFunctionFromPool(save, this);
m_pub_funcs[func_id] = pFunc;
}
} else {
func_id >>= 1;
unsigned int index;
if (!g_pVM->FunctionLookup(m_ctx.ctx, func_id, &index))
{
return NULL;
}
pFunc = m_priv_funcs[func_id];
if (!pFunc)
{
pFunc = g_PluginSys.GetFunctionFromPool(save, this);
m_priv_funcs[func_id] = pFunc;
}
m_ctx.base->SetRunnable(false);
}
return pFunc;
}
IPluginFunction *CPlugin::GetFunctionByName(const char *public_name)
{
uint32_t index;
IPluginContext *base = m_ctx.base;
if (base->FindPublicByName(public_name, &index) != SP_ERROR_NONE)
{
return NULL;
}
CFunction *pFunc = m_pub_funcs[index];
if (!pFunc)
{
sp_public_t *pub = NULL;
base->GetPublicByIndex(index, &pub);
if (pub)
{
pFunc = g_PluginSys.GetFunctionFromPool(pub->funcid, this);
m_pub_funcs[index] = pFunc;
}
}
return pFunc;
}
void CPlugin::UpdateInfo()
@@ -338,7 +238,7 @@ void CPlugin::Call_OnPluginInit()
m_status = Plugin_Running;
cell_t result;
IPluginFunction *pFunction = GetFunctionByName("OnPluginInit");
IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginInit");
if (!pFunction)
{
return;
@@ -356,7 +256,7 @@ void CPlugin::Call_OnPluginUnload()
}
cell_t result;
IPluginFunction *pFunction = GetFunctionByName("OnPluginUnload");
IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginUnload");
if (!pFunction)
{
return;
@@ -373,10 +273,11 @@ bool CPlugin::Call_AskPluginLoad(char *error, size_t maxlength)
}
m_status = Plugin_Loaded;
m_ctx.base->SetRunnable(true);
int err;
cell_t result;
IPluginFunction *pFunction = GetFunctionByName("AskPluginLoad");
IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("AskPluginLoad");
if (!pFunction)
{
@@ -389,13 +290,11 @@ bool CPlugin::Call_AskPluginLoad(char *error, size_t maxlength)
pFunction->PushCell(maxlength);
if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE)
{
m_status = Plugin_Failed;
return false;
}
if (!result || m_status != Plugin_Loaded)
{
m_status = Plugin_Failed;
return false;
}
@@ -463,7 +362,7 @@ bool CPlugin::SetPauseState(bool paused)
m_status = (paused) ? Plugin_Paused : Plugin_Running;
IPluginFunction *pFunction = GetFunctionByName("OnPluginPauseChange");
IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginPauseChange");
if (pFunction)
{
cell_t result;
@@ -756,7 +655,6 @@ bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug
snprintf(error, err_max, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val);
}
pPlugin->CancelMyCompile();
pPlugin->m_status = Plugin_Failed;
co = NULL;
break;
}
@@ -932,7 +830,6 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
{
snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file);
}
pPlugin->m_status = Plugin_Failed;
return false;
} else {
g_Extensions.BindChildPlugin(pExt, pPlugin);
@@ -1091,29 +988,6 @@ void CPluginManager::ReleaseIterator(CPluginIterator *iter)
m_iters.push(iter);
}
void CPluginManager::ReleaseFunctionToPool(CFunction *func)
{
if (!func)
{
return;
}
func->Cancel();
m_funcpool.push(func);
}
CFunction *CPluginManager::GetFunctionFromPool(funcid_t f, CPlugin *plugin)
{
if (m_funcpool.empty())
{
return new CFunction(f, plugin);
} else {
CFunction *func = m_funcpool.front();
m_funcpool.pop();
func->Set(f, plugin);
return func;
}
}
bool CPluginManager::TestAliasMatch(const char *alias, const char *localpath)
{
/* As an optimization, we do not call strlen, but compute the length in the first pass */
-9
View File
@@ -8,7 +8,6 @@
#include <sh_stack.h>
#include "sm_globals.h"
#include "vm/sp_vm_basecontext.h"
#include "CFunction.h"
#include "PluginInfoDatabase.h"
#include "sm_trie.h"
#include "sourcemod.h"
@@ -93,8 +92,6 @@ public:
virtual bool SetPauseState(bool paused);
virtual unsigned int GetSerial() const;
virtual const sp_plugin_t *GetPluginStructure() const;
virtual IPluginFunction *GetFunctionByName(const char *public_name);
virtual IPluginFunction *GetFunctionById(funcid_t func_id);
virtual IdentityToken_t *GetIdentity() const;
public:
/**
@@ -174,9 +171,6 @@ private:
unsigned int m_serial;
sm_plugininfo_t m_info;
sp_plugin_t *m_plugin;
unsigned int m_funcsnum;
CFunction **m_priv_funcs;
CFunction **m_pub_funcs;
char m_errormsg[256];
time_t m_LastAccess;
IdentityToken_t *m_ident;
@@ -312,8 +306,6 @@ protected:
* Caching internal objects
*/
void ReleaseIterator(CPluginIterator *iter);
CFunction *GetFunctionFromPool(funcid_t f, CPlugin *plugin);
void ReleaseFunctionToPool(CFunction *func);
inline IdentityToken_t *GetIdentity()
{
return m_MyIdent;
@@ -323,7 +315,6 @@ private:
List<CPlugin *> m_plugins;
List<sp_nativeinfo_t *> m_natives;
CStack<CPluginManager::CPluginIterator *> m_iters;
CStack<CFunction *> m_funcpool;
CPluginInfoDatabase m_PluginInfo;
Trie *m_LoadLookup;
bool m_AllPluginsLoaded;