summaryrefslogtreecommitdiffstats
path: root/core/statistics.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-06-27 07:42:09 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-11-09 19:19:04 +0100
commit11467fa326896934a299712810b74aef6a373c6f (patch)
treed8c599a8b2b5a50e4dd189ff8ec108cac20c007d /core/statistics.c
parentf179ec033f39a6b774e6e6bb79f41771dfaf7e02 (diff)
downloadsubsurface-11467fa326896934a299712810b74aef6a373c6f.tar.gz
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>
Diffstat (limited to 'core/statistics.c')
-rw-r--r--core/statistics.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/core/statistics.c b/core/statistics.c
index 6e4630d64..d5ef58955 100644
--- a/core/statistics.c
+++ b/core/statistics.c
@@ -359,10 +359,13 @@ bool is_cylinder_prot(const struct dive *dive, int idx)
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;
+ volume_t *gases = malloc(MAX_CYLINDERS * sizeof(volume_t));
for (idx = 0; idx < MAX_CYLINDERS; idx++) {
cylinder_t *cyl = &dive->cylinder[idx];
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)
gases[idx].mliter = gas_volume(cyl, start) - gas_volume(cyl, end);
}
+
+ return gases;
}
/* 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) {
if (!d->selected)
continue;
- volume_t diveGases[MAX_CYLINDERS] = {};
- get_gas_used(d, diveGases);
+ volume_t *diveGases = get_gas_used(d);
for (j = 0; j < MAX_CYLINDERS; j++) {
if (diveGases[j].mliter) {
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;
}
}
+ free(diveGases);
}
}