Modernize sp_vm_engine style.

This commit is contained in:
dvander@alliedmods.net 2015-02-23 13:08:28 -08:00
parent bcd88b4437
commit 1b47aa10ca
2 changed files with 246 additions and 283 deletions

View File

@ -1,34 +1,15 @@
/**
* vim: set ts=4 sw=4 tw=99 et:
* =============================================================================
* SourcePawn
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
// vim: set sts=2 ts=8 sw=2 tw=99 et:
//
// Copyright (C) 2006-2015 AlliedModders LLC
//
// This file is part of SourcePawn. SourcePawn is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// You should have received a copy of the GNU General Public License along with
// SourcePawn. If not, see http://www.gnu.org/licenses/.
//
#include <stdlib.h>
#include <string.h>
#include <assert.h>
@ -93,13 +74,11 @@ static const char *g_ErrorMsgTable[] =
"Script execution timed out"
};
const char *SourcePawnEngine::GetErrorString(int error)
const char *
SourcePawnEngine::GetErrorString(int error)
{
if (error < 1 || error > ERROR_MESSAGE_MAX)
{
return NULL;
}
return g_ErrorMsgTable[error];
}
@ -112,7 +91,8 @@ SourcePawnEngine::~SourcePawnEngine()
{
}
void *SourcePawnEngine::ExecAlloc(size_t size)
void *
SourcePawnEngine::ExecAlloc(size_t size)
{
#if defined WIN32
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
@ -122,8 +102,7 @@ void *SourcePawnEngine::ExecAlloc(size_t size)
# else
void *base = memalign(sysconf(_SC_PAGESIZE), size);
# endif
if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
{
if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
free(base);
return NULL;
}
@ -131,27 +110,32 @@ void *SourcePawnEngine::ExecAlloc(size_t size)
#endif
}
void *SourcePawnEngine::AllocatePageMemory(size_t size)
void *
SourcePawnEngine::AllocatePageMemory(size_t size)
{
return g_Jit.AllocCode(size);
}
void SourcePawnEngine::SetReadExecute(void *ptr)
void
SourcePawnEngine::SetReadExecute(void *ptr)
{
/* already re */
}
void SourcePawnEngine::SetReadWrite(void *ptr)
void
SourcePawnEngine::SetReadWrite(void *ptr)
{
/* already rw */
}
void SourcePawnEngine::FreePageMemory(void *ptr)
void
SourcePawnEngine::FreePageMemory(void *ptr)
{
g_Jit.FreeCode(ptr);
}
void SourcePawnEngine::ExecFree(void *address)
void
SourcePawnEngine::ExecFree(void *address)
{
#if defined WIN32
VirtualFree(address, 0, MEM_RELEASE);
@ -160,48 +144,51 @@ void SourcePawnEngine::ExecFree(void *address)
#endif
}
void SourcePawnEngine::SetReadWriteExecute(void *ptr)
void
SourcePawnEngine::SetReadWriteExecute(void *ptr)
{
//:TODO: g_ExeMemory.SetRWE(ptr);
SetReadExecute(ptr);
}
void *SourcePawnEngine::BaseAlloc(size_t size)
void *
SourcePawnEngine::BaseAlloc(size_t size)
{
return malloc(size);
}
void SourcePawnEngine::BaseFree(void *memory)
void
SourcePawnEngine::BaseFree(void *memory)
{
free(memory);
}
sp_plugin_t *SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err)
sp_plugin_t *
SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err)
{
if (err != NULL)
{
*err = SP_ERROR_ABORTED;
}
return NULL;
}
sp_plugin_t *SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err)
sp_plugin_t *
SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err)
{
if (err != NULL)
{
*err = SP_ERROR_ABORTED;
}
return NULL;
}
int SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin)
int
SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin)
{
return SP_ERROR_ABORTED;
}
IDebugListener *SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
IDebugListener *
SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
{
IDebugListener *old = m_pDebugHook;
@ -210,22 +197,23 @@ IDebugListener *SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
return old;
}
unsigned int SourcePawnEngine::GetEngineAPIVersion()
unsigned int
SourcePawnEngine::GetEngineAPIVersion()
{
return SOURCEPAWN_ENGINE_API_VERSION;
}
unsigned int SourcePawnEngine::GetContextCallCount()
unsigned int
SourcePawnEngine::GetContextCallCount()
{
return 0;
}
void SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start)
void
SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start)
{
if (m_pDebugHook == NULL)
{
return;
}
CContextTrace trace(runtime, err, errstr, rp_start);
@ -233,54 +221,56 @@ void SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *er
}
CContextTrace::CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp)
: m_pRuntime(pRuntime), m_Error(err), m_pMsg(errstr), m_StartRp(start_rp), m_Level(0)
: m_pRuntime(pRuntime),
m_Error(err),
m_pMsg(errstr),
m_StartRp(start_rp),
m_Level(0)
{
m_ctx = pRuntime->m_pCtx->GetCtx();
m_pDebug = m_pRuntime->GetDebugInfo();
}
bool CContextTrace::DebugInfoAvailable()
bool
CContextTrace::DebugInfoAvailable()
{
return (m_pDebug != NULL);
}
const char *CContextTrace::GetCustomErrorString()
const char *
CContextTrace::GetCustomErrorString()
{
return m_pMsg;
}
int CContextTrace::GetErrorCode()
int
CContextTrace::GetErrorCode()
{
return m_Error;
}
const char *CContextTrace::GetErrorString()
const char *
CContextTrace::GetErrorString()
{
if (m_Error > ERROR_MESSAGE_MAX || m_Error < 1)
{
return "Invalid error code";
}
else
{
return g_ErrorMsgTable[m_Error];
}
}
void CContextTrace::ResetTrace()
void
CContextTrace::ResetTrace()
{
m_Level = 0;
}
bool CContextTrace::GetTraceInfo(CallStackInfo *trace)
bool
CContextTrace::GetTraceInfo(CallStackInfo *trace)
{
cell_t cip;
if (m_Level == 0)
{
if (m_Level == 0) {
cip = m_ctx->cip;
}
else if (m_ctx->rp > 0)
{
} else if (m_ctx->rp > 0) {
/* Entries go from ctx.rp - 1 to m_StartRp */
cell_t offs, start, end;
@ -294,60 +284,47 @@ bool CContextTrace::GetTraceInfo(CallStackInfo *trace)
}
cip = m_ctx->rstk_cips[start - offs];
}
else
{
} else {
return false;
}
if (trace == NULL)
{
if (trace == NULL) {
m_Level++;
return true;
}
if (m_pDebug->LookupFile(cip, &(trace->filename)) != SP_ERROR_NONE)
{
trace->filename = NULL;
}
if (m_pDebug->LookupFunction(cip, &(trace->function)) != SP_ERROR_NONE)
{
trace->function = NULL;
}
if (m_pDebug->LookupLine(cip, &(trace->line)) != SP_ERROR_NONE)
{
trace->line = 0;
}
m_Level++;
return true;
}
const char *CContextTrace::GetLastNative(uint32_t *index)
const char *
CContextTrace::GetLastNative(uint32_t *index)
{
if (m_ctx->n_err == SP_ERROR_NONE)
{
return NULL;
}
sp_native_t *native;
if (m_pRuntime->GetNativeByIndex(m_ctx->n_idx, &native) != SP_ERROR_NONE)
{
return NULL;
}
if (index)
{
*index = m_ctx->n_idx;
}
return native->name;
}
IDebugListener *SourcePawnEngine::GetDebugHook()
IDebugListener *
SourcePawnEngine::GetDebugHook()
{
return m_pDebugHook;
}

View File

@ -1,34 +1,15 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourcePawn
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
// vim: set sts=2 ts=8 sw=2 tw=99 et:
//
// Copyright (C) 2006-2015 AlliedModders LLC
//
// This file is part of SourcePawn. SourcePawn is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// You should have received a copy of the GNU General Public License along with
// SourcePawn. If not, see http://www.gnu.org/licenses/.
//
#ifndef _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
#define _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
@ -41,6 +22,7 @@ class CContextTrace : public IContextTrace
{
public:
CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp);
public:
int GetErrorCode();
const char *GetErrorString();
@ -49,6 +31,7 @@ public:
bool GetTraceInfo(CallStackInfo *trace);
void ResetTrace();
const char *GetLastNative(uint32_t *index);
private:
BaseRuntime *m_pRuntime;
sp_context_t *m_ctx;
@ -64,6 +47,7 @@ class SourcePawnEngine : public ISourcePawnEngine
public:
SourcePawnEngine();
~SourcePawnEngine();
public: //ISourcePawnEngine
sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err);
sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err);
@ -82,10 +66,12 @@ public: //ISourcePawnEngine
void FreePageMemory(void *ptr);
const char *GetErrorString(int err);
void ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start);
public: //Plugin function stuff
CFunction *GetFunctionFromPool(funcid_t f, IPluginContext *plugin);
void ReleaseFunctionToPool(CFunction *func);
IDebugListener *GetDebugHook();
private:
IDebugListener *m_pDebugHook;
};