Remove use of memcmp in CheckCaches

This commit is contained in:
Jonathan G Rennison
2024-05-29 18:30:54 +01:00
parent 2b539a2d37
commit cb1e41fc88
8 changed files with 24 additions and 14 deletions

View File

@@ -68,6 +68,8 @@ struct AircraftCache {
uint32_t cached_max_range_sqr; ///< Cached squared maximum range. uint32_t cached_max_range_sqr; ///< Cached squared maximum range.
uint16_t cached_max_range; ///< Cached maximum range. uint16_t cached_max_range; ///< Cached maximum range.
uint8_t image_movement_state; ///< Cached image aircraft movement state uint8_t image_movement_state; ///< Cached image aircraft movement state
bool operator==(const AircraftCache &) const = default;
}; };
/** /**

View File

@@ -50,6 +50,8 @@ struct CompanyInfrastructure {
uint32_t GetTramTotal() const; uint32_t GetTramTotal() const;
char *Dump(char *buffer, const char *last) const; char *Dump(char *buffer, const char *last) const;
bool operator==(const CompanyInfrastructure &) const = default;
}; };
class FreeUnitIDGenerator { class FreeUnitIDGenerator {

View File

@@ -47,6 +47,8 @@ struct GroundVehicleCache {
/* Cached UI information. */ /* Cached UI information. */
uint16_t last_speed; ///< The last speed we did display, so we only have to redraw when this changes. uint16_t last_speed; ///< The last speed we did display, so we only have to redraw when this changes.
bool operator==(const GroundVehicleCache &) const = default;
}; };
/** Ground vehicle flags. */ /** Ground vehicle flags. */

View File

@@ -1749,7 +1749,7 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
if (old_town_caches[i].part_of_subsidy != t->cache.part_of_subsidy) { if (old_town_caches[i].part_of_subsidy != t->cache.part_of_subsidy) {
CCLOG("town cache population mismatch: town %i, (old size: %u, new size: %u)", (int)t->index, old_town_caches[i].part_of_subsidy, t->cache.part_of_subsidy); CCLOG("town cache population mismatch: town %i, (old size: %u, new size: %u)", (int)t->index, old_town_caches[i].part_of_subsidy, t->cache.part_of_subsidy);
} }
if (MemCmpT(old_town_caches[i].squared_town_zone_radius, t->cache.squared_town_zone_radius, lengthof(t->cache.squared_town_zone_radius)) != 0) { if (old_town_caches[i].squared_town_zone_radius != t->cache.squared_town_zone_radius) {
CCLOG("town cache squared_town_zone_radius mismatch: town %i", (int)t->index); CCLOG("town cache squared_town_zone_radius mismatch: town %i", (int)t->index);
} }
if (old_town_caches[i].building_counts != t->cache.building_counts) { if (old_town_caches[i].building_counts != t->cache.building_counts) {
@@ -1807,7 +1807,7 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
uint i = 0; uint i = 0;
for (const Company *c : Company::Iterate()) { for (const Company *c : Company::Iterate()) {
if (MemCmpT(old_infrastructure.data() + i, &c->infrastructure) != 0) { if (old_infrastructure[i] != c->infrastructure) {
CCLOG("infrastructure cache mismatch: company %i", (int)c->index); CCLOG("infrastructure cache mismatch: company %i", (int)c->index);
char buffer[4096]; char buffer[4096];
old_infrastructure[i].Dump(buffer, lastof(buffer)); old_infrastructure[i].Dump(buffer, lastof(buffer));
@@ -1963,10 +1963,10 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
}; };
switch (u->type) { switch (u->type) {
case VEH_TRAIN: case VEH_TRAIN:
if (memcmp(&gro_cache[length], &Train::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) { if (gro_cache[length] != Train::From(u)->gcache) {
print_gv_cache_diff("train", gro_cache[length], Train::From(u)->gcache); print_gv_cache_diff("train", gro_cache[length], Train::From(u)->gcache);
} }
if (memcmp(&tra_cache[length], &Train::From(u)->tcache, sizeof(TrainCache)) != 0) { if (tra_cache[length] != Train::From(u)->tcache) {
CCLOGV("train cache mismatch: %c%c%c%c%c%c%c%c%c%c%c", CCLOGV("train cache mismatch: %c%c%c%c%c%c%c%c%c%c%c",
tra_cache[length].cached_override != Train::From(u)->tcache.cached_override ? 'o' : '-', tra_cache[length].cached_override != Train::From(u)->tcache.cached_override ? 'o' : '-',
tra_cache[length].cached_curve_speed_mod != Train::From(u)->tcache.cached_curve_speed_mod ? 'C' : '-', tra_cache[length].cached_curve_speed_mod != Train::From(u)->tcache.cached_curve_speed_mod ? 'C' : '-',
@@ -1991,12 +1991,12 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
} }
break; break;
case VEH_ROAD: case VEH_ROAD:
if (memcmp(&gro_cache[length], &RoadVehicle::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) { if (gro_cache[length] != RoadVehicle::From(u)->gcache) {
print_gv_cache_diff("road vehicle", gro_cache[length], Train::From(u)->gcache); print_gv_cache_diff("road vehicle", gro_cache[length], Train::From(u)->gcache);
} }
break; break;
case VEH_AIRCRAFT: case VEH_AIRCRAFT:
if (memcmp(&air_cache[length], &Aircraft::From(u)->acache, sizeof(AircraftCache)) != 0) { if (air_cache[length] != Aircraft::From(u)->acache) {
CCLOGV("Aircraft vehicle cache mismatch: %c%c", CCLOGV("Aircraft vehicle cache mismatch: %c%c",
air_cache[length].cached_max_range != Aircraft::From(u)->acache.cached_max_range ? 'r' : '-', air_cache[length].cached_max_range != Aircraft::From(u)->acache.cached_max_range ? 'r' : '-',
air_cache[length].cached_max_range_sqr != Aircraft::From(u)->acache.cached_max_range_sqr ? 's' : '-'); air_cache[length].cached_max_range_sqr != Aircraft::From(u)->acache.cached_max_range_sqr ? 's' : '-');

View File

@@ -377,6 +377,8 @@ void SlResetTNNC()
_town_zone_radii_no_update = false; _town_zone_radii_no_update = false;
} }
static_assert(std::tuple_size<decltype(TownCache::squared_town_zone_radius)>::value == HZB_END);
void Save_TNNC() void Save_TNNC()
{ {
assert(_sl_xv_feature_versions[XSLFI_TNNC_CHUNK] != 0); assert(_sl_xv_feature_versions[XSLFI_TNNC_CHUNK] != 0);
@@ -391,7 +393,7 @@ void Save_TNNC()
if (IsGetTownZonesCallbackHandlerPresent()) { if (IsGetTownZonesCallbackHandlerPresent()) {
flags |= 1; flags |= 1;
length += Town::GetNumItems() * lengthof(TownCache::squared_town_zone_radius) * 4; length += Town::GetNumItems() * HZB_END * 4;
} }
SlSetLength(length); SlSetLength(length);
@@ -403,7 +405,7 @@ void Save_TNNC()
SlWriteUint32(t->index); SlWriteUint32(t->index);
SlWriteUint16(t->noise_reached); SlWriteUint16(t->noise_reached);
if (flags & 1) { if (flags & 1) {
for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { for (uint i = 0; i < HZB_END; i++) {
SlWriteUint32(t->cache.squared_town_zone_radius[i]); SlWriteUint32(t->cache.squared_town_zone_radius[i]);
} }
} }
@@ -430,7 +432,7 @@ void Load_TNNC()
if (t == nullptr) SlErrorCorrupt("TNNC: invalid town ID"); if (t == nullptr) SlErrorCorrupt("TNNC: invalid town ID");
t->noise_reached = SlReadUint16(); t->noise_reached = SlReadUint16();
if (flags & 1) { if (flags & 1) {
for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { for (uint i = 0; i < HZB_END; i++) {
t->cache.squared_town_zone_radius[i] = SlReadUint32(); t->cache.squared_town_zone_radius[i] = SlReadUint32();
} }
} }

View File

@@ -57,7 +57,7 @@ struct TownCache {
uint32_t population; ///< Current population of people uint32_t population; ///< Current population of people
TrackedViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this TrackedViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this
PartOfSubsidy part_of_subsidy; ///< Is this town a source/destination of a subsidy? PartOfSubsidy part_of_subsidy; ///< Is this town a source/destination of a subsidy?
uint32_t squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count std::array<uint32_t, HZB_END> squared_town_zone_radius; ///< UpdateTownRadius updates this given the house count
BuildingCounts<uint16_t> building_counts; ///< The number of each type of building in the town BuildingCounts<uint16_t> building_counts; ///< The number of each type of building in the town
}; };

View File

@@ -2105,7 +2105,7 @@ static bool GrowTown(Town *t)
*/ */
void UpdateTownRadius(Town *t) void UpdateTownRadius(Town *t)
{ {
static const uint32_t _town_squared_town_zone_radius_data[23][HZB_END] = { static const std::array<std::array<uint32_t, HZB_END>, 23> _town_squared_town_zone_radius_data = {{
{ 4, 0, 0, 0, 0}, // 0 { 4, 0, 0, 0, 0}, // 0
{ 16, 0, 0, 0, 0}, { 16, 0, 0, 0, 0},
{ 25, 0, 0, 0, 0}, { 25, 0, 0, 0, 0},
@@ -2129,7 +2129,7 @@ void UpdateTownRadius(Town *t)
{121, 81, 0, 49, 25}, // 80 {121, 81, 0, 49, 25}, // 80
{121, 81, 0, 49, 25}, {121, 81, 0, 49, 25},
{121, 81, 0, 49, 36}, // 88 {121, 81, 0, 49, 36}, // 88
}; }};
if (_settings_game.economy.town_zone_calc_mode) { if (_settings_game.economy.town_zone_calc_mode) {
int mass = t->cache.num_houses / 8; int mass = t->cache.num_houses / 8;
@@ -2149,7 +2149,7 @@ void UpdateTownRadius(Town *t)
return; return;
} }
MemSetT(t->cache.squared_town_zone_radius, 0, lengthof(t->cache.squared_town_zone_radius)); t->cache.squared_town_zone_radius = {};
uint16_t cb_result = GetTownZonesCallback(t); uint16_t cb_result = GetTownZonesCallback(t);
if (cb_result == 0) { if (cb_result == 0) {
@@ -2162,7 +2162,7 @@ void UpdateTownRadius(Town *t)
} }
if (t->cache.num_houses < 92) { if (t->cache.num_houses < 92) {
memcpy(t->cache.squared_town_zone_radius, _town_squared_town_zone_radius_data[t->cache.num_houses / 4], sizeof(t->cache.squared_town_zone_radius)); t->cache.squared_town_zone_radius = _town_squared_town_zone_radius_data[t->cache.num_houses / 4];
} else { } else {
int mass = t->cache.num_houses / 8; int mass = t->cache.num_houses / 8;
/* Actually we are proportional to sqrt() but that's right because we are covering an area. /* Actually we are proportional to sqrt() but that's right because we are covering an area.

View File

@@ -123,6 +123,8 @@ struct TrainCache {
int16_t cached_curve_speed_mod; ///< curve speed modifier of the entire train int16_t cached_curve_speed_mod; ///< curve speed modifier of the entire train
uint16_t cached_max_curve_speed; ///< max consist speed limited by curves uint16_t cached_max_curve_speed; ///< max consist speed limited by curves
bool operator==(const TrainCache &) const = default;
}; };
/** /**