1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-22 11:44:17 +01:00

Codechange: remove need for new (address) PoolItem(...)

This commit is contained in:
Rubidium
2026-01-03 16:40:12 +01:00
committed by rubidium42
parent 2e6f8fe191
commit e9e7e4af51
6 changed files with 13 additions and 28 deletions

View File

@@ -318,26 +318,8 @@ public:
/** Do not use new (index) PoolItem(...), but rather PoolItem::CreateAtIndex(index, ...). */
inline void *operator new(size_t size, Tindex index) = delete;
/**
* Allocates space for new Titem at given memory address
* @param ptr where are we allocating the item?
* @return pointer to allocated memory (== ptr)
* @note use of this is strongly discouraged
* @pre the memory must not be allocated in the Pool!
*/
inline void *operator new(size_t, void *ptr)
{
for (size_t i = 0; i < Tpool->first_unused; i++) {
/* Don't allow creating new objects over existing.
* Even if we called the destructor and reused this memory,
* we don't know whether 'size' and size of currently allocated
* memory are the same (because of possible inheritance).
* Use { size_t index = item->index; delete item; new (index) item; }
* instead to make sure destructor is called and no memory leaks. */
assert(ptr != Tpool->data[i]);
}
return ptr;
}
/** Do not use new (address) PoolItem(...). */
inline void *operator new(size_t, void *ptr) = delete;
/**

View File

@@ -71,6 +71,10 @@ static_assert(lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_in
Engine::Engine(EngineID index, VehicleType type, uint16_t local_id) : EnginePool::PoolItem<&_engine_pool>(index)
{
this->type = type;
/* Called in the context of loading a savegame. The rest comes from the loader. */
if (type == VEH_INVALID) return;
this->grf_prop.local_id = local_id;
this->list_position = local_id;
this->preview_company = CompanyID::Invalid();

View File

@@ -77,7 +77,6 @@ private:
std::variant<std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo> vehicle_info{};
public:
Engine(EngineID index) : EnginePool::PoolItem<&_engine_pool>(index) {}
Engine(EngineID index, VehicleType type, uint16_t local_id);
bool IsEnabled() const;

View File

@@ -43,12 +43,12 @@ static const SaveLoad _engine_desc[] = {
static TypedIndexContainer<std::vector<Engine>, EngineID> _temp_engine;
Engine *GetTempDataEngine(EngineID index)
Engine *GetTempDataEngine(EngineID index, VehicleType type, uint16_t local_id)
{
if (index < _temp_engine.size()) {
return &_temp_engine[index];
} else if (index == _temp_engine.size()) {
return &_temp_engine.emplace_back(index);
return &_temp_engine.emplace_back(index, type, local_id);
} else {
NOT_REACHED();
}

View File

@@ -379,10 +379,10 @@ static bool FixTTOEngines()
/* Load the default engine set. Many of them will be overridden later */
{
EngineID j = EngineID::Begin();
for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_TRAIN, i);
for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_ROAD, i);
for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_SHIP, i);
for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_AIRCRAFT, i);
for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) GetTempDataEngine(j, VEH_TRAIN, i);
for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) GetTempDataEngine(j, VEH_ROAD, i);
for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) GetTempDataEngine(j, VEH_SHIP, i);
for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) GetTempDataEngine(j, VEH_AIRCRAFT, i);
}
TimerGameCalendar::Date aging_date = std::min(TimerGameCalendar::date + CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR, TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year{2050}, 0, 1));

View File

@@ -44,7 +44,7 @@ void ConvertOldMultiheadToNew();
void ConnectMultiheadedTrains();
void ResetTempEngineData();
Engine *GetTempDataEngine(EngineID index);
Engine *GetTempDataEngine(EngineID index, VehicleType type = VEH_INVALID, uint16_t local_id = 0);
void CopyTempEngineData();
extern int32_t _saved_scrollpos_x;