Skip to content

Commit

Permalink
Future-proofing for CUnit-derived class sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
saurtron authored and sprunk committed Jan 10, 2025
1 parent 6cde9cd commit e2a584c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions rts/Sim/Units/UnitHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ void CUnitHandler::Init() {
GeneralMoveSystem::Init();
UnitTrapCheckSystem::Init();

static_assert(sizeof(CBuilder) >= sizeof(CUnit ), "");
static_assert(sizeof(CBuilder) >= sizeof(CBuilding ), "");
static_assert(sizeof(CBuilder) >= sizeof(CExtractorBuilding), "");
static_assert(sizeof(CBuilder) >= sizeof(CFactory ), "");
static_assert(sizeof(LargestDerivedFromCUnit) >= sizeof(CUnit ), "");
static_assert(sizeof(LargestDerivedFromCUnit) >= sizeof(CBuilder ), "");
static_assert(sizeof(LargestDerivedFromCUnit) >= sizeof(CBuilding ), "");
static_assert(sizeof(LargestDerivedFromCUnit) >= sizeof(CExtractorBuilding), "");
static_assert(sizeof(LargestDerivedFromCUnit) >= sizeof(CFactory ), "");

{
// set the global (runtime-constant) unit-limit as the sum
Expand Down
8 changes: 6 additions & 2 deletions rts/Sim/Units/UnitMemPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#include "Sim/Misc/GlobalConstants.h"
#include "System/MemPoolTypes.h"

/* Size asserted manually in UnitHandler.cpp (to avoid extra #includes here).
* TODO: would be good if this could be found automatically via metaprogramming */
using LargestDerivedFromCUnit = CBuilder;

#if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64))
// CBuilder is (currently) the largest derived unit-type
typedef StaticMemPoolT<MAX_UNITS, CBuilder> UnitMemPool;
typedef StaticMemPoolT<MAX_UNITS, LargestDerivedFromCUnit> UnitMemPool;
#else
typedef FixedDynMemPoolT<MAX_UNITS / 1000, MAX_UNITS / 32, CBuilder> UnitMemPool;
typedef FixedDynMemPoolT<MAX_UNITS / 1000, MAX_UNITS / 32, LargestDerivedFromCUnit> UnitMemPool;
#endif

extern UnitMemPool unitMemPool;
Expand Down

0 comments on commit e2a584c

Please sign in to comment.