Core: dynamically allocate the result of get_gas_used()

get_gas_used() returns the volume of used gases. Currently,
an array with MAX_CYLINDERS is passed in. If we want to make the
number of cylinders dynamic, the function must use an arbitrarilly
sized array.

Therefore, return a dynamically allocated array and free it
in the caller.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-06-27 07:42:09 +02:00 committed by Dirk Hohndel
parent f179ec033f
commit 11467fa326
5 changed files with 16 additions and 10 deletions

View file

@ -388,13 +388,14 @@ QVector<QPair<QString, int>> selectedDivesGasUsed()
for_each_dive (i, d) { for_each_dive (i, d) {
if (!d->selected) if (!d->selected)
continue; continue;
volume_t diveGases[MAX_CYLINDERS] = {}; volume_t *diveGases = get_gas_used(d);
get_gas_used(d, diveGases); for (j = 0; j < MAX_CYLINDERS; j++) {
for (j = 0; j < MAX_CYLINDERS; j++)
if (diveGases[j].mliter) { if (diveGases[j].mliter) {
QString gasName = gasname(d->cylinder[j].gasmix); QString gasName = gasname(d->cylinder[j].gasmix);
gasUsed[gasName] += diveGases[j].mliter; gasUsed[gasName] += diveGases[j].mliter;
} }
}
free(diveGases);
} }
QVector<QPair<QString, int>> gasUsedOrdered; QVector<QPair<QString, int>> gasUsedOrdered;
gasUsedOrdered.reserve(gasUsed.size()); gasUsedOrdered.reserve(gasUsed.size());

View file

@ -359,10 +359,13 @@ bool is_cylinder_prot(const struct dive *dive, int idx)
return false; return false;
} }
void get_gas_used(struct dive *dive, volume_t gases[MAX_CYLINDERS]) /* Returns a dynamically allocated array with MAX_CYLINDERS entries that
* has to be freed by the caller */
volume_t *get_gas_used(struct dive *dive)
{ {
int idx; int idx;
volume_t *gases = malloc(MAX_CYLINDERS * sizeof(volume_t));
for (idx = 0; idx < MAX_CYLINDERS; idx++) { for (idx = 0; idx < MAX_CYLINDERS; idx++) {
cylinder_t *cyl = &dive->cylinder[idx]; cylinder_t *cyl = &dive->cylinder[idx];
pressure_t start, end; pressure_t start, end;
@ -372,6 +375,8 @@ void get_gas_used(struct dive *dive, volume_t gases[MAX_CYLINDERS])
if (end.mbar && start.mbar > end.mbar) if (end.mbar && start.mbar > end.mbar)
gases[idx].mliter = gas_volume(cyl, start) - gas_volume(cyl, end); gases[idx].mliter = gas_volume(cyl, start) - gas_volume(cyl, end);
} }
return gases;
} }
/* Quite crude reverse-blender-function, but it produces a approx result */ /* Quite crude reverse-blender-function, but it produces a approx result */
@ -397,8 +402,7 @@ void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot)
for_each_dive (i, d) { for_each_dive (i, d) {
if (!d->selected) if (!d->selected)
continue; continue;
volume_t diveGases[MAX_CYLINDERS] = {}; volume_t *diveGases = get_gas_used(d);
get_gas_used(d, diveGases);
for (j = 0; j < MAX_CYLINDERS; j++) { for (j = 0; j < MAX_CYLINDERS; j++) {
if (diveGases[j].mliter) { if (diveGases[j].mliter) {
volume_t o2 = {}, he = {}; volume_t o2 = {}, he = {};
@ -407,5 +411,6 @@ void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot)
he_tot->mliter += he.mliter; he_tot->mliter += he.mliter;
} }
} }
free(diveGases);
} }
} }

View file

@ -10,7 +10,6 @@
#define STATISTICS_H #define STATISTICS_H
#include "core/units.h" #include "core/units.h"
#include "core/dive.h" // For MAX_CYLINDERS
#define STATS_MAX_DEPTH 300 /* Max depth for stats bucket is 300m */ #define STATS_MAX_DEPTH 300 /* Max depth for stats bucket is 300m */
#define STATS_DEPTH_BUCKET 10 /* Size of buckets for depth range */ #define STATS_DEPTH_BUCKET 10 /* Size of buckets for depth range */
@ -62,7 +61,7 @@ extern void init_stats_summary(struct stats_summary *stats);
extern void free_stats_summary(struct stats_summary *stats); extern void free_stats_summary(struct stats_summary *stats);
extern void calculate_stats_summary(struct stats_summary *stats, bool selected_only); extern void calculate_stats_summary(struct stats_summary *stats, bool selected_only);
extern void calculate_stats_selected(stats_t *stats_selection); extern void calculate_stats_selected(stats_t *stats_selection);
extern void get_gas_used(struct dive *dive, volume_t gases[MAX_CYLINDERS]); extern volume_t *get_gas_used(struct dive *dive);
extern void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot); extern void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -53,8 +53,7 @@ void TabDiveInformation::updateProfile()
ui->maximumDepthText->setText(get_depth_string(current_dive->maxdepth, true)); ui->maximumDepthText->setText(get_depth_string(current_dive->maxdepth, true));
ui->averageDepthText->setText(get_depth_string(current_dive->meandepth, true)); ui->averageDepthText->setText(get_depth_string(current_dive->meandepth, true));
volume_t gases[MAX_CYLINDERS] = {}; volume_t *gases = get_gas_used(current_dive);
get_gas_used(current_dive, gases);
QString volumes; QString volumes;
int mean[MAX_CYLINDERS], duration[MAX_CYLINDERS]; int mean[MAX_CYLINDERS], duration[MAX_CYLINDERS];
per_cylinder_mean_depth(current_dive, select_dc(current_dive), mean, duration); per_cylinder_mean_depth(current_dive, select_dc(current_dive), mean, duration);
@ -76,6 +75,7 @@ void TabDiveInformation::updateProfile()
SACs.append(get_volume_string(sac, true).append(tr("/min"))); SACs.append(get_volume_string(sac, true).append(tr("/min")));
} }
} }
free(gases);
ui->gasUsedText->setText(volumes); ui->gasUsedText->setText(volumes);
ui->oxygenHeliumText->setText(gaslist); ui->oxygenHeliumText->setText(gaslist);

View file

@ -3,6 +3,7 @@
#include "core/qthelper.h" #include "core/qthelper.h"
#include "core/metrics.h" #include "core/metrics.h"
#include "core/statistics.h" #include "core/statistics.h"
#include "core/dive.h" // For NUM_DIVEMODE
class YearStatisticsItem : public TreeItem { class YearStatisticsItem : public TreeItem {
Q_DECLARE_TR_FUNCTIONS(YearStatisticsItem) Q_DECLARE_TR_FUNCTIONS(YearStatisticsItem)