From 67c8ee4ce3a2a3f361386ef12bf6d75c898e872a Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 5 Sep 2015 21:10:32 -0700 Subject: [PATCH] Move CDataPack from core to logic. --- bridge/include/LogicProvider.h | 3 +++ core/AMBuilder | 1 - core/logic/AMBuilder | 1 + core/{ => logic}/CDataPack.cpp | 25 ++++++++++++++++++++++++- core/{ => logic}/CDataPack.h | 3 +++ core/logic/common_logic.cpp | 3 +++ core/logic/smn_datapacks.cpp | 11 +++-------- core/sourcemod.cpp | 24 ++---------------------- core/sourcemod.h | 2 -- 9 files changed, 39 insertions(+), 34 deletions(-) rename core/{ => logic}/CDataPack.cpp (89%) rename core/{ => logic}/CDataPack.h (94%) diff --git a/bridge/include/LogicProvider.h b/bridge/include/LogicProvider.h index 4b3c33b5..34f58826 100644 --- a/bridge/include/LogicProvider.h +++ b/bridge/include/LogicProvider.h @@ -50,6 +50,7 @@ class IProviderCallbacks; class IExtensionSys; class ITextParsers; class ILogger; +class IDataPack; struct sm_logic_t { @@ -71,6 +72,8 @@ struct sm_logic_t bool (*DumpAdminCache)(const char *filename); void (*RegisterProfiler)(IProfilingTool *tool); void (*OnRootCommand)(const ICommandArgs *args); + IDataPack * (*CreateDataPack)(); + void (*FreeDataPack)(IDataPack *pack); IScriptManager *scripts; IShareSys *sharesys; IExtensionSys *extsys; diff --git a/core/AMBuilder b/core/AMBuilder index 85181755..b4488a0c 100644 --- a/core/AMBuilder +++ b/core/AMBuilder @@ -9,7 +9,6 @@ project.sources += [ 'sm_stringutil.cpp', 'MenuVoting.cpp', 'smn_events.cpp', - 'CDataPack.cpp', 'frame_hooks.cpp', 'smn_nextmap.cpp', 'sourcemm_api.cpp', diff --git a/core/logic/AMBuilder b/core/logic/AMBuilder index 56955df5..bbc9310f 100644 --- a/core/logic/AMBuilder +++ b/core/logic/AMBuilder @@ -79,6 +79,7 @@ binary.sources += [ 'sprintf.cpp', 'LibrarySys.cpp', 'RootConsoleMenu.cpp', + 'CDataPack.cpp', ] if builder.target_platform == 'windows': binary.sources += ['thread/WinThreads.cpp'] diff --git a/core/CDataPack.cpp b/core/logic/CDataPack.cpp similarity index 89% rename from core/CDataPack.cpp rename to core/logic/CDataPack.cpp index 268d619d..39fea990 100644 --- a/core/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -32,6 +32,10 @@ #include #include #include "CDataPack.h" +#include +#include + +using namespace ke; #define DATAPACK_INITIAL_SIZE 64 @@ -47,6 +51,25 @@ CDataPack::~CDataPack() free(m_pBase); } +static Vector> sDataPackCache; + +IDataPack * CDataPack::New() +{ + if (sDataPackCache.empty()) + return new CDataPack(); + + CDataPack *pack = sDataPackCache.back().take(); + sDataPackCache.pop(); + pack->Initialize(); + return pack; +} + +void +CDataPack::Free(IDataPack *pack) +{ + sDataPackCache.append(static_cast(pack)); +} + void CDataPack::Initialize() { m_curptr = m_pBase; @@ -331,4 +354,4 @@ cell_t CDataPack::ReadFunction() const cell_t val = *reinterpret_cast(m_curptr); m_curptr += sizeof(cell_t); return val; -} \ No newline at end of file +} diff --git a/core/CDataPack.h b/core/logic/CDataPack.h similarity index 94% rename from core/CDataPack.h rename to core/logic/CDataPack.h index ec1195d2..5acebd69 100644 --- a/core/CDataPack.h +++ b/core/logic/CDataPack.h @@ -41,6 +41,9 @@ class CDataPack : public IDataPack public: CDataPack(); ~CDataPack(); + + static IDataPack *New(); + static void Free(IDataPack *pack); public: //IDataReader void Reset() const; size_t GetPosition() const; diff --git a/core/logic/common_logic.cpp b/core/logic/common_logic.cpp index 96ffc9ad..807edad1 100644 --- a/core/logic/common_logic.cpp +++ b/core/logic/common_logic.cpp @@ -54,6 +54,7 @@ #include "sprintf.h" #include "LibrarySys.h" #include "RootConsoleMenu.h" +#include "CDataPack.h" #include #include @@ -158,6 +159,8 @@ static sm_logic_t logic = DumpAdminCache, RegisterProfiler, OnRootCommand, + CDataPack::New, + CDataPack::Free, &g_PluginSys, &g_ShareSys, &g_Extensions, diff --git a/core/logic/smn_datapacks.cpp b/core/logic/smn_datapacks.cpp index bbc4c229..1b588e12 100644 --- a/core/logic/smn_datapacks.cpp +++ b/core/logic/smn_datapacks.cpp @@ -32,12 +32,7 @@ #include "common_logic.h" #include #include - -// This just in from the bucket o' hacks department. -// One day, IDataPack will go away and CDataPack will be merged into this file. -// This internal header is included directly to access GetCapacity, -// which can not be added to the public interface due to ABI issues. -#include "../CDataPack.h" +#include "CDataPack.h" HandleType_t g_DataPackType; @@ -66,7 +61,7 @@ public: } void OnHandleDestroy(HandleType_t type, void *object) { - g_pSM->FreeDataPack(reinterpret_cast(object)); + CDataPack::Free(reinterpret_cast(object)); } bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize) { @@ -78,7 +73,7 @@ public: static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params) { - IDataPack *pDataPack = g_pSM->CreateDataPack(); + IDataPack *pDataPack = CDataPack::New(); if (!pDataPack) { diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index afc5394a..6139a735 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -515,16 +515,6 @@ void SourceModBase::ShutdownServices() pBase = pBase->m_pGlobalClassNext; } - /* Delete all data packs */ - CStack::iterator iter; - CDataPack *pd; - for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++) - { - pd = (*iter); - delete pd; - } - m_freepacks.popall(); - sCoreProviderImpl.ShutdownHooks(); /* Notify! */ @@ -624,26 +614,16 @@ unsigned int SourceModBase::GetGlobalTarget() const IDataPack *SourceModBase::CreateDataPack() { - CDataPack *pack; - if (m_freepacks.empty()) - { - pack = new CDataPack; - } else { - pack = m_freepacks.front(); - m_freepacks.pop(); - pack->Initialize(); - } - return pack; + return logicore.CreateDataPack(); } void SourceModBase::FreeDataPack(IDataPack *pack) { - m_freepacks.push(static_cast(pack)); + logicore.FreeDataPack(pack); } Handle_t SourceModBase::GetDataPackHandleType(bool readonly) { - //:TODO: return 0; } diff --git a/core/sourcemod.h b/core/sourcemod.h index c45a1f2f..db16772f 100644 --- a/core/sourcemod.h +++ b/core/sourcemod.h @@ -36,7 +36,6 @@ #include #include #include -#include "CDataPack.h" using namespace SourceHook; @@ -139,7 +138,6 @@ public: // ISourceMod private: void ShutdownServices(); private: - CStack m_freepacks; char m_SMBaseDir[PLATFORM_MAX_PATH]; char m_SMRelDir[PLATFORM_MAX_PATH]; char m_ModDir[32];