Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begone realloc #966

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions ai/aitraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ void ai_traits_init(struct player *pplayer)
{
enum trait tr;

pplayer->ai_common.traits = static_cast<ai_trait *>(fc_realloc(
pplayer->ai_common.traits, sizeof(struct ai_trait) * TRAIT_COUNT));
pplayer->ai_common.traits = std::vector<ai_trait>(TRAIT_COUNT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be ever resized. TRAIT_COUNT is 3 and ai_trait is just two ints. Would this make more sense as a std::array<ai_trait, TRAIT_COUNT>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering the same, also we have waay too much ram these days to have a lot more traits still in stack, Ended up doing the same in heap because I thought the difference would be negligible, and array didnt have clear.


for (tr = trait_begin(); tr != trait_end(); tr = trait_next(tr)) {
int min = pplayer->nation->server.traits[tr].min;
Expand All @@ -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();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions client/cityrepdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_spec> city_report_specs;
static int num_creport_cols;

/**
Expand Down Expand Up @@ -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<city_report_spec *>(fc_realloc(
city_report_specs, num_creport_cols * sizeof(*city_report_specs)));
city_report_specs = std::vector<city_report_spec>(num_creport_cols);
p = &city_report_specs[0];

fc_snprintf(sp_explanations, sizeof(sp_explanations), "%s",
Expand Down
2 changes: 1 addition & 1 deletion client/cityrepdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_spec> city_report_specs;

int num_city_report_spec();
bool *city_report_spec_show_ptr(int i);
Expand Down
43 changes: 21 additions & 22 deletions client/gui-qt/cityrep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -138,16 +138,16 @@ 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);
}
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();
}

Expand Down Expand Up @@ -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;
const auto &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;
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -1117,16 +1116,16 @@ 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;
}

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);
}
Expand Down
2 changes: 1 addition & 1 deletion common/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct player_ai {

int love[MAX_NUM_PLAYER_SLOTS];

struct ai_trait *traits;
std::vector<ai_trait> traits;
};

/* Diplomatic states (how one player views another).
Expand Down
34 changes: 9 additions & 25 deletions server/generator/mapgen_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Continent_id> lake_surrounders;
std::vector<int> continent_sizes;
std::vector<int> 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<Continent_id *>(fc_realloc(lake_surrounders, size));
memset(lake_surrounders, 0, size);
lake_surrounders = std::vector<Continent_id>(wld.map.num_oceans + 1, 0);

whole_map_iterate(&(wld.map), ptile)
{
Expand Down Expand Up @@ -476,15 +472,12 @@ void assign_continent_numbers()

if (terrain_type_terrain_class(pterrain) != TC_OCEAN) {
wld.map.num_continents++;
continent_sizes = static_cast<int *>(
fc_realloc(continent_sizes, (wld.map.num_continents + 1)
* sizeof(*continent_sizes)));
continent_sizes = std::vector<int>(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<int *>(fc_realloc(
ocean_sizes, (wld.map.num_oceans + 1) * sizeof(*ocean_sizes)));
ocean_sizes = std::vector<int>(wld.map.num_oceans + 1);
ocean_sizes[wld.map.num_oceans] = 0;
assign_continent_flood(ptile, false, -wld.map.num_oceans);
}
Expand Down Expand Up @@ -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();
}

/**
Expand Down