Fixed build failure in profiler.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "sm_globals.h"
33 #include "HandleSys.h"
35 //Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
36 //nonetheless we need accurate counting
37 #if !defined PLATFORM_LINUX
46 #if defined PLATFORM_WINDOWS
55 HandleType_t g_ProfilerType = 0;
57 class ProfilerHelpers :
59 public IHandleTypeDispatch
62 void OnSourceModAllInitialized()
64 g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
66 void OnSourceModShutdown()
68 g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
70 void OnHandleDestroy(HandleType_t type, void *object)
72 Profiler *prof = (Profiler *)object;
77 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
79 Profiler *p = new Profiler();
81 #if defined PLATFORM_WINDOWS
83 QueryPerformanceFrequency(&qpf);
84 p->freq = 1.0 / (double)(qpf.QuadPart);
87 Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
88 if (hndl == BAD_HANDLE)
96 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
98 Handle_t hndl = params[1];
99 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
103 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
106 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
109 #if defined PLATFORM_WINDOWS
110 QueryPerformanceCounter(&prof->start);
113 prof->started = true;
114 prof->stopped = false;
119 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
121 Handle_t hndl = params[1];
122 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
126 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
129 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
134 return pContext->ThrowNativeError("Profiler was never started");
137 #if defined PLATFORM_WINDOWS
138 QueryPerformanceCounter(&prof->end);
141 prof->started = false;
142 prof->stopped = true;
147 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
149 Handle_t hndl = params[1];
150 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
154 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
157 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
162 return pContext->ThrowNativeError("Profiler was never stopped");
167 #if defined PLATFORM_WINDOWS
168 LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
169 double seconds = diff * prof->freq;
170 fTime = (float)seconds;
173 return sp_ftoc(fTime);
176 REGISTER_NATIVES(profilerNatives)
178 {"CreateProfiler", CreateProfiler},
179 {"GetProfilerTime", GetProfilerTime},
180 {"StartProfiling", StartProfiling},
181 {"StopProfiling", StopProfiling},