extensions/bintools/CallWrapper.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1226 478550a4d5ec
child 1894 e6efc8ffa47a
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
faluco@739
     1
/**
faluco@739
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
ds@757
     4
 * SourceMod BinTools Extension
dvander@1226
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
faluco@739
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1226
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1226
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1226
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
faluco@739
    28
 *
faluco@739
    29
 * Version: $Id$
faluco@739
    30
 */
faluco@739
    31
faluco@739
    32
#include "extension.h"
faluco@739
    33
#include "CallWrapper.h"
faluco@739
    34
faluco@739
    35
CallWrapper::CallWrapper(CallConvention cv, const PassInfo *paramInfo, const PassInfo *retInfo, unsigned int numParams)
faluco@739
    36
{
faluco@739
    37
	m_Cv = cv;
faluco@739
    38
faluco@739
    39
	if (numParams)
faluco@739
    40
	{
faluco@739
    41
		m_Params = new PassEncode[numParams];
faluco@739
    42
		for (size_t i=0; i<numParams; i++)
faluco@739
    43
		{
faluco@739
    44
			m_Params[i].info = paramInfo[i];
faluco@739
    45
		}
dvander@948
    46
	} else {
dvander@948
    47
		m_Params = NULL;
faluco@739
    48
	}
faluco@739
    49
faluco@739
    50
	if (retInfo)
faluco@739
    51
	{
faluco@739
    52
		m_RetParam = new PassInfo;
faluco@739
    53
		*m_RetParam = *retInfo;
faluco@739
    54
	} else {
faluco@739
    55
		m_RetParam = NULL;
faluco@739
    56
	}
faluco@739
    57
faluco@739
    58
	m_NumParams = numParams;
faluco@739
    59
faluco@739
    60
	/* Calculate virtual stack offsets for each parameter */
faluco@739
    61
	size_t offs = 0;
faluco@739
    62
faluco@739
    63
	if (cv == CallConv_ThisCall)
faluco@739
    64
	{
faluco@739
    65
		offs += sizeof(void *);
faluco@739
    66
	}
faluco@739
    67
	for (size_t i=0; i<numParams; i++)
faluco@739
    68
	{
faluco@739
    69
		m_Params[i].offset = offs;
faluco@739
    70
		offs += m_Params[i].info.size;
faluco@739
    71
	}
faluco@739
    72
}
faluco@739
    73
faluco@739
    74
CallWrapper::~CallWrapper()
faluco@739
    75
{
faluco@739
    76
	delete [] m_Params;
faluco@739
    77
	delete m_RetParam;
faluco@739
    78
	g_SPEngine->ExecFree(m_Addrs[ADDR_CODEBASE]);
faluco@739
    79
}
faluco@739
    80
dvander@942
    81
void CallWrapper::Destroy()
dvander@942
    82
{
dvander@942
    83
	delete this;
dvander@942
    84
}
dvander@942
    85
faluco@739
    86
CallConvention CallWrapper::GetCallConvention()
faluco@739
    87
{
faluco@739
    88
	return m_Cv;
faluco@739
    89
}
faluco@739
    90
faluco@739
    91
const PassEncode *CallWrapper::GetParamInfo(unsigned int num)
faluco@739
    92
{
faluco@739
    93
	return (num+1 > m_NumParams) ? NULL : &m_Params[num];
faluco@739
    94
}
faluco@739
    95
faluco@739
    96
const PassInfo *CallWrapper::GetReturnInfo()
faluco@739
    97
{
faluco@739
    98
	return m_RetParam;
faluco@739
    99
}
faluco@739
   100
faluco@739
   101
unsigned int CallWrapper::GetParamCount()
faluco@739
   102
{
faluco@739
   103
	return m_NumParams;
faluco@739
   104
}
faluco@739
   105
faluco@739
   106
void CallWrapper::Execute(void *vParamStack, void *retBuffer)
faluco@739
   107
{
faluco@739
   108
	typedef void (*CALL_EXECUTE)(void *, void *);
faluco@739
   109
	CALL_EXECUTE fn = (CALL_EXECUTE)m_Addrs[ADDR_CODEBASE];
faluco@739
   110
	fn(vParamStack, retBuffer);
faluco@739
   111
}