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>
This commit is contained in:
Berthold Stoeger 2018-08-16 13:35:14 +02:00 committed by Dirk Hohndel
parent b6187f73aa
commit f5b11daffd
10 changed files with 65 additions and 55 deletions

View file

@ -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, &current_divemode), dive->sac);
}
}