mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
planner: encapsulate deco state cache in a struct
Removes memory management pain: the struct cleans up when it goes out of scope. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
77e8c3655e
commit
f69686d429
7 changed files with 90 additions and 91 deletions
|
|
@ -13,8 +13,6 @@
|
|||
* set_gf() - set Buehlmann gradient factors
|
||||
* set_vpmb_conservatism() - set VPM-B conservatism value
|
||||
* clear_deco()
|
||||
* cache_deco_state()
|
||||
* restore_deco_state()
|
||||
* dump_tissues()
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
|
@ -522,19 +520,17 @@ extern "C" void clear_deco(struct deco_state *ds, double surface_pressure, bool
|
|||
ds->ci_pointing_to_guiding_tissue = -1;
|
||||
}
|
||||
|
||||
extern "C" void cache_deco_state(struct deco_state *src, struct deco_state **cached_datap)
|
||||
void deco_state_cache::cache(const struct deco_state *src)
|
||||
{
|
||||
struct deco_state *data = *cached_datap;
|
||||
|
||||
if (!data) {
|
||||
data = (deco_state *)malloc(sizeof(struct deco_state));
|
||||
*cached_datap = data;
|
||||
}
|
||||
if (!data)
|
||||
data = std::make_unique<deco_state>();
|
||||
*data = *src;
|
||||
}
|
||||
|
||||
extern "C" void restore_deco_state(struct deco_state *data, struct deco_state *target, bool keep_vpmb_state)
|
||||
void deco_state_cache::restore(struct deco_state *target, bool keep_vpmb_state) const
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
if (keep_vpmb_state) {
|
||||
int ci;
|
||||
for (ci = 0; ci < 16; ci++) {
|
||||
|
|
@ -547,7 +543,6 @@ extern "C" void restore_deco_state(struct deco_state *data, struct deco_state *t
|
|||
data->max_bottom_ceiling_pressure = target->max_bottom_ceiling_pressure;
|
||||
}
|
||||
*target = *data;
|
||||
|
||||
}
|
||||
|
||||
extern "C" int deco_allowed_depth(double tissues_tolerance, double surface_pressure, const struct dive *dive, bool smooth)
|
||||
|
|
|
|||
17
core/deco.h
17
core/deco.h
|
|
@ -56,8 +56,6 @@ extern void clear_deco(struct deco_state *ds, double surface_pressure, bool in_p
|
|||
extern void dump_tissues(struct deco_state *ds);
|
||||
extern void set_gf(short gflow, short gfhigh);
|
||||
extern void set_vpmb_conservatism(short conservatism);
|
||||
extern void cache_deco_state(struct deco_state *source, struct deco_state **datap);
|
||||
extern void restore_deco_state(struct deco_state *data, struct deco_state *target, bool keep_vpmb_state);
|
||||
extern void nuclear_regeneration(struct deco_state *ds, double time);
|
||||
extern void vpmb_start_gradient(struct deco_state *ds);
|
||||
extern void vpmb_next_gradient(struct deco_state *ds, double deco_time, double surface_pressure, bool in_planner);
|
||||
|
|
@ -74,6 +72,21 @@ extern void update_regression(struct deco_state *ds, const struct dive *dive);
|
|||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
// C++ only functions
|
||||
|
||||
#include <memory>
|
||||
struct deco_state_cache {
|
||||
// Test if there is cached data
|
||||
operator bool () {
|
||||
return !!data;
|
||||
}
|
||||
void cache(const struct deco_state *source);
|
||||
void restore(struct deco_state *target, bool keep_vpmb_state) const;
|
||||
private:
|
||||
std::unique_ptr<deco_state> data;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif // DECO_H
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ static void interpolate_transition(struct deco_state *ds, struct dive *dive, dur
|
|||
}
|
||||
|
||||
/* returns the tissue tolerance at the end of this (partial) dive */
|
||||
static int tissue_at_end(struct deco_state *ds, struct dive *dive, struct deco_state **cached_datap)
|
||||
static int tissue_at_end(struct deco_state *ds, struct dive *dive, deco_state_cache &cache)
|
||||
{
|
||||
struct divecomputer *dc;
|
||||
struct sample *sample, *psample;
|
||||
|
|
@ -123,11 +123,11 @@ static int tissue_at_end(struct deco_state *ds, struct dive *dive, struct deco_s
|
|||
|
||||
if (!dive)
|
||||
return 0;
|
||||
if (*cached_datap) {
|
||||
restore_deco_state(*cached_datap, ds, true);
|
||||
if (cache) {
|
||||
cache.restore(ds, true);
|
||||
} else {
|
||||
surface_interval = init_decompression(ds, dive, true);
|
||||
cache_deco_state(ds, cached_datap);
|
||||
cache.cache(ds);
|
||||
}
|
||||
dc = &dive->dc;
|
||||
if (!dc->samples)
|
||||
|
|
@ -544,12 +544,12 @@ static bool trial_ascent(struct deco_state *ds, int wait_time, int trial_depth,
|
|||
{
|
||||
|
||||
bool clear_to_ascend = true;
|
||||
struct deco_state *trial_cache = NULL;
|
||||
deco_state_cache trial_cache;
|
||||
|
||||
// For consistency with other VPM-B implementations, we should not start the ascent while the ceiling is
|
||||
// deeper than the next stop (thus the offgasing during the ascent is ignored).
|
||||
// However, we still need to make sure we don't break the ceiling due to on-gassing during ascent.
|
||||
cache_deco_state(ds, &trial_cache);
|
||||
trial_cache.cache(ds);
|
||||
if (wait_time)
|
||||
add_segment(ds, depth_to_bar(trial_depth, dive),
|
||||
gasmix,
|
||||
|
|
@ -558,8 +558,7 @@ static bool trial_ascent(struct deco_state *ds, int wait_time, int trial_depth,
|
|||
double tolerance_limit = tissue_tolerance_calc(ds, dive, depth_to_bar(stoplevel, dive), true);
|
||||
update_regression(ds, dive);
|
||||
if (deco_allowed_depth(tolerance_limit, surface_pressure, dive, 1) > stoplevel) {
|
||||
restore_deco_state(trial_cache, ds, false);
|
||||
free(trial_cache);
|
||||
trial_cache.restore(ds, false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -582,8 +581,7 @@ static bool trial_ascent(struct deco_state *ds, int wait_time, int trial_depth,
|
|||
}
|
||||
trial_depth -= deltad;
|
||||
}
|
||||
restore_deco_state(trial_cache, ds, false);
|
||||
free(trial_cache);
|
||||
trial_cache.restore(ds, false);
|
||||
return clear_to_ascend;
|
||||
}
|
||||
|
||||
|
|
@ -654,7 +652,7 @@ static void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_de
|
|||
*avg_depth = *max_depth = 0;
|
||||
}
|
||||
|
||||
extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, struct deco_state **cached_datap, bool is_planner, bool show_disclaimer)
|
||||
bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer)
|
||||
{
|
||||
|
||||
int bottom_depth;
|
||||
|
|
@ -663,7 +661,7 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
bool is_final_plan = true;
|
||||
int bottom_time;
|
||||
int previous_deco_time;
|
||||
struct deco_state *bottom_cache = NULL;
|
||||
deco_state_cache bottom_cache;
|
||||
struct sample *sample;
|
||||
int po2;
|
||||
int transitiontime, gi;
|
||||
|
|
@ -782,7 +780,7 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
gi = gaschangenr - 1;
|
||||
|
||||
/* Set tissue tolerance and initial vpmb gradient at start of ascent phase */
|
||||
diveplan->surface_interval = tissue_at_end(ds, dive, cached_datap);
|
||||
diveplan->surface_interval = tissue_at_end(ds, dive, cache);
|
||||
nuclear_regeneration(ds, clock);
|
||||
vpmb_start_gradient(ds);
|
||||
if (decoMode(true) == RECREATIONAL) {
|
||||
|
|
@ -850,7 +848,7 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
}
|
||||
|
||||
// VPM-B or Buehlmann Deco
|
||||
tissue_at_end(ds, dive, cached_datap);
|
||||
tissue_at_end(ds, dive, cache);
|
||||
if ((divemode == CCR || divemode == PSCR) && prefs.dobailout) {
|
||||
divemode = OC;
|
||||
po2 = 0;
|
||||
|
|
@ -864,7 +862,7 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
}
|
||||
previous_deco_time = 100000000;
|
||||
ds->deco_time = 10000000;
|
||||
cache_deco_state(ds, &bottom_cache); // Lets us make several iterations
|
||||
bottom_cache.cache(ds); // Lets us make several iterations
|
||||
bottom_depth = depth;
|
||||
bottom_gi = gi;
|
||||
bottom_gas = gas;
|
||||
|
|
@ -878,7 +876,7 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
vpmb_next_gradient(ds, ds->deco_time, diveplan->surface_pressure / 1000.0, true);
|
||||
|
||||
previous_deco_time = ds->deco_time;
|
||||
restore_deco_state(bottom_cache, ds, true);
|
||||
bottom_cache.restore(ds, true);
|
||||
|
||||
depth = bottom_depth;
|
||||
gi = bottom_gi;
|
||||
|
|
@ -1124,7 +1122,6 @@ extern "C" bool plan(struct deco_state *ds, struct diveplan *diveplan, struct di
|
|||
|
||||
free(stoplevels);
|
||||
free(gaschanges);
|
||||
free(bottom_cache);
|
||||
return decodive;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ struct diveplan {
|
|||
int surface_interval;
|
||||
};
|
||||
|
||||
struct deco_state_cache;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
@ -58,9 +60,10 @@ struct decostop {
|
|||
int depth;
|
||||
int time;
|
||||
};
|
||||
extern bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, struct deco_state **cached_datap, bool is_planner, bool show_disclaimer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
extern bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, int timestep, struct decostop *decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer);
|
||||
#endif
|
||||
#endif // PLANNER_H
|
||||
|
|
|
|||
|
|
@ -943,11 +943,11 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_
|
|||
ds->deco_time = planner_ds->deco_time;
|
||||
ds->first_ceiling_pressure = planner_ds->first_ceiling_pressure;
|
||||
}
|
||||
struct deco_state *cache_data_initial = NULL;
|
||||
deco_state_cache cache_data_initial;
|
||||
lock_planner();
|
||||
/* For VPM-B outside the planner, cache the initial deco state for CVA iterations */
|
||||
if (decoMode(in_planner) == VPMB) {
|
||||
cache_deco_state(ds, &cache_data_initial);
|
||||
cache_data_initial.cache(ds);
|
||||
}
|
||||
/* For VPM-B outside the planner, iterate until deco time converges (usually one or two iterations after the initial)
|
||||
* Set maximum number of iterations to 10 just in case */
|
||||
|
|
@ -1078,14 +1078,13 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_
|
|||
last_ndl_tts_calc_time = entry->sec;
|
||||
|
||||
/* 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);
|
||||
deco_state_cache cache_data;
|
||||
cache_data.cache(ds);
|
||||
calculate_ndl_tts(ds, dive, entry, gasmix, surface_pressure, current_divemode, in_planner);
|
||||
if (decoMode(in_planner) == VPMB && !in_planner && i == pi->nr - 1)
|
||||
final_tts = entry->tts_calc;
|
||||
/* Restore "real" deco state for next real time step */
|
||||
restore_deco_state(cache_data, ds, decoMode(in_planner) == VPMB);
|
||||
free(cache_data);
|
||||
cache_data.restore(ds, decoMode(in_planner) == VPMB);
|
||||
}
|
||||
}
|
||||
if (decoMode(in_planner) == VPMB && !in_planner) {
|
||||
|
|
@ -1107,7 +1106,7 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_
|
|||
first_iteration = false;
|
||||
count_iteration ++;
|
||||
this_deco_time = ds->deco_time;
|
||||
restore_deco_state(cache_data_initial, ds, true);
|
||||
cache_data_initial.restore(ds, true);
|
||||
ds->deco_time = this_deco_time;
|
||||
} else {
|
||||
// With Buhlmann iterating isn't needed. This makes the while condition false.
|
||||
|
|
@ -1115,7 +1114,6 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_
|
|||
}
|
||||
}
|
||||
|
||||
free(cache_data_initial);
|
||||
#if DECO_CALC_DEBUG & 1
|
||||
dump_tissues(ds);
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue