From f5b11daffd6f240268ce78d72c64be43670988ea Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 16 Aug 2018 13:35:14 +0200 Subject: 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 --- core/dive.c | 30 +++++++++++++++++------------- core/dive.h | 8 ++++++-- core/divelist.c | 6 +++--- core/profile.c | 36 +++++++++++++++++++----------------- core/save-git.c | 4 ++-- core/save-xml.c | 4 ++-- 6 files changed, 49 insertions(+), 39 deletions(-) (limited to 'core') 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"); } -- cgit v1.2.3-70-g09d2