summaryrefslogtreecommitdiffstats
path: root/core/statistics.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-08-04 18:44:57 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-11-09 19:19:04 +0100
commit7c9f46acd202121e67557bb634961ef17a9f6c1f (patch)
treef21b6ff42a930cc97c31db9f20c03f1613e70291 /core/statistics.c
parentcd4f66014f7b5269dff9e088c9d7d38241c03156 (diff)
downloadsubsurface-7c9f46acd202121e67557bb634961ef17a9f6c1f.tar.gz
Core: remove MAX_CYLINDERS restriction
Instead of using fixed size arrays, use a new cylinder_table structure. The code copies the weightsystem code, but is significantly more complex because cylinders are such an integral part of the core. Two functions to access the cylinders were added: get_cylinder() and get_or_create_cylinder() The former does a simple array access and supposes that the cylinder exists. The latter is used by the parser(s) and if a cylinder with the given id does not exist, cylinders up to that id are generated. One point will make C programmers cringe: the cylinder structure is passed by value. This is due to the way the table-macros work. A refactoring of the table macros is planned. It has to be noted that the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit architecture, so passing on the stack is probably not even significantly slower than passing as reference. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/statistics.c')
-rw-r--r--core/statistics.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/core/statistics.c b/core/statistics.c
index d5ef58955..fa7985c90 100644
--- a/core/statistics.c
+++ b/core/statistics.c
@@ -330,13 +330,15 @@ bool has_gaschange_event(const struct dive *dive, const struct divecomputer *dc,
bool is_cylinder_used(const struct dive *dive, int idx)
{
const struct divecomputer *dc;
- if (cylinder_none(&dive->cylinder[idx]))
+ cylinder_t *cyl;
+ if (idx < 0 || idx >= dive->cylinders.nr)
return false;
- if ((dive->cylinder[idx].start.mbar - dive->cylinder[idx].end.mbar) > SOME_GAS)
+ cyl = &dive->cylinders.cylinders[idx];
+ if ((cyl->start.mbar - cyl->end.mbar) > SOME_GAS)
return true;
- if ((dive->cylinder[idx].sample_start.mbar - dive->cylinder[idx].sample_end.mbar) > SOME_GAS)
+ if ((cyl->sample_start.mbar - cyl->sample_end.mbar) > SOME_GAS)
return true;
for_each_dc(dive, dc) {
@@ -349,7 +351,7 @@ bool is_cylinder_used(const struct dive *dive, int idx)
bool is_cylinder_prot(const struct dive *dive, int idx)
{
const struct divecomputer *dc;
- if (cylinder_none(&dive->cylinder[idx]))
+ if (idx < 0 || idx >= dive->cylinders.nr)
return false;
for_each_dc(dive, dc) {
@@ -359,15 +361,15 @@ bool is_cylinder_prot(const struct dive *dive, int idx)
return false;
}
-/* Returns a dynamically allocated array with MAX_CYLINDERS entries that
- * has to be freed by the caller */
+/* Returns a dynamically allocated array with dive->cylinders.nr entries,
+ * which 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];
+ volume_t *gases = malloc(dive->cylinders.nr * sizeof(volume_t));
+ for (idx = 0; idx < dive->cylinders.nr; idx++) {
+ cylinder_t *cyl = &dive->cylinders.cylinders[idx];
pressure_t start, end;
start = cyl->start.mbar ? cyl->start : cyl->sample_start;
@@ -403,10 +405,10 @@ void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot)
if (!d->selected)
continue;
volume_t *diveGases = get_gas_used(d);
- for (j = 0; j < MAX_CYLINDERS; j++) {
+ for (j = 0; j < d->cylinders.nr; j++) {
if (diveGases[j].mliter) {
volume_t o2 = {}, he = {};
- get_gas_parts(d->cylinder[j].gasmix, diveGases[j], O2_IN_AIR, &o2, &he);
+ get_gas_parts(d->cylinders.cylinders[j].gasmix, diveGases[j], O2_IN_AIR, &o2, &he);
o2_tot->mliter += o2.mliter;
he_tot->mliter += he.mliter;
}