sourcepawn/jit/sp_vm_basecontext.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 26 23:50:54 2008 -0500 (2008-10-26)
changeset 2377 6ee3bbd6fbdf
parent 2193 db7c6362f3f2
child 2545 9cb1a1fefc2c
permissions -rw-r--r--
Fixed leaking relocation table memory in JIT.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourcePawn
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEPAWN_BASECONTEXT_H_
    33 #define _INCLUDE_SOURCEPAWN_BASECONTEXT_H_
    34 
    35 #include "sp_vm_api.h"
    36 #include "sp_vm_function.h"
    37 #include "BaseRuntime.h"
    38 #include "jit_shared.h"
    39 
    40 /**
    41  * :TODO: Make functions allocate as a lump instead of individual allocations!
    42  */
    43 
    44 class BaseContext : public IPluginContext
    45 {
    46 public:
    47 	BaseContext(BaseRuntime *pRuntime);
    48 	~BaseContext();
    49 public: //IPluginContext
    50 	IVirtualMachine *GetVirtualMachine();
    51 	sp_context_t *GetContext();
    52 	sp_context_t *GetCtx();
    53 	bool IsDebugging();
    54 	int SetDebugBreak(void *newpfn, void *oldpfn);
    55 	IPluginDebugInfo *GetDebugInfo();
    56 	int HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr);
    57 	int HeapPop(cell_t local_addr);
    58 	int HeapRelease(cell_t local_addr);
    59 	int FindNativeByName(const char *name, uint32_t *index);
    60 	int GetNativeByIndex(uint32_t index, sp_native_t **native);
    61 	uint32_t GetNativesNum();
    62 	int FindPublicByName(const char *name, uint32_t *index);
    63 	int GetPublicByIndex(uint32_t index, sp_public_t **publicptr);
    64 	uint32_t GetPublicsNum();
    65 	int GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar);
    66 	int FindPubvarByName(const char *name, uint32_t *index);
    67 	int GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr);
    68 	uint32_t GetPubVarsNum();
    69 	int LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr);
    70 	int LocalToString(cell_t local_addr, char **addr);
    71 	int StringToLocal(cell_t local_addr, size_t chars, const char *source);
    72 	int StringToLocalUTF8(cell_t local_addr, size_t maxbytes, const char *source, size_t *wrtnbytes);
    73 	int PushCell(cell_t value);
    74 	int PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells);
    75 	int PushString(cell_t *local_addr, char **phys_addr, const char *string);
    76 	int PushCellsFromArray(cell_t array[], unsigned int numcells);
    77 	int BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite);
    78 	int BindNative(const sp_nativeinfo_t *native);
    79 	int BindNativeToAny(SPVM_NATIVE_FUNC native);
    80 	int Execute(uint32_t code_addr, cell_t *result);
    81 	cell_t ThrowNativeErrorEx(int error, const char *msg, ...);
    82 	cell_t ThrowNativeError(const char *msg, ...);
    83 	IPluginFunction *GetFunctionByName(const char *public_name);
    84 	IPluginFunction *GetFunctionById(funcid_t func_id);
    85 	SourceMod::IdentityToken_t *GetIdentity();
    86 	cell_t *GetNullRef(SP_NULL_TYPE type);
    87 	int LocalToStringNULL(cell_t local_addr, char **addr);
    88 	int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native);
    89 	int Execute2(IPluginFunction *function, const cell_t *params, unsigned int num_params, cell_t *result);
    90 	IPluginRuntime *GetRuntime();
    91 	int GetLastNativeError();
    92 	cell_t *GetLocalParams();
    93 	void SetKey(int k, void *value);
    94 	bool GetKey(int k, void **value);
    95 	void Refresh();
    96 public:
    97 	bool IsInExec();
    98 private:
    99 	void SetErrorMessage(const char *msg, va_list ap);
   100 	void _SetErrorMessage(const char *msg, ...);
   101 private:
   102 	sp_plugin_t *m_pPlugin;
   103 	cell_t *m_pNullVec;
   104 	cell_t *m_pNullString;
   105 	char m_MsgCache[1024];
   106 	bool m_CustomMsg;
   107 	bool m_InExec;
   108 	BaseRuntime *m_pRuntime;
   109 	sp_context_t m_ctx;
   110 	void *m_keys[4];
   111 	bool m_keys_set[4];
   112 };
   113 
   114 #endif //_INCLUDE_SOURCEPAWN_BASECONTEXT_H_