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 12, 2025
1 parent 6cde9cd commit ed18f16
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 0 additions & 5 deletions rts/Sim/Units/UnitHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ 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 ), "");

{
// set the global (runtime-constant) unit-limit as the sum
// of all team unit-limits, which is *always* <= MAX_UNITS
Expand Down
18 changes: 15 additions & 3 deletions rts/Sim/Units/UnitMemPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@
#define UNIT_MEMPOOL_H

#include "UnitTypes/Builder.h"
#include "UnitTypes/Building.h"
#include "UnitTypes/ExtractorBuilding.h"
#include "UnitTypes/Factory.h"

#include "Sim/Misc/GlobalConstants.h"
#include "System/MemPoolTypes.h"

/* Needs manually listing all derived classes to find the biggest,
* but we probably can't do better, at least without reflection */
union LargestDerivedFromCUnit {
CBuilder builder;
CFactory factory;
CBuilding building;
CExtractorBuilding extractorBuilding;
};

#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 ed18f16

Please sign in to comment.