Skip to content

Commit

Permalink
Merge pull request #392 from alliedmodders/mv-datapack
Browse files Browse the repository at this point in the history
Move CDataPack from core to logic.
  • Loading branch information
dvander committed Sep 9, 2015
2 parents d85568b + 67c8ee4 commit 67ba703
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 34 deletions.
3 changes: 3 additions & 0 deletions bridge/include/LogicProvider.h
Expand Up @@ -50,6 +50,7 @@ class IProviderCallbacks;
class IExtensionSys;
class ITextParsers;
class ILogger;
class IDataPack;

struct sm_logic_t
{
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion core/AMBuilder
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions core/logic/AMBuilder
Expand Up @@ -79,6 +79,7 @@ binary.sources += [
'sprintf.cpp',
'LibrarySys.cpp',
'RootConsoleMenu.cpp',
'CDataPack.cpp',
]
if builder.target_platform == 'windows':
binary.sources += ['thread/WinThreads.cpp']
Expand Down
25 changes: 24 additions & 1 deletion core/CDataPack.cpp → core/logic/CDataPack.cpp
Expand Up @@ -32,6 +32,10 @@
#include <stdlib.h>
#include <string.h>
#include "CDataPack.h"
#include <am-utility.h>
#include <am-vector.h>

using namespace ke;

#define DATAPACK_INITIAL_SIZE 64

Expand All @@ -47,6 +51,25 @@ CDataPack::~CDataPack()
free(m_pBase);
}

static Vector<AutoPtr<CDataPack>> 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<CDataPack *>(pack));
}

void CDataPack::Initialize()
{
m_curptr = m_pBase;
Expand Down Expand Up @@ -331,4 +354,4 @@ cell_t CDataPack::ReadFunction() const
cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
m_curptr += sizeof(cell_t);
return val;
}
}
3 changes: 3 additions & 0 deletions core/CDataPack.h → core/logic/CDataPack.h
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/logic/common_logic.cpp
Expand Up @@ -54,6 +54,7 @@
#include "sprintf.h"
#include "LibrarySys.h"
#include "RootConsoleMenu.h"
#include "CDataPack.h"
#include <bridge/include/BridgeAPI.h>
#include <bridge/include/IProviderCallbacks.h>

Expand Down Expand Up @@ -158,6 +159,8 @@ static sm_logic_t logic =
DumpAdminCache,
RegisterProfiler,
OnRootCommand,
CDataPack::New,
CDataPack::Free,
&g_PluginSys,
&g_ShareSys,
&g_Extensions,
Expand Down
11 changes: 3 additions & 8 deletions core/logic/smn_datapacks.cpp
Expand Up @@ -32,12 +32,7 @@
#include "common_logic.h"
#include <IHandleSys.h>
#include <ISourceMod.h>

// 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;

Expand Down Expand Up @@ -66,7 +61,7 @@ class DataPackNatives :
}
void OnHandleDestroy(HandleType_t type, void *object)
{
g_pSM->FreeDataPack(reinterpret_cast<IDataPack *>(object));
CDataPack::Free(reinterpret_cast<IDataPack *>(object));
}
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
{
Expand All @@ -78,7 +73,7 @@ class DataPackNatives :

static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
{
IDataPack *pDataPack = g_pSM->CreateDataPack();
IDataPack *pDataPack = CDataPack::New();

if (!pDataPack)
{
Expand Down
24 changes: 2 additions & 22 deletions core/sourcemod.cpp
Expand Up @@ -515,16 +515,6 @@ void SourceModBase::ShutdownServices()
pBase = pBase->m_pGlobalClassNext;
}

/* Delete all data packs */
CStack<CDataPack *>::iterator iter;
CDataPack *pd;
for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
{
pd = (*iter);
delete pd;
}
m_freepacks.popall();

sCoreProviderImpl.ShutdownHooks();

/* Notify! */
Expand Down Expand Up @@ -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<CDataPack *>(pack));
logicore.FreeDataPack(pack);
}

Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
{
//:TODO:
return 0;
}

Expand Down
2 changes: 0 additions & 2 deletions core/sourcemod.h
Expand Up @@ -36,7 +36,6 @@
#include <ISourceMod.h>
#include <sh_stack.h>
#include <sh_vector.h>
#include "CDataPack.h"

using namespace SourceHook;

Expand Down Expand Up @@ -139,7 +138,6 @@ class SourceModBase :
private:
void ShutdownServices();
private:
CStack<CDataPack *> m_freepacks;
char m_SMBaseDir[PLATFORM_MAX_PATH];
char m_SMRelDir[PLATFORM_MAX_PATH];
char m_ModDir[32];
Expand Down

0 comments on commit 67ba703

Please sign in to comment.