Skip to content

Commit 36f88bc

Browse files
committed
Add engineSetPoolCapacity support
1 parent 53ad82a commit 36f88bc

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

Client/game_sa/CDynamicPool.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,53 @@ class CDynamicPool
133133
return size;
134134
}
135135

136+
bool SetCapacity(std::size_t newSize) {
137+
if (newSize == 0)
138+
return false;
139+
140+
std::size_t currentSize = GetCapacity();
141+
142+
if (currentSize == newSize)
143+
return false;
144+
else if (currentSize < newSize)
145+
{
146+
// Grown
147+
while (currentSize < newSize)
148+
{
149+
try
150+
{
151+
auto* nextPart = AllocateNewPart();
152+
currentSize += nextPart->GetCapacity();
153+
}
154+
catch (const std::bad_alloc& ex)
155+
{
156+
return false;
157+
}
158+
}
159+
}
160+
else
161+
{
162+
// Shrink
163+
while (true)
164+
{
165+
auto* part = m_poolParts.back().get();
166+
if (part->GetUsedSize() != 0)
167+
return false;
168+
169+
currentSize -= part->GetCapacity();
170+
if (currentSize < newSize)
171+
return false;
172+
173+
m_poolParts.pop_back();
174+
175+
if (currentSize == newSize)
176+
return true;
177+
}
178+
}
179+
180+
return true;
181+
}
182+
136183
private:
137184
CDynamicPoolPart<PoolObjT>* AllocateNewPart()
138185
{

Client/game_sa/CPoolsSA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CPoolsSA : public CPools
9898
CBuildingsPool& GetBuildingsPool() noexcept override { return m_BuildingsPool; };
9999
CDummyPool& GetDummyPool() noexcept { return m_DummyPool; };
100100
CTxdPool& GetTxdPool() noexcept { return m_TxdPool; };
101-
CPtrNodeSingleLinkPoolSA& GetPtrNodeSingleLinkPool() noexcept { return m_PtrNodeSingleLinkPool; };
101+
CPtrNodeSingleLinkPool& GetPtrNodeSingleLinkPool() noexcept override { return m_PtrNodeSingleLinkPool; };
102102

103103
private:
104104
// Pools

Client/game_sa/CPtrNodeSingleLinkPoolSA.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
#include "CPoolSAInterface.h"
1515
#include "CDynamicPool.h"
1616
#include "CPtrNodeSingleListSA.h"
17+
#include <game/CPtrNodeSingleLinkPool.h>
1718

18-
class CPtrNodeSingleLinkPoolSA
19+
class CPtrNodeSingleLinkPoolSA final : public CPtrNodeSingleLinkPool
1920
{
2021
public:
2122
using pool_item_t = CPtrNodeSingleLink<void*>;
2223
using pool_t = CDynamicPool<pool_item_t, PoolGrownByHalfStrategy<MAX_POINTER_SINGLE_LINKS>>;
2324

2425
CPtrNodeSingleLinkPoolSA();
2526

26-
int GetCapacity() const { return m_customPool->GetCapacity(); }
27-
int GetUsedSize() const { return m_customPool->GetUsedSize(); }
27+
std::size_t GetCapacity() const override { return m_customPool->GetCapacity(); }
28+
std::size_t GetUsedSize() const override { return m_customPool->GetUsedSize(); }
29+
30+
bool Resize(std::size_t newSize) override { return m_customPool->SetCapacity(newSize); };
2831

2932
static auto* GetPoolInstance() { return m_customPool; }
3033
static void StaticSetHooks();

Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <game/CColPoint.h>
1414
#include <game/CObjectGroupPhysicalProperties.h>
1515
#include <game/CStreaming.h>
16+
#include <game/CPtrNodeSingleLinkPool.h>
1617
#include <lua/CLuaFunctionParser.h>
1718
#include "CLuaEngineDefs.h"
1819

@@ -2505,6 +2506,10 @@ bool CLuaEngineDefs::EngineSetPoolCapacity(lua_State* luaVM, ePools pool, size_t
25052506
{
25062507
return m_pBuildingManager->SetPoolCapacity(newSize);
25072508
}
2509+
case ePools::POINTER_SINGLE_LINK_POOL:
2510+
{
2511+
return g_pGame->GetPools()->GetPtrNodeSingleLinkPool().Resize(newSize);
2512+
}
25082513
default:
25092514
throw std::invalid_argument("Can not change this pool capacity");
25102515
}

Client/sdk/game/CPools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CBuildingsPool.h"
1616
#include "CDummyPool.h"
1717
#include "CTxdPool.h"
18+
#include "CPtrNodeSingleLinkPool.h"
1819

1920
class CClientEntity;
2021
class CEntity;
@@ -111,4 +112,5 @@ class CPools
111112
virtual CBuildingsPool& GetBuildingsPool() noexcept = 0;
112113
virtual CDummyPool& GetDummyPool() noexcept = 0;
113114
virtual CTxdPool& GetTxdPool() noexcept = 0;
115+
virtual CPtrNodeSingleLinkPool& GetPtrNodeSingleLinkPool() noexcept = 0;
114116
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: sdk/game/CPtrNodeSingleLinkPool.h
6+
*
7+
* Multi Theft Auto is available from http://www.multitheftauto.com/
8+
*
9+
*****************************************************************************/
10+
11+
#pragma once
12+
13+
class CPtrNodeSingleLinkPool
14+
{
15+
public:
16+
virtual bool Resize(std::size_t size) = 0;
17+
virtual std::size_t GetCapacity() const = 0;
18+
virtual std::size_t GetUsedSize() const = 0;
19+
};

0 commit comments

Comments
 (0)