Don't use std::list at all in DynamicHooks

Switch to amtl Vector.
This commit is contained in:
Peace-Maker 2016-12-13 17:37:12 -07:00
parent c70d3b9ee0
commit 6e96e0fb84
13 changed files with 81 additions and 81 deletions

View File

@ -34,11 +34,8 @@
// ============================================================================ // ============================================================================
// >> INCLUDES // >> INCLUDES
// ============================================================================ // ============================================================================
#include <list>
#include <vector>
#include <stdio.h>
#include "registers.h" #include "registers.h"
#include <am-vector.h>
// ============================================================================ // ============================================================================
// >> DataType_t // >> DataType_t
@ -150,13 +147,13 @@ public:
@param <returnType>: @param <returnType>:
The return type of the function. The return type of the function.
*/ */
ICallingConvention(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment=4) ICallingConvention(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4)
{ {
m_vecArgTypes = vecArgTypes; m_vecArgTypes = ke::Move(vecArgTypes);
std::vector<DataTypeSized_t>::iterator it = m_vecArgTypes.begin();
for (; it != m_vecArgTypes.end(); it++) for (size_t i=0; i < m_vecArgTypes.length(); i++)
{ {
DataTypeSized_t &type = *it; DataTypeSized_t &type = m_vecArgTypes[i];
if (!type.size) if (!type.size)
type.size = GetDataTypeSize(type); type.size = GetDataTypeSize(type);
} }
@ -170,7 +167,7 @@ public:
This should return a list of Register_t values. These registers will be This should return a list of Register_t values. These registers will be
saved for later access. saved for later access.
*/ */
virtual std::list<Register_t> GetRegisters() = 0; virtual ke::Vector<Register_t> GetRegisters() = 0;
/* /*
Returns the number of bytes that should be added to the stack to clean up. Returns the number of bytes that should be added to the stack to clean up.
@ -208,7 +205,7 @@ public:
virtual void ReturnPtrChanged(CRegisters* pRegisters, void* pReturnPtr) = 0; virtual void ReturnPtrChanged(CRegisters* pRegisters, void* pReturnPtr) = 0;
public: public:
std::vector<DataTypeSized_t> m_vecArgTypes; ke::Vector<DataTypeSized_t> m_vecArgTypes;
DataTypeSized_t m_returnType; DataTypeSized_t m_returnType;
int m_iAlignment; int m_iAlignment;
}; };

View File

@ -38,7 +38,7 @@
// ============================================================================ // ============================================================================
// >> x86MsCdecl // >> x86MsCdecl
// ============================================================================ // ============================================================================
x86MsCdecl::x86MsCdecl(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment) : x86MsCdecl::x86MsCdecl(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment) :
ICallingConvention(vecArgTypes, returnType, iAlignment) ICallingConvention(vecArgTypes, returnType, iAlignment)
{ {
if (m_returnType.size > 4) if (m_returnType.size > 4)
@ -59,22 +59,22 @@ x86MsCdecl::~x86MsCdecl()
} }
} }
std::list<Register_t> x86MsCdecl::GetRegisters() ke::Vector<Register_t> x86MsCdecl::GetRegisters()
{ {
std::list<Register_t> registers; ke::Vector<Register_t> registers;
registers.push_back(ESP); registers.append(ESP);
if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE)
{ {
registers.push_back(ST0); registers.append(ST0);
} }
else else
{ {
registers.push_back(EAX); registers.append(EAX);
if (m_pReturnBuffer) if (m_pReturnBuffer)
{ {
registers.push_back(EDX); registers.append(EDX);
} }
} }
@ -90,7 +90,7 @@ int x86MsCdecl::GetArgStackSize()
{ {
int iArgStackSize = 0; int iArgStackSize = 0;
for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) for (unsigned int i = 0; i < m_vecArgTypes.length(); i++)
{ {
iArgStackSize += m_vecArgTypes[i].size; iArgStackSize += m_vecArgTypes[i].size;
} }

View File

@ -63,10 +63,10 @@ Return values:
class x86MsCdecl: public ICallingConvention class x86MsCdecl: public ICallingConvention
{ {
public: public:
x86MsCdecl(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); x86MsCdecl(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4);
~x86MsCdecl(); ~x86MsCdecl();
virtual std::list<Register_t> GetRegisters(); virtual ke::Vector<Register_t> GetRegisters();
virtual int GetPopSize(); virtual int GetPopSize();
virtual int GetArgStackSize(); virtual int GetArgStackSize();
virtual void** GetStackArgumentPtr(CRegisters* pRegisters); virtual void** GetStackArgumentPtr(CRegisters* pRegisters);

View File

@ -38,7 +38,7 @@
// ============================================================================ // ============================================================================
// >> x86MsStdcall // >> x86MsStdcall
// ============================================================================ // ============================================================================
x86MsStdcall::x86MsStdcall(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment) : x86MsStdcall::x86MsStdcall(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment) :
ICallingConvention(vecArgTypes, returnType, iAlignment) ICallingConvention(vecArgTypes, returnType, iAlignment)
{ {
if (m_returnType.size > 4) if (m_returnType.size > 4)
@ -59,22 +59,22 @@ x86MsStdcall::~x86MsStdcall()
} }
} }
std::list<Register_t> x86MsStdcall::GetRegisters() ke::Vector<Register_t> x86MsStdcall::GetRegisters()
{ {
std::list<Register_t> registers; ke::Vector<Register_t> registers;
registers.push_back(ESP); registers.append(ESP);
if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE)
{ {
registers.push_back(ST0); registers.append(ST0);
} }
else else
{ {
registers.push_back(EAX); registers.append(EAX);
if (m_pReturnBuffer) if (m_pReturnBuffer)
{ {
registers.push_back(EDX); registers.append(EDX);
} }
} }
@ -85,7 +85,7 @@ int x86MsStdcall::GetPopSize()
{ {
int iPopSize = 0; int iPopSize = 0;
for(unsigned int i=0; i < m_vecArgTypes.size(); i++) for(unsigned int i=0; i < m_vecArgTypes.length(); i++)
{ {
iPopSize += m_vecArgTypes[i].size; iPopSize += m_vecArgTypes[i].size;
} }
@ -97,7 +97,7 @@ int x86MsStdcall::GetArgStackSize()
{ {
int iArgStackSize = 0; int iArgStackSize = 0;
for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) for (unsigned int i = 0; i < m_vecArgTypes.length(); i++)
{ {
iArgStackSize += m_vecArgTypes[i].size; iArgStackSize += m_vecArgTypes[i].size;
} }

View File

@ -63,10 +63,10 @@ Return values:
class x86MsStdcall: public ICallingConvention class x86MsStdcall: public ICallingConvention
{ {
public: public:
x86MsStdcall(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); x86MsStdcall(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4);
~x86MsStdcall(); ~x86MsStdcall();
virtual std::list<Register_t> GetRegisters(); virtual ke::Vector<Register_t> GetRegisters();
virtual int GetPopSize(); virtual int GetPopSize();
virtual int GetArgStackSize(); virtual int GetArgStackSize();
virtual void** GetStackArgumentPtr(CRegisters* pRegisters); virtual void** GetStackArgumentPtr(CRegisters* pRegisters);

View File

@ -38,7 +38,7 @@
// ============================================================================ // ============================================================================
// >> x86MsThiscall // >> x86MsThiscall
// ============================================================================ // ============================================================================
x86MsThiscall::x86MsThiscall(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment) : x86MsThiscall::x86MsThiscall(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment) :
ICallingConvention(vecArgTypes, returnType, iAlignment) ICallingConvention(vecArgTypes, returnType, iAlignment)
{ {
if (m_returnType.size > 4) if (m_returnType.size > 4)
@ -59,23 +59,23 @@ x86MsThiscall::~x86MsThiscall()
} }
} }
std::list<Register_t> x86MsThiscall::GetRegisters() ke::Vector<Register_t> x86MsThiscall::GetRegisters()
{ {
std::list<Register_t> registers; ke::Vector<Register_t> registers;
registers.push_back(ESP); registers.append(ESP);
registers.push_back(ECX); registers.append(ECX);
if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE)
{ {
registers.push_back(ST0); registers.append(ST0);
} }
else else
{ {
registers.push_back(EAX); registers.append(EAX);
if (m_pReturnBuffer) if (m_pReturnBuffer)
{ {
registers.push_back(EDX); registers.append(EDX);
} }
} }
@ -89,7 +89,7 @@ int x86MsThiscall::GetPopSize()
//int iPopSize = GetDataTypeSize(DATA_TYPE_POINTER, m_iAlignment); //int iPopSize = GetDataTypeSize(DATA_TYPE_POINTER, m_iAlignment);
int iPopSize = 0; int iPopSize = 0;
for(unsigned int i=0; i < m_vecArgTypes.size(); i++) for(unsigned int i=0; i < m_vecArgTypes.length(); i++)
{ {
iPopSize += m_vecArgTypes[i].size; iPopSize += m_vecArgTypes[i].size;
} }
@ -101,7 +101,7 @@ int x86MsThiscall::GetArgStackSize()
{ {
int iArgStackSize = 0; int iArgStackSize = 0;
for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) for (unsigned int i = 0; i < m_vecArgTypes.length(); i++)
{ {
iArgStackSize += m_vecArgTypes[i].size; iArgStackSize += m_vecArgTypes[i].size;
} }

View File

@ -64,10 +64,10 @@ Return values:
class x86MsThiscall: public ICallingConvention class x86MsThiscall: public ICallingConvention
{ {
public: public:
x86MsThiscall(std::vector<DataTypeSized_t> vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); x86MsThiscall(ke::Vector<DataTypeSized_t> &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4);
~x86MsThiscall(); ~x86MsThiscall();
virtual std::list<Register_t> GetRegisters(); virtual ke::Vector<Register_t> GetRegisters();
virtual int GetPopSize(); virtual int GetPopSize();
virtual int x86MsThiscall::GetArgStackSize(); virtual int x86MsThiscall::GetArgStackSize();
virtual void** GetStackArgumentPtr(CRegisters* pRegisters); virtual void** GetStackArgumentPtr(CRegisters* pRegisters);

View File

@ -316,10 +316,10 @@ void CHook::Write_CallHandler(sp::MacroAssembler& masm, HookType_t type)
void CHook::Write_SaveRegisters(sp::MacroAssembler& masm) void CHook::Write_SaveRegisters(sp::MacroAssembler& masm)
{ {
std::list<Register_t> vecRegistersToSave = m_pCallingConvention->GetRegisters(); ke::Vector<Register_t> vecRegistersToSave = m_pCallingConvention->GetRegisters();
for(std::list<Register_t>::iterator it=vecRegistersToSave.begin(); it != vecRegistersToSave.end(); it++) for(size_t i = 0; i < vecRegistersToSave.length(); i++)
{ {
switch(*it) switch(vecRegistersToSave[i])
{ {
// ======================================================================== // ========================================================================
// >> 8-bit General purpose registers // >> 8-bit General purpose registers
@ -378,10 +378,10 @@ void CHook::Write_SaveRegisters(sp::MacroAssembler& masm)
void CHook::Write_RestoreRegisters(sp::MacroAssembler& masm) void CHook::Write_RestoreRegisters(sp::MacroAssembler& masm)
{ {
std::list<Register_t> vecRegistersToSave = m_pCallingConvention->GetRegisters(); ke::Vector<Register_t> vecRegistersToSave = m_pCallingConvention->GetRegisters();
for(std::list<Register_t>::iterator it=vecRegistersToSave.begin(); it != vecRegistersToSave.end(); it++) for (size_t i = 0; i < vecRegistersToSave.length(); i++)
{ {
switch(*it) switch (vecRegistersToSave[i])
{ {
// ======================================================================== // ========================================================================
// >> 8-bit General purpose registers // >> 8-bit General purpose registers

View File

@ -50,17 +50,24 @@ CHook* CHookManager::HookFunction(void* pFunc, ICallingConvention* pConvention)
} }
pHook = new CHook(pFunc, pConvention); pHook = new CHook(pFunc, pConvention);
m_Hooks.push_back(pHook); m_Hooks.append(pHook);
return pHook; return pHook;
} }
void CHookManager::UnhookFunction(void* pFunc) void CHookManager::UnhookFunction(void* pFunc)
{ {
CHook* pHook = FindHook(pFunc); if (!pFunc)
if (pHook) return;
for (size_t i = 0; i < m_Hooks.length(); i++)
{ {
m_Hooks.remove(pHook); CHook* pHook = m_Hooks[i];
if (pHook->m_pFunc == pFunc)
{
m_Hooks.remove(i);
delete pHook; delete pHook;
return;
}
} }
} }
@ -69,9 +76,9 @@ CHook* CHookManager::FindHook(void* pFunc)
if (!pFunc) if (!pFunc)
return NULL; return NULL;
for(std::list<CHook *>::iterator it=m_Hooks.begin(); it != m_Hooks.end(); it++) for(size_t i = 0; i < m_Hooks.length(); i++)
{ {
CHook* pHook = *it; CHook* pHook = m_Hooks[i];
if (pHook->m_pFunc == pFunc) if (pHook->m_pFunc == pFunc)
return pHook; return pHook;
} }
@ -80,8 +87,8 @@ CHook* CHookManager::FindHook(void* pFunc)
void CHookManager::UnhookAllFunctions() void CHookManager::UnhookAllFunctions()
{ {
for(std::list<CHook *>::iterator it=m_Hooks.begin(); it != m_Hooks.end(); it++) for(size_t i = 0; i < m_Hooks.length(); i++)
delete *it; delete m_Hooks[i];
m_Hooks.clear(); m_Hooks.clear();
} }

View File

@ -34,9 +34,9 @@
// ============================================================================ // ============================================================================
// >> INCLUDES // >> INCLUDES
// ============================================================================ // ============================================================================
#include <list>
#include "hook.h" #include "hook.h"
#include "convention.h" #include "convention.h"
#include <am-vector.h>
// ============================================================================ // ============================================================================
@ -68,7 +68,7 @@ public:
void UnhookAllFunctions(); void UnhookAllFunctions();
public: public:
std::list<CHook *> m_Hooks; ke::Vector<CHook *> m_Hooks;
}; };

View File

@ -30,7 +30,7 @@
#include "registers.h" #include "registers.h"
CRegisters::CRegisters(std::list<Register_t> registers) CRegisters::CRegisters(ke::Vector<Register_t> registers)
{ {
// ======================================================================== // ========================================================================
// >> 8-bit General purpose registers // >> 8-bit General purpose registers
@ -360,11 +360,11 @@ CRegisters::~CRegisters()
DeleteRegister(m_st7); DeleteRegister(m_st7);
} }
CRegister* CRegisters::CreateRegister(std::list<Register_t>& registers, Register_t reg, int iSize) CRegister* CRegisters::CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, int iSize)
{ {
for(std::list<Register_t>::iterator it=registers.begin(); it != registers.end(); it++) for(size_t i = 0; i < registers.length(); i++)
{ {
if ((*it) == reg) if (registers[i] == reg)
{ {
return new CRegister(iSize); return new CRegister(iSize);
} }

View File

@ -35,8 +35,7 @@
// ============================================================================ // ============================================================================
// >> INCLUDES // >> INCLUDES
// ============================================================================ // ============================================================================
#include <stdlib.h> #include <am-vector.h>
#include <list>
// ============================================================================ // ============================================================================
@ -261,11 +260,11 @@ public:
class CRegisters class CRegisters
{ {
public: public:
CRegisters(std::list<Register_t> registers); CRegisters(ke::Vector<Register_t> registers);
~CRegisters(); ~CRegisters();
private: private:
CRegister* CreateRegister(std::list<Register_t>& registers, Register_t reg, int iSize); CRegister* CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, int iSize);
void DeleteRegister(CRegister* pRegister); void DeleteRegister(CRegister* pRegister);
public: public:

View File

@ -182,14 +182,14 @@ void CleanupDetours()
ICallingConvention *ConstructCallingConvention(HookSetup *setup) ICallingConvention *ConstructCallingConvention(HookSetup *setup)
{ {
std::vector<DataTypeSized_t> vecArgTypes; ke::Vector<DataTypeSized_t> vecArgTypes;
for (size_t i = 0; i < setup->params.size(); i++) for (size_t i = 0; i < setup->params.size(); i++)
{ {
ParamInfo &info = setup->params[i]; ParamInfo &info = setup->params[i];
DataTypeSized_t type; DataTypeSized_t type;
type.type = DynamicHooks_ConvertParamTypeFrom(info.type); type.type = DynamicHooks_ConvertParamTypeFrom(info.type);
type.size = info.size; type.size = info.size;
vecArgTypes.push_back(type); vecArgTypes.append(type);
} }
DataTypeSized_t returnType; DataTypeSized_t returnType;
@ -235,7 +235,7 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour)
HookParamsStruct *paramStruct = NULL; HookParamsStruct *paramStruct = NULL;
Handle_t pHndl = BAD_HANDLE; Handle_t pHndl = BAD_HANDLE;
int argNum = pDetour->m_pCallingConvention->m_vecArgTypes.size(); int argNum = pDetour->m_pCallingConvention->m_vecArgTypes.length();
MRESReturn finalRet = MRES_Ignored; MRESReturn finalRet = MRES_Ignored;
ke::AutoPtr<void> finalRetBuf(new uint8_t[pDetour->m_pCallingConvention->m_returnType.size]); ke::AutoPtr<void> finalRetBuf(new uint8_t[pDetour->m_pCallingConvention->m_returnType.size]);
@ -338,8 +338,6 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour)
break; break;
} }
} }
// TODO: Introduce that override concept in dyndetours.
// This doesn't call the original function at the moment, but just returns the given return value.
tempRet = MRES_Override; tempRet = MRES_Override;
pWrapper->UpdateParamsFromStruct(paramStruct); pWrapper->UpdateParamsFromStruct(paramStruct);
break; break;
@ -348,8 +346,6 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour)
{ {
if (returnStruct->isChanged) if (returnStruct->isChanged)
{ {
// TODO: Introduce that override concept in dyndetours.
// This doesn't call the original function at the moment, but just returns the given return value.
tempRet = MRES_Override; tempRet = MRES_Override;
if (pWrapper->returnType == ReturnType_String || pWrapper->returnType == ReturnType_Int || pWrapper->returnType == ReturnType_Bool) if (pWrapper->returnType == ReturnType_String || pWrapper->returnType == ReturnType_Int || pWrapper->returnType == ReturnType_Bool)
{ {
@ -429,6 +425,7 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour)
} }
} }
// If we want to use our own return value, write it back.
if (finalRet >= MRES_Override) if (finalRet >= MRES_Override)
{ {
void* pPtr = pDetour->m_pCallingConvention->GetReturnPtr(pDetour->m_pRegisters); void* pPtr = pDetour->m_pCallingConvention->GetReturnPtr(pDetour->m_pRegisters);
@ -540,8 +537,8 @@ HookParamsStruct *CDynamicHooksSourcePawn::GetParamStruct()
params->dg = this; params->dg = this;
size_t paramsSize = this->m_pDetour->m_pCallingConvention->GetArgStackSize(); size_t paramsSize = this->m_pDetour->m_pCallingConvention->GetArgStackSize();
std::vector<DataTypeSized_t> &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; ke::Vector<DataTypeSized_t> &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes;
int numArgs = argTypes.size(); int numArgs = argTypes.length();
params->orgParams = (void **)malloc(paramsSize); params->orgParams = (void **)malloc(paramsSize);
params->newParams = (void **)malloc(paramsSize); params->newParams = (void **)malloc(paramsSize);
@ -568,8 +565,8 @@ void CDynamicHooksSourcePawn::UpdateParamsFromStruct(HookParamsStruct *params)
if (!params) if (!params)
return; return;
std::vector<DataTypeSized_t> &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; ke::Vector<DataTypeSized_t> &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes;
int numArgs = argTypes.size(); int numArgs = argTypes.length();
int firstArg = 0; int firstArg = 0;
if (callConv == CallConv_THISCALL) if (callConv == CallConv_THISCALL)