From d164ba2b93d018287cf1608cf4c1501ed1f4fb91 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 25 Mar 2022 14:04:28 +0100 Subject: [PATCH 1/4] Change aitraits to a vector --- ai/aitraits.cpp | 7 ++----- common/player.cpp | 2 +- common/player.h | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ai/aitraits.cpp b/ai/aitraits.cpp index dea340d57c..3e81e3e642 100644 --- a/ai/aitraits.cpp +++ b/ai/aitraits.cpp @@ -33,8 +33,7 @@ void ai_traits_init(struct player *pplayer) { enum trait tr; - pplayer->ai_common.traits = static_cast(fc_realloc( - pplayer->ai_common.traits, sizeof(struct ai_trait) * TRAIT_COUNT)); + pplayer->ai_common.traits = std::vector(TRAIT_COUNT); for (tr = trait_begin(); tr != trait_end(); tr = trait_next(tr)) { int min = pplayer->nation->server.traits[tr].min; @@ -58,9 +57,7 @@ void ai_traits_init(struct player *pplayer) */ void ai_traits_close(struct player *pplayer) { - free(pplayer->ai_common.traits); // realloc - - pplayer->ai_common.traits = nullptr; + pplayer->ai_common.traits.clear(); } /** diff --git a/common/player.cpp b/common/player.cpp index 924c0ff5cc..841a872ac4 100644 --- a/common/player.cpp +++ b/common/player.cpp @@ -575,7 +575,7 @@ static void player_defaults(struct player *pplayer) pplayer->ai_common.love[player_slot_index(pslot)] = 1; } player_slots_iterate_end; - pplayer->ai_common.traits = nullptr; + pplayer->ai_common.traits.clear(); pplayer->ai = nullptr; pplayer->was_created = false; diff --git a/common/player.h b/common/player.h index 03b7dd0d6e..aff423743e 100644 --- a/common/player.h +++ b/common/player.h @@ -112,7 +112,7 @@ struct player_ai { int love[MAX_NUM_PLAYER_SLOTS]; - struct ai_trait *traits; + std::vector traits; }; /* Diplomatic states (how one player views another). From a6d2aea7ad0dd58c0e02b1d88ced6a3680a7b541 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 25 Mar 2022 17:56:38 +0100 Subject: [PATCH 2/4] Change city_report_specs to a vector --- client/cityrepdata.cpp | 5 ++--- client/cityrepdata.h | 2 +- client/gui-qt/cityrep.cpp | 43 +++++++++++++++++++-------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/client/cityrepdata.cpp b/client/cityrepdata.cpp index e27c5b0b24..5e70c3915c 100644 --- a/client/cityrepdata.cpp +++ b/client/cityrepdata.cpp @@ -790,7 +790,7 @@ static const struct city_report_spec base_city_report_specs[] = { {true, 0, 1, N_("Currently Building"), N_("?Stock/Target:(Have/Need)"), N_("Currently Building"), nullptr, FUNC_TAG(building)}}; -struct city_report_spec *city_report_specs; +std::vector city_report_specs; static int num_creport_cols; /** @@ -828,8 +828,7 @@ void init_city_report_game_data() num_creport_cols = ARRAY_SIZE(base_city_report_specs) + specialist_count() + 1; - city_report_specs = static_cast(fc_realloc( - city_report_specs, num_creport_cols * sizeof(*city_report_specs))); + city_report_specs = std::vector(num_creport_cols); p = &city_report_specs[0]; fc_snprintf(sp_explanations, sizeof(sp_explanations), "%s", diff --git a/client/cityrepdata.h b/client/cityrepdata.h index b4ecb7fb2d..af177bddf9 100644 --- a/client/cityrepdata.h +++ b/client/cityrepdata.h @@ -29,7 +29,7 @@ struct city_report_spec { const char *tagname; // for save_options }; -extern struct city_report_spec *city_report_specs; +extern std::vector city_report_specs; int num_city_report_spec(); bool *city_report_spec_show_ptr(int i); diff --git a/client/gui-qt/cityrep.cpp b/client/gui-qt/cityrep.cpp index 9e12b6ce6e..8a717f652c 100644 --- a/client/gui-qt/cityrep.cpp +++ b/client/gui-qt/cityrep.cpp @@ -75,9 +75,9 @@ void city_item_delegate::paint(QPainter *painter, QString txt; QFont font; QPalette palette; - struct city_report_spec *spec; - spec = city_report_specs + index.column(); - txt = spec->tagname; + struct city_report_spec spec; + spec = city_report_specs[index.column()]; + txt = spec.tagname; if (txt == QLatin1String("cityname")) { font.setCapitalization(QFont::SmallCaps); font.setBold(true); @@ -138,7 +138,7 @@ bool city_item::setData(int column, const QVariant &value, int role) */ QVariant city_item::data(int column, int role) const { - struct city_report_spec *spec; + struct city_report_spec spec; if (role == Qt::UserRole && column == 0) { return QVariant::fromValue((void *) i_city); @@ -146,8 +146,8 @@ QVariant city_item::data(int column, int role) const if (role != Qt::DisplayRole) { return QVariant(); } - spec = city_report_specs + column; - QString buf = QStringLiteral("%1").arg(spec->func(i_city, spec->data)); + spec = city_report_specs[column]; + QString buf = QStringLiteral("%1").arg(spec.func(i_city, spec.data)); return buf.trimmed(); } @@ -219,24 +219,23 @@ bool city_model::setData(const QModelIndex &index, const QVariant &value, QVariant city_model::headerData(int section, Qt::Orientation orientation, int role) const { - struct city_report_spec *spec = city_report_specs + section; + struct city_report_spec spec = city_report_specs[section]; if (orientation == Qt::Horizontal && section < NUM_CREPORT_COLS) { if (role == Qt::DisplayRole) { - QString buf = - QStringLiteral("%1\n%2").arg(spec->title1 ? spec->title1 : "", - spec->title2 ? spec->title2 : ""); - QIcon i = hIcon::i()->get(spec->tagname); + QString buf = QStringLiteral("%1\n%2").arg( + spec.title1 ? spec.title1 : "", spec.title2 ? spec.title2 : ""); + QIcon i = hIcon::i()->get(spec.tagname); if (!i.isNull()) { // icon exists for that header return QString(); } return buf.trimmed(); } if (role == Qt::ToolTipRole) { - return QString(spec->explanation); + return QString(spec.explanation); } if (role == Qt::DecorationRole) { - QIcon i = hIcon::i()->get(spec->tagname); + QIcon i = hIcon::i()->get(spec.tagname); if (!i.isNull()) { return i; } @@ -250,11 +249,11 @@ QVariant city_model::headerData(int section, Qt::Orientation orientation, */ QVariant city_model::menu_data(int section) const { - struct city_report_spec *spec; + struct city_report_spec spec; if (section < NUM_CREPORT_COLS) { - spec = city_report_specs + section; - return QString(spec->explanation); + spec = city_report_specs[section]; + return QString(spec.explanation); } return QVariant(); } @@ -264,11 +263,11 @@ QVariant city_model::menu_data(int section) const */ QVariant city_model::hide_data(int section) const { - struct city_report_spec *spec; + struct city_report_spec spec; if (section < NUM_CREPORT_COLS) { - spec = city_report_specs + section; - return spec->show; + spec = city_report_specs[section]; + return spec.show; } return QVariant(); } @@ -1117,7 +1116,7 @@ void city_widget::display_header_menu(const QPoint) hideshow_column->setAttribute(Qt::WA_DeleteOnClose); connect(hideshow_column, &QMenu::triggered, this, [=](QAction *act) { int col; - struct city_report_spec *spec; + struct city_report_spec spec; if (!act) { return; } @@ -1125,8 +1124,8 @@ void city_widget::display_header_menu(const QPoint) col = actions.indexOf(act); fc_assert_ret(col >= 0); setColumnHidden(col, !isColumnHidden(col)); - spec = city_report_specs + col; - spec->show = !spec->show; + spec = city_report_specs[col]; + spec.show = !spec.show; if (!isColumnHidden(col) && columnWidth(col) <= 5) { setColumnWidth(col, 100); } From 8a19414ee4e5590f96f287447257005af05a4127 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 25 Mar 2022 18:10:31 +0100 Subject: [PATCH 3/4] Change lake_surrounders,continent_sizes,ocean_sizes to a vector --- server/generator/mapgen_utils.cpp | 34 ++++++++----------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/server/generator/mapgen_utils.cpp b/server/generator/mapgen_utils.cpp index 5fd0acfb4d..286a7ddf9e 100644 --- a/server/generator/mapgen_utils.cpp +++ b/server/generator/mapgen_utils.cpp @@ -233,20 +233,16 @@ void smooth_int_map(int *int_map, bool zeroes_at_edges) * The _sizes arrays give the sizes (in tiles) of each continent and * ocean. */ -static Continent_id *lake_surrounders = nullptr; -static int *continent_sizes = nullptr; -static int *ocean_sizes = nullptr; +std::vector lake_surrounders; +std::vector continent_sizes; +std::vector ocean_sizes; /** Calculate lake_surrounders[] array */ static void recalculate_lake_surrounders() { - const size_t size = (wld.map.num_oceans + 1) * sizeof(*lake_surrounders); - - lake_surrounders = - static_cast(fc_realloc(lake_surrounders, size)); - memset(lake_surrounders, 0, size); + lake_surrounders = std::vector(wld.map.num_oceans + 1, 0); whole_map_iterate(&(wld.map), ptile) { @@ -476,15 +472,12 @@ void assign_continent_numbers() if (terrain_type_terrain_class(pterrain) != TC_OCEAN) { wld.map.num_continents++; - continent_sizes = static_cast( - fc_realloc(continent_sizes, (wld.map.num_continents + 1) - * sizeof(*continent_sizes))); + continent_sizes = std::vector(wld.map.num_continents + 1); continent_sizes[wld.map.num_continents] = 0; assign_continent_flood(ptile, true, wld.map.num_continents); } else { wld.map.num_oceans++; - ocean_sizes = static_cast(fc_realloc( - ocean_sizes, (wld.map.num_oceans + 1) * sizeof(*ocean_sizes))); + ocean_sizes = std::vector(wld.map.num_oceans + 1); ocean_sizes[wld.map.num_oceans] = 0; assign_continent_flood(ptile, false, -wld.map.num_oceans); } @@ -678,18 +671,9 @@ void smooth_water_depth() */ void generator_free() { - if (lake_surrounders != nullptr) { - free(lake_surrounders); - lake_surrounders = nullptr; - } - if (continent_sizes != nullptr) { - free(continent_sizes); - continent_sizes = nullptr; - } - if (ocean_sizes != nullptr) { - free(ocean_sizes); - ocean_sizes = nullptr; - } + lake_surrounders.clear(); + continent_sizes.clear(); + ocean_sizes.clear(); } /** From b87a880c9da031789076afacabed65640b75cfb7 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 25 Mar 2022 19:43:37 +0100 Subject: [PATCH 4/4] make spec const in citymodel::headerData We are only reading from spec and not modifying it. --- client/gui-qt/cityrep.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/gui-qt/cityrep.cpp b/client/gui-qt/cityrep.cpp index 8a717f652c..4a9071251a 100644 --- a/client/gui-qt/cityrep.cpp +++ b/client/gui-qt/cityrep.cpp @@ -219,7 +219,7 @@ bool city_model::setData(const QModelIndex &index, const QVariant &value, QVariant city_model::headerData(int section, Qt::Orientation orientation, int role) const { - struct city_report_spec spec = city_report_specs[section]; + const auto &spec = city_report_specs[section]; if (orientation == Qt::Horizontal && section < NUM_CREPORT_COLS) { if (role == Qt::DisplayRole) {