From 56dcd348919f2cf166cdede90648270fd42beb1f Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 3 Sep 2022 07:35:41 +0300 Subject: [PATCH 53/53] *_iterate_safe(): Evaluate list parameter just once See osdn #45480 Signed-off-by: Marko Lindqvist --- common/city.h | 7 ++++--- common/traderoutes.h | 6 ++++-- common/unitlist.h | 10 ++++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/common/city.h b/common/city.h index afc693593a..24c4d7defc 100644 --- a/common/city.h +++ b/common/city.h @@ -507,13 +507,14 @@ extern struct output_type output_types[]; #define city_list_iterate_safe(citylist, _city) \ { \ - int _city##_size = city_list_size(citylist); \ + struct city_list *_city##_cl = citylist; \ + int _city##_size = city_list_size(_city##_cl); \ \ if (_city##_size > 0) { \ int _city##_numbers[_city##_size]; \ int _city##_index = 0; \ \ - city_list_iterate(citylist, _city) { \ + city_list_iterate(_city##_cl, _city) { \ _city##_numbers[_city##_index++] = _city->id; \ } city_list_iterate_end; \ \ @@ -521,7 +522,7 @@ extern struct output_type output_types[]; _city##_index < _city##_size; \ _city##_index++) { \ struct city *_city = \ - game_city_by_number(_city##_numbers[_city##_index]); \ + game_city_by_number(_city##_numbers[_city##_index]); \ \ if (NULL != _city) { diff --git a/common/traderoutes.h b/common/traderoutes.h index 198756cff3..58fa11bb56 100644 --- a/common/traderoutes.h +++ b/common/traderoutes.h @@ -17,6 +17,7 @@ extern "C" { #endif /* __cplusplus */ +/* utility */ #include "support.h" /* bool */ struct city; @@ -134,11 +135,12 @@ do { \ #define trade_routes_iterate_safe(c, proute) \ { \ - int _routes##_size = trade_route_list_size(c->routes); \ + struct city *c##_proute = c; \ + int _routes##_size = trade_route_list_size(c##_proute->routes); \ if (_routes##_size > 0) { \ struct trade_route *_routes##_saved[_routes##_size]; \ int _routes##_index = 0; \ - trade_routes_iterate(c, _proute) { \ + trade_routes_iterate(c##_proute, _proute) { \ _routes##_saved[_routes##_index++] = _proute; \ } trade_routes_iterate_end; \ for (_routes##_index = 0; \ diff --git a/common/unitlist.h b/common/unitlist.h index 6273a1d908..98e83c4d8b 100644 --- a/common/unitlist.h +++ b/common/unitlist.h @@ -18,9 +18,10 @@ extern "C" { #endif /* __cplusplus */ +/* common */ #include "fc_types.h" -#include "unit.h" /* for diplomat_actions */ -#include "unittype.h" /* for unit_type_flag_id */ +#include "unit.h" /* for diplomat_actions */ +#include "unittype.h" /* for unit_type_flag_id */ /* get 'struct unit_list' and related functions: */ #define SPECLIST_TAG unit @@ -37,13 +38,14 @@ extern "C" { #define unit_list_iterate_safe(unitlist, _unit) \ { \ - int _unit##_size = unit_list_size(unitlist); \ + struct unit_list *_unit##_ul = unitlist; \ + int _unit##_size = unit_list_size(_unit##_ul); \ \ if (_unit##_size > 0) { \ int _unit##_numbers[_unit##_size]; \ int _unit##_index = 0; \ \ - unit_list_iterate(unitlist, _unit) { \ + unit_list_iterate(_unit##_ul, _unit) { \ _unit##_numbers[_unit##_index++] = _unit->id; \ } unit_list_iterate_end; \ \ -- 2.35.1