diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-08-16 13:35:14 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-08-23 05:16:38 -0700 |
commit | f5b11daffd6f240268ce78d72c64be43670988ea (patch) | |
tree | 99805cc176483e5f069a7920c11528e37cf0db8c | |
parent | b6187f73aad09adb0b70af8d77f1af1a34a338f8 (diff) | |
download | subsurface-f5b11daffd6f240268ce78d72c64be43670988ea.tar.gz |
Cleanup: return gasmix by value
Currently, get_gasmix_from_event() and get_gasmix() return pointers
to either static or to (possibly changing) dive data. This seems like
a dangerous practice and the returned data should be used immediately.
Instead, return the gasmix by value. This is in preparation of
const-ifying input parameters of a number of core functions, which
will ultimately let the merge() function take const-arguments in
preparation of undo of dive-merging.
On common 64-bit systems gasmix (two "int"s) is the size of a pointer
and can be returned in a register.
On 32-bit systems a pointer to the struct to be filled out will be
passed.
Since get_gasmix() now returns a value, the first invocation is
tested by a NULL-initialized "struct event *". Document this in
a comment.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/dive.c | 30 | ||||
-rw-r--r-- | core/dive.h | 8 | ||||
-rw-r--r-- | core/divelist.c | 6 | ||||
-rw-r--r-- | core/profile.c | 36 | ||||
-rw-r--r-- | core/save-git.c | 4 | ||||
-rw-r--r-- | core/save-xml.c | 4 | ||||
-rw-r--r-- | profile-widget/diveeventitem.cpp | 18 | ||||
-rw-r--r-- | profile-widget/diveprofileitem.cpp | 6 | ||||
-rw-r--r-- | profile-widget/profilewidget2.cpp | 2 | ||||
-rw-r--r-- | profile-widget/tankitem.cpp | 6 |
10 files changed, 65 insertions, 55 deletions
diff --git a/core/dive.c b/core/dive.c index da4b19346..6d9945932 100644 --- a/core/dive.c +++ b/core/dive.c @@ -270,17 +270,16 @@ enum divemode_t get_current_divemode(struct divecomputer *dc, int time, struct e return *divemode; } -/* this returns a pointer to static variable - so use it right away after calling */ -struct gasmix *get_gasmix_from_event(struct dive *dive, struct event *ev) +struct gasmix get_gasmix_from_event(struct dive *dive, struct event *ev) { - static struct gasmix dummy; + struct gasmix dummy = { 0 }; if (ev && event_is_gaschange(ev)) { int index = ev->gas.index; if (index >= 0 && index < MAX_CYLINDERS) - return &dive->cylinder[index].gasmix; - return &ev->gas.mix; + return dive->cylinder[index].gasmix; + return ev->gas.mix; } - return &dummy; + return dummy; } int get_pressure_units(int mb, const char **units) @@ -1073,7 +1072,7 @@ void update_setpoint_events(struct dive *dive, struct divecomputer *dc) // So we make sure, this comes from a Predator or Petrel and we only remove // pO2 values we would have computed anyway. struct event *ev = get_next_event(dc->events, "gaschange"); - struct gasmix *gasmix = get_gasmix_from_event(dive, ev); + struct gasmix gasmix = get_gasmix_from_event(dive, ev); struct event *next = get_next_event(ev, "gaschange"); for (int i = 0; i < dc->samples; i++) { @@ -1083,7 +1082,7 @@ void update_setpoint_events(struct dive *dive, struct divecomputer *dc) gasmix = get_gasmix_from_event(dive, ev); next = get_next_event(ev, "gaschange"); } - fill_pressures(&pressures, calculate_depth_to_mbar(dc->sample[i].depth.mm, dc->surface_pressure, 0), gasmix ,0, dc->divemode); + fill_pressures(&pressures, calculate_depth_to_mbar(dc->sample[i].depth.mm, dc->surface_pressure, 0), &gasmix ,0, dc->divemode); if (abs(dc->sample[i].setpoint.mbar - (int)(1000 * pressures.o2)) <= 50) dc->sample[i].setpoint.mbar = 0; } @@ -4288,19 +4287,24 @@ int dive_has_gps_location(struct dive *dive) return dive_site_has_gps_location(get_dive_site_by_uuid(dive->dive_site_uuid)); } -struct gasmix *get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix) +struct gasmix get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix) { struct event *ev = *evp; + struct gasmix res; - if (!gasmix) { + if (!ev) { + /* on first invocation, get initial gas mix and first event (if any) */ int cyl = explicit_first_cylinder(dive, dc); - gasmix = &dive->cylinder[cyl].gasmix; + res = dive->cylinder[cyl].gasmix; ev = dc ? get_next_event(dc->events, "gaschange") : NULL; + } else { + res = *gasmix; } + while (ev && ev->time.seconds < time) { - gasmix = get_gasmix_from_event(dive, ev); + res = get_gasmix_from_event(dive, ev); ev = get_next_event(ev->next, "gaschange"); } *evp = ev; - return gasmix; + return res; } diff --git a/core/dive.h b/core/dive.h index 98ac06fdc..7e62262d9 100644 --- a/core/dive.h +++ b/core/dive.h @@ -585,7 +585,7 @@ extern void update_event_name(struct dive *d, struct event* event, const char *n extern void add_extra_data(struct divecomputer *dc, const char *key, const char *value); extern void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *mean, int *duration); extern int get_cylinder_index(struct dive *dive, struct event *ev); -extern struct gasmix *get_gasmix_from_event(struct dive *, struct event *ev); +extern struct gasmix get_gasmix_from_event(struct dive *, struct event *ev); extern int nr_cylinders(struct dive *dive); extern int nr_weightsystems(struct dive *dive); @@ -718,7 +718,11 @@ extern void printdecotable(struct decostop *table); extern struct event *get_next_event(struct event *event, const char *name); -extern struct gasmix *get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix); +/* Get gasmix at increasing timestamps. + * In "evp", pass a pointer to a "struct event *" which is NULL-initialized on first invocation. + * On subsequent calls, pass the same "evp" and the "gasmix" from previous calls. + */ +extern struct gasmix get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix); /* these structs holds the information that * describes the cylinders / weight systems. diff --git a/core/divelist.c b/core/divelist.c index b81e89d25..9c2484417 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -404,7 +404,7 @@ static int calculate_sac(struct dive *dive) static void add_dive_to_deco(struct deco_state *ds, struct dive *dive) { struct divecomputer *dc = &dive->dc; - struct gasmix *gasmix = NULL; + struct gasmix gasmix = { 0 }; int i; struct event *ev = NULL, *evd = NULL; enum divemode_t current_divemode = UNDEF_COMP_TYPE; @@ -421,8 +421,8 @@ static void add_dive_to_deco(struct deco_state *ds, struct dive *dive) for (j = t0; j < t1; j++) { int depth = interpolate(psample->depth.mm, sample->depth.mm, j - t0, t1 - t0); - gasmix = get_gasmix(dive, dc, j, &ev, gasmix); - add_segment(ds, depth_to_bar(depth, dive), gasmix, 1, sample->setpoint.mbar, + gasmix = get_gasmix(dive, dc, j, &ev, &gasmix); + add_segment(ds, depth_to_bar(depth, dive), &gasmix, 1, sample->setpoint.mbar, get_current_divemode(&dive->dc, j, &evd, ¤t_divemode), dive->sac); } } diff --git a/core/profile.c b/core/profile.c index 113f62845..56cc85aac 100644 --- a/core/profile.c +++ b/core/profile.c @@ -319,7 +319,7 @@ struct plot_info *analyze_plot_info(struct plot_info *pi) int get_cylinder_index(struct dive *dive, struct event *ev) { int best; - struct gasmix *mix; + struct gasmix mix; if (ev->gas.index >= 0) return ev->gas.index; @@ -333,7 +333,7 @@ int get_cylinder_index(struct dive *dive, struct event *ev) fprintf(stderr, "Still looking up cylinder based on gas mix in get_cylinder_index()!\n"); mix = get_gasmix_from_event(dive, ev); - best = find_best_gasmix_match(mix, dive->cylinder, 0); + best = find_best_gasmix_match(&mix, dive->cylinder, 0); return best < 0 ? 0 : best; } @@ -775,16 +775,16 @@ static unsigned int matching_gases(struct dive *dive, struct gasmix *gasmix) static void calculate_sac(struct dive *dive, struct divecomputer *dc, struct plot_info *pi) { - struct gasmix *gasmix = NULL; + struct gasmix gasmix = { 0 }; struct event *ev = NULL; unsigned int gases = 0; for (int i = 0; i < pi->nr; i++) { struct plot_data *entry = pi->entry + i; - struct gasmix *newmix = get_gasmix(dive, dc, entry->sec, &ev, gasmix); - if (newmix != gasmix) { + struct gasmix newmix = get_gasmix(dive, dc, entry->sec, &ev, &gasmix); + if (!same_gasmix(&newmix, &gasmix)) { gasmix = newmix; - gases = matching_gases(dive, newmix); + gases = matching_gases(dive, &newmix); } fill_sac(dive, pi, i, gases); @@ -1016,7 +1016,7 @@ void calculate_deco_information(struct deco_state *ds, struct deco_state *planne int last_ndl_tts_calc_time = 0, first_ceiling = 0, current_ceiling, last_ceiling = 0, final_tts = 0 , time_clear_ceiling = 0; if (decoMode() == VPMB) ds->first_ceiling_pressure.mbar = depth_to_mbar(first_ceiling, dive); - struct gasmix *gasmix = NULL; + struct gasmix gasmix = { 0 }; struct event *ev = NULL, *evd = NULL; enum divemode_t current_divemode = UNDEF_COMP_TYPE; @@ -1026,7 +1026,7 @@ void calculate_deco_information(struct deco_state *ds, struct deco_state *planne int time_stepsize = 20; current_divemode = get_current_divemode(dc, entry->sec, &evd, ¤t_divemode); - gasmix = get_gasmix(dive, dc, t1, &ev, gasmix); + gasmix = get_gasmix(dive, dc, t1, &ev, &gasmix); entry->ambpressure = depth_to_bar(entry->depth, dive); entry->gfline = get_gf(ds, entry->ambpressure, dive) * (100.0 - AMB_PERCENTAGE) + AMB_PERCENTAGE; if (t0 > t1) { @@ -1040,7 +1040,7 @@ void calculate_deco_information(struct deco_state *ds, struct deco_state *planne for (j = t0 + time_stepsize; j <= t1; j += time_stepsize) { int depth = interpolate(entry[-1].depth, entry[0].depth, j - t0, t1 - t0); add_segment(ds, depth_to_bar(depth, dive), - gasmix, time_stepsize, entry->o2pressure.mbar, current_divemode, entry->sac); + &gasmix, time_stepsize, entry->o2pressure.mbar, current_divemode, entry->sac); entry->icd_warning = ds->icd_warning; if ((t1 - j < time_stepsize) && (j < t1)) time_stepsize = t1 - j; @@ -1115,7 +1115,7 @@ void calculate_deco_information(struct deco_state *ds, struct deco_state *planne /* We are going to mess up deco state, so store it for later restore */ struct deco_state *cache_data = NULL; cache_deco_state(ds, &cache_data); - calculate_ndl_tts(ds, dive, entry, gasmix, surface_pressure, current_divemode); + calculate_ndl_tts(ds, dive, entry, &gasmix, surface_pressure, current_divemode); if (decoMode() == VPMB && !in_planner() && i == pi->nr - 1) final_tts = entry->tts_calc; /* Restore "real" deco state for next real time step */ @@ -1205,7 +1205,7 @@ static void calculate_gas_information_new(struct dive *dive, struct divecomputer { int i; double amb_pressure; - struct gasmix *gasmix = NULL; + struct gasmix gasmix = { 0 }; struct event *evg = NULL, *evd = NULL; enum divemode_t current_divemode = UNDEF_COMP_TYPE; @@ -1213,21 +1213,23 @@ static void calculate_gas_information_new(struct dive *dive, struct divecomputer int fn2, fhe; struct plot_data *entry = pi->entry + i; - gasmix = get_gasmix(dive, dc, entry->sec, &evg, gasmix); + gasmix = get_gasmix(dive, dc, entry->sec, &evg, &gasmix); amb_pressure = depth_to_bar(entry->depth, dive); current_divemode = get_current_divemode(dc, entry->sec, &evd, ¤t_divemode); - fill_pressures(&entry->pressures, amb_pressure, gasmix, (current_divemode == OC) ? 0.0 : entry->o2pressure.mbar / 1000.0, current_divemode); + fill_pressures(&entry->pressures, amb_pressure, &gasmix, (current_divemode == OC) ? 0.0 : entry->o2pressure.mbar / 1000.0, current_divemode); fn2 = (int)(1000.0 * entry->pressures.n2 / amb_pressure); fhe = (int)(1000.0 * entry->pressures.he / amb_pressure); - if (dc->divemode == PSCR) // OC pO2 is calulated for PSCR with or without external PO2 monitoring. - entry->scr_OC_pO2.mbar = (int) depth_to_mbar(entry->depth, dive) * get_o2(get_gasmix(dive, dc, entry->sec, &evg, gasmix)) / 1000; + if (dc->divemode == PSCR) { // OC pO2 is calulated for PSCR with or without external PO2 monitoring. + struct gasmix gasmix2 = get_gasmix(dive, dc, entry->sec, &evg, &gasmix); + entry->scr_OC_pO2.mbar = (int) depth_to_mbar(entry->depth, dive) * get_o2(&gasmix2) / 1000; + } /* Calculate MOD, EAD, END and EADD based on partial pressures calculated before * so there is no difference in calculating between OC and CC * END takes O₂ + N₂ (air) into account ("Narcotic" for trimix dives) * EAD just uses N₂ ("Air" for nitrox dives) */ pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) }; - entry->mod = (double)gas_mod(gasmix, modpO2, dive, 1).mm; + entry->mod = (double)gas_mod(&gasmix, modpO2, dive, 1).mm; entry->end = (entry->depth + 10000) * (1000 - fhe) / 1000.0 - 10000; entry->ead = (entry->depth + 10000) * fn2 / (double)N2_IN_AIR - 10000; entry->eadd = (entry->depth + 10000) * @@ -1235,7 +1237,7 @@ static void calculate_gas_information_new(struct dive *dive, struct divecomputer entry->pressures.n2 / amb_pressure * N2_DENSITY + entry->pressures.he / amb_pressure * HE_DENSITY) / (O2_IN_AIR * O2_DENSITY + N2_IN_AIR * N2_DENSITY) * 1000 - 10000; - entry->density = gas_density(gasmix, depth_to_mbar(entry->depth, dive)); + entry->density = gas_density(&gasmix, depth_to_mbar(entry->depth, dive)); if (entry->mod < 0) entry->mod = 0; if (entry->ead < 0) diff --git a/core/save-git.c b/core/save-git.c index fcfbd93d1..7717a4b5f 100644 --- a/core/save-git.c +++ b/core/save-git.c @@ -382,10 +382,10 @@ static void save_one_event(struct membuffer *b, struct dive *dive, struct event show_index(b, ev->value, "value=", ""); show_utf8(b, " name=", ev->name, ""); if (event_is_gaschange(ev)) { - struct gasmix *mix = get_gasmix_from_event(dive, ev); + struct gasmix mix = get_gasmix_from_event(dive, ev); if (ev->gas.index >= 0) show_integer(b, ev->gas.index, "cylinder=", ""); - put_gasmix(b, mix); + put_gasmix(b, &mix); } put_string(b, "\n"); } diff --git a/core/save-xml.c b/core/save-xml.c index e3af000a4..7e2328e5c 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -307,10 +307,10 @@ static void save_one_event(struct membuffer *b, struct dive *dive, struct event show_index(b, ev->value, "value='", "'"); show_utf8(b, ev->name, " name='", "'", 1); if (event_is_gaschange(ev)) { - struct gasmix *mix = get_gasmix_from_event(dive, ev); + struct gasmix mix = get_gasmix_from_event(dive, ev); if (ev->gas.index >= 0) show_integer(b, ev->gas.index, "cylinder='", "'"); - put_gasmix(b, mix); + put_gasmix(b, &mix); } put_format(b, " />\n"); } diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index 13ab3770b..875954b37 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -92,20 +92,20 @@ void DiveEventItem::setupPixmap(struct gasmix *lastgasmix) } else if (internalEvent->type == SAMPLE_EVENT_BOOKMARK) { setPixmap(EVENT_PIXMAP(":dive-bookmark-icon")); } else if (event_is_gaschange(internalEvent)) { - struct gasmix *mix = get_gasmix_from_event(&displayed_dive, internalEvent); + struct gasmix mix = get_gasmix_from_event(&displayed_dive, internalEvent); struct icd_data icd_data; - bool icd = isobaric_counterdiffusion(lastgasmix, mix, &icd_data); - if (mix->he.permille) { + bool icd = isobaric_counterdiffusion(lastgasmix, &mix, &icd_data); + if (mix.he.permille) { if (icd) setPixmap(EVENT_PIXMAP_BIGGER(":gaschange-trimix-ICD-icon")); else setPixmap(EVENT_PIXMAP_BIGGER(":gaschange-trimix-icon")); - } else if (gasmix_is_air(mix)) { + } else if (gasmix_is_air(&mix)) { if (icd) setPixmap(EVENT_PIXMAP_BIGGER(":gaschange-air-ICD-icon")); else setPixmap(EVENT_PIXMAP_BIGGER(":gaschange-air-icon")); - } else if (mix->o2.permille == 1000) { + } else if (mix.o2.permille == 1000) { if (icd) setPixmap(EVENT_PIXMAP_BIGGER(":gaschange-oxygen-ICD-icon")); else @@ -174,15 +174,15 @@ void DiveEventItem::setupToolTipString(struct gasmix *lastgasmix) if (event_is_gaschange(internalEvent)) { struct icd_data icd_data; - struct gasmix *mix = get_gasmix_from_event(&displayed_dive, internalEvent); + struct gasmix mix = get_gasmix_from_event(&displayed_dive, internalEvent); struct membuffer mb = {}; name += ": "; - name += gasname(mix); + name += gasname(&mix); /* Do we have an explicit cylinder index? Show it. */ if (internalEvent->gas.index >= 0) name += tr(" (cyl. %1)").arg(internalEvent->gas.index + 1); - bool icd = isobaric_counterdiffusion(lastgasmix, mix, &icd_data); + bool icd = isobaric_counterdiffusion(lastgasmix, &mix, &icd_data); if (icd_data.dHe < 0) { put_format(&mb, "\n%s %s:%+.3g%% %s:%+.3g%%%s%+.3g%%", qPrintable(tr("ICD")), @@ -192,7 +192,7 @@ void DiveEventItem::setupToolTipString(struct gasmix *lastgasmix) name += QString::fromUtf8(mb.buffer, mb.len); free_buffer(&mb); } - *lastgasmix = *mix; + *lastgasmix = mix; } else if (same_string(internalEvent->name, "modechange")) { name += QString(": %1").arg(gettextFromC::tr(divemode_text_ui[internalEvent->value])); } else if (value) { diff --git a/profile-widget/diveprofileitem.cpp b/profile-widget/diveprofileitem.cpp index bedefb97e..94fb787b1 100644 --- a/profile-widget/diveprofileitem.cpp +++ b/profile-widget/diveprofileitem.cpp @@ -410,11 +410,11 @@ void DivePercentageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem for (int i = 1, modelDataCount = dataModel->rowCount(); i < modelDataCount; i++) { if (i < poly.count()) { double value = dataModel->index(i, vDataColumn).data().toDouble(); - struct gasmix *gasmix = NULL; + struct gasmix gasmix = { 0 }; struct event *ev = NULL; int sec = dataModel->index(i, DivePlotDataModel::TIME).data().toInt(); - gasmix = get_gasmix(&displayed_dive, displayed_dc, sec, &ev, gasmix); - int inert = 1000 - get_o2(gasmix); + gasmix = get_gasmix(&displayed_dive, displayed_dc, sec, &ev, &gasmix); + int inert = 1000 - get_o2(&gasmix); mypen.setBrush(QBrush(ColorScale(value, inert))); painter->setPen(mypen); painter->drawLine(poly[i - 1], poly[i]); diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index cbd63cf40..d9875681a 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -759,7 +759,7 @@ void ProfileWidget2::plotDive(struct dive *d, bool force, bool doClearPictures) eventItems.clear(); struct event *event = currentdc->events; struct event *ev; - struct gasmix lastgasmix = *get_gasmix(&displayed_dive, current_dc, 1, &ev, NULL); + struct gasmix lastgasmix = get_gasmix(&displayed_dive, current_dc, 1, &ev, NULL); while (event) { #ifndef SUBSURFACE_MOBILE diff --git a/profile-widget/tankitem.cpp b/profile-widget/tankitem.cpp index 22e5d3d21..d1adae41c 100644 --- a/profile-widget/tankitem.cpp +++ b/profile-widget/tankitem.cpp @@ -104,7 +104,7 @@ void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&) // start with the first gasmix and at the start of the dive int cyl = explicit_first_cylinder(&displayed_dive, dc); - struct gasmix *gasmix = &displayed_dive.cylinder[cyl].gasmix; + struct gasmix gasmix = displayed_dive.cylinder[cyl].gasmix; int startTime = 0; // work through all the gas changes and add the rectangle for each gas while it was used @@ -112,14 +112,14 @@ void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&) while (ev && (int)ev->time.seconds < last_entry->sec) { width = hAxis->posAtValue(ev->time.seconds) - hAxis->posAtValue(startTime); left = hAxis->posAtValue(startTime); - createBar(left, width, gasmix); + createBar(left, width, &gasmix); startTime = ev->time.seconds; gasmix = get_gasmix_from_event(&displayed_dive, ev); ev = get_next_event(ev->next, "gaschange"); } width = hAxis->posAtValue(last_entry->sec) - hAxis->posAtValue(startTime); left = hAxis->posAtValue(startTime); - createBar(left, width, gasmix); + createBar(left, width, &gasmix); } void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal) |