sourcepawn/jit/engine2.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 28 16:59:41 2008 +0000 (2008-08-28)
changeset 2242 cb53a313c38e
parent 2226 d10b38524be3
child 2376 91efbb63ad5b
permissions -rw-r--r--
Fixed plugins not being unloadable from files not being closed (amb 1875).
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include "engine2.h"
     5 #include "x86/jit_x86.h"
     6 #include "jit_version.h"
     7 #include "zlib/zlib.h"
     8 #include "BaseRuntime.h"
     9 #include "sp_vm_engine.h"
    10 
    11 using namespace SourcePawn;
    12 
    13 SourcePawnEngine2::SourcePawnEngine2()
    14 {
    15 	m_Profiler = NULL;
    16 }
    17 
    18 IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
    19 {
    20 	sp_file_hdr_t hdr;
    21 	uint8_t *base;
    22 	int z_result;
    23 	int error;
    24 	BaseRuntime *pRuntime;
    25 
    26 	FILE *fp = fopen(file, "rb");
    27 
    28 	if (!fp)
    29 	{
    30 		error = SP_ERROR_NOT_FOUND;
    31 		goto return_error;
    32 	}
    33 
    34 	/* Rewind for safety */
    35 	fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
    36 
    37 	if (hdr.magic != SPFILE_MAGIC)
    38 	{
    39 		error = SP_ERROR_FILE_FORMAT;
    40 		goto return_error;
    41 	}
    42 
    43 	switch (hdr.compression)
    44 	{
    45 	case SPFILE_COMPRESSION_GZ:
    46 		{
    47 			uint32_t uncompsize = hdr.imagesize - hdr.dataoffs;
    48 			uint32_t compsize = hdr.disksize - hdr.dataoffs;
    49 			uint32_t sectsize = hdr.dataoffs - sizeof(sp_file_hdr_t);
    50 			uLongf destlen = uncompsize;
    51 
    52 			char *tempbuf = (char *)malloc(compsize);
    53 			void *uncompdata = malloc(uncompsize);
    54 			void *sectheader = malloc(sectsize);
    55 
    56 			fread(sectheader, sectsize, 1, fp);
    57 			fread(tempbuf, compsize, 1, fp);
    58 
    59 			z_result = uncompress((Bytef *)uncompdata, &destlen, (Bytef *)tempbuf, compsize);
    60 			free(tempbuf);
    61 			if (z_result != Z_OK)
    62 			{
    63 				free(sectheader);
    64 				free(uncompdata);
    65 				error = SP_ERROR_DECOMPRESSOR;
    66 				goto return_error;
    67 			}
    68 
    69 			base = (uint8_t *)malloc(hdr.imagesize);
    70 			memcpy(base, &hdr, sizeof(sp_file_hdr_t));
    71 			memcpy(base + sizeof(sp_file_hdr_t), sectheader, sectsize);
    72 			free(sectheader);
    73 			memcpy(base + hdr.dataoffs, uncompdata, uncompsize);
    74 			free(uncompdata);
    75 			break;
    76 		}
    77 	case SPFILE_COMPRESSION_NONE:
    78 		{
    79 			base = (uint8_t *)malloc(hdr.imagesize);
    80 			rewind(fp);
    81 			fread(base, hdr.imagesize, 1, fp);
    82 			break;
    83 		}
    84 	default:
    85 		{
    86 			error = SP_ERROR_DECOMPRESSOR;
    87 			goto return_error;
    88 		}
    89 	}
    90 
    91 
    92 	pRuntime = new BaseRuntime();
    93 	if ((error = pRuntime->CreateFromMemory(&hdr, base)) != SP_ERROR_NONE)
    94 	{
    95 		delete pRuntime;
    96 		goto return_error;
    97 	}
    98 
    99 	size_t len;
   100 	
   101 	len = strlen(file);
   102 	for (size_t i = len - 1; i >= 0 && i < len; i--)
   103 	{
   104 		if (file[i] == '/' 
   105 		#if defined WIN32
   106 			|| file[i] == '\\'
   107 		#endif
   108 		)
   109 		{
   110 			pRuntime->m_pPlugin->name = strdup(&file[i+1]);
   111 			break;
   112 		}
   113 	}
   114 
   115 	if (pRuntime->m_pPlugin->name == NULL)
   116 	{
   117 		pRuntime->m_pPlugin->name = strdup(file);
   118 	}
   119 
   120 	pRuntime->ApplyCompilationOptions(co);
   121 
   122     fclose(fp);
   123 
   124 	return pRuntime;
   125 
   126 return_error:
   127 	*err = error;
   128     fclose(fp);
   129 
   130 	return NULL;
   131 }
   132 
   133 SPVM_NATIVE_FUNC SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
   134 {
   135 	return g_Jit.CreateFakeNative(callback, pData);
   136 }
   137 
   138 void SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
   139 {
   140 	g_Jit.DestroyFakeNative(func);
   141 }
   142 
   143 const char *SourcePawnEngine2::GetEngineName()
   144 {
   145 	return "SourcePawn 1.1, jit-x86";
   146 }
   147 
   148 const char *SourcePawnEngine2::GetVersionString()
   149 {
   150 	return SVN_FULL_VERSION;
   151 }
   152 
   153 IProfiler *SourcePawnEngine2::GetProfiler()
   154 {
   155 	return m_Profiler;
   156 }
   157 
   158 void SourcePawnEngine2::SetProfiler(IProfiler *profiler)
   159 {
   160 	m_Profiler = profiler;
   161 }
   162 
   163 IDebugListener *SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
   164 {
   165 	return g_engine1.SetDebugListener(listener);
   166 }
   167 
   168 unsigned int SourcePawnEngine2::GetAPIVersion()
   169 {
   170 	return SOURCEPAWN_ENGINE2_API_VERSION;
   171 }
   172 
   173 ICompilation *SourcePawnEngine2::StartCompilation()
   174 {
   175 	return g_Jit.StartCompilation();
   176 }
   177 
   178 const char *SourcePawnEngine2::GetErrorString(int err)
   179 {
   180 	return g_engine1.GetErrorString(err);
   181 }
   182 
   183 bool SourcePawnEngine2::Initialize()
   184 {
   185 	return g_Jit.InitializeJIT();
   186 }
   187 
   188 void SourcePawnEngine2::Shutdown()
   189 {
   190 	g_Jit.ShutdownJIT();
   191 }