Cylinders: dynamically allocate cylinder arrays

Dynamically allocate cylinder arrays in C code. This is a tiny
step in removing the MAX_CYLINDERS limitation.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-06-27 22:03:53 +02:00 committed by Dirk Hohndel
parent ff653f721c
commit 16bdbc54b9
2 changed files with 32 additions and 12 deletions

View file

@ -757,7 +757,7 @@ static unsigned int get_cylinder_known(const struct dive *dive, const struct div
void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, int *mean, int *duration) void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, int *mean, int *duration)
{ {
int i; int i;
int depthtime[MAX_CYLINDERS] = { 0, }; int *depthtime;
uint32_t lasttime = 0; uint32_t lasttime = 0;
int lastdepth = 0; int lastdepth = 0;
int idx = 0; int idx = 0;
@ -803,6 +803,8 @@ void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, i
if (!dc->samples) if (!dc->samples)
fake_dc(dc); fake_dc(dc);
const struct event *ev = get_next_event(dc->events, "gaschange"); const struct event *ev = get_next_event(dc->events, "gaschange");
depthtime = malloc(MAX_CYLINDERS * sizeof(*depthtime));
memset(depthtime, 0, MAX_CYLINDERS * sizeof(*depthtime));
for (i = 0; i < dc->samples; i++) { for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i; struct sample *sample = dc->sample + i;
uint32_t time = sample->time.seconds; uint32_t time = sample->time.seconds;
@ -835,6 +837,7 @@ void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, i
if (duration[i]) if (duration[i])
mean[i] = (depthtime[i] + duration[i] / 2) / duration[i]; mean[i] = (depthtime[i] + duration[i] / 2) / duration[i];
} }
free(depthtime);
} }
static void update_min_max_temperatures(struct dive *dive, temperature_t temperature) static void update_min_max_temperatures(struct dive *dive, temperature_t temperature)
@ -2983,7 +2986,7 @@ bool has_planned(const struct dive *dive, bool planned)
struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset, bool prefer_downloaded, struct dive_trip **trip, struct dive_site **site) struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset, bool prefer_downloaded, struct dive_trip **trip, struct dive_site **site)
{ {
struct dive *res = alloc_dive(); struct dive *res = alloc_dive();
int cylinders_map_a[MAX_CYLINDERS], cylinders_map_b[MAX_CYLINDERS]; int *cylinders_map_a, *cylinders_map_b;
if (offset) { if (offset) {
/* /*
@ -3015,6 +3018,8 @@ struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset,
MERGE_NONZERO(res, a, b, visibility); MERGE_NONZERO(res, a, b, visibility);
STRUCTURED_LIST_COPY(struct picture, a->picture_list ? a->picture_list : b->picture_list, res->picture_list, copy_pl); STRUCTURED_LIST_COPY(struct picture, a->picture_list ? a->picture_list : b->picture_list, res->picture_list, copy_pl);
taglist_merge(&res->tag_list, a->tag_list, b->tag_list); taglist_merge(&res->tag_list, a->tag_list, b->tag_list);
cylinders_map_a = malloc(MAX_CYLINDERS * sizeof(*cylinders_map_a));
cylinders_map_b = malloc(MAX_CYLINDERS * sizeof(*cylinders_map_b));
merge_cylinders(res, a, b, cylinders_map_a, cylinders_map_b); merge_cylinders(res, a, b, cylinders_map_a, cylinders_map_b);
merge_equipment(res, a, b); merge_equipment(res, a, b);
merge_temperatures(res, a, b); merge_temperatures(res, a, b);
@ -3029,6 +3034,8 @@ struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset,
/* we take the first dive site, unless it's empty */ /* we take the first dive site, unless it's empty */
*site = a->dive_site && !dive_site_is_empty(a->dive_site) ? a->dive_site : b->dive_site; *site = a->dive_site && !dive_site_is_empty(a->dive_site) ? a->dive_site : b->dive_site;
fixup_dive(res); fixup_dive(res);
free(cylinders_map_a);
free(cylinders_map_b);
return res; return res;
} }
@ -3046,6 +3053,11 @@ static struct dive *create_new_copy(const struct dive *from)
return to; return to;
} }
struct start_end_pressure {
pressure_t start;
pressure_t end;
};
static void force_fixup_dive(struct dive *d) static void force_fixup_dive(struct dive *d)
{ {
struct divecomputer *dc = &d->dc; struct divecomputer *dc = &d->dc;
@ -3053,8 +3065,7 @@ static void force_fixup_dive(struct dive *d)
int old_mintemp = d->mintemp.mkelvin; int old_mintemp = d->mintemp.mkelvin;
int old_maxtemp = d->maxtemp.mkelvin; int old_maxtemp = d->maxtemp.mkelvin;
duration_t old_duration = d->duration; duration_t old_duration = d->duration;
cylinder_t old_cylinders[MAX_CYLINDERS]; struct start_end_pressure *old_pressures = malloc(MAX_CYLINDERS * sizeof(*old_pressures));
memcpy(old_cylinders, &d->cylinder, MAX_CYLINDERS * sizeof(cylinder_t));
d->maxdepth.mm = 0; d->maxdepth.mm = 0;
dc->maxdepth.mm = 0; dc->maxdepth.mm = 0;
@ -3064,6 +3075,8 @@ static void force_fixup_dive(struct dive *d)
d->maxtemp.mkelvin = 0; d->maxtemp.mkelvin = 0;
d->mintemp.mkelvin = 0; d->mintemp.mkelvin = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) { for (int i = 0; i < MAX_CYLINDERS; i++) {
old_pressures[i].start = d->cylinder[i].start;
old_pressures[i].end = d->cylinder[i].end;
d->cylinder[i].start.mbar = 0; d->cylinder[i].start.mbar = 0;
d->cylinder[i].end.mbar = 0; d->cylinder[i].end.mbar = 0;
} }
@ -3086,11 +3099,11 @@ static void force_fixup_dive(struct dive *d)
d->duration = old_duration; d->duration = old_duration;
for (int i = 0; i < MAX_CYLINDERS; i++) { for (int i = 0; i < MAX_CYLINDERS; i++) {
if (!d->cylinder[i].start.mbar) if (!d->cylinder[i].start.mbar)
d->cylinder[i].start = old_cylinders[i].start; d->cylinder[i].start = old_pressures[i].start;
if (!d->cylinder[i].end.mbar) if (!d->cylinder[i].end.mbar)
d->cylinder[i].end = old_cylinders[i].end; d->cylinder[i].end = old_pressures[i].end;
} }
free(old_pressures);
} }
/* /*

View file

@ -828,15 +828,18 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive
{ {
int prev, i; int prev, i;
const struct event *ev; const struct event *ev;
int seen[MAX_CYLINDERS] = { 0, }; int *seen = malloc(MAX_CYLINDERS * sizeof(*seen));
unsigned int first[MAX_CYLINDERS] = { 0, }; int *first = malloc(MAX_CYLINDERS * sizeof(*first));
unsigned int last[MAX_CYLINDERS] = { 0, }; int *last = malloc(MAX_CYLINDERS * sizeof(*last));
const struct divecomputer *secondary; const struct divecomputer *secondary;
for (i = 0; i < MAX_CYLINDERS; i++) {
seen[i] = 0;
first[i] = 0;
last[i] = INT_MAX;
}
prev = explicit_first_cylinder(dive, dc); prev = explicit_first_cylinder(dive, dc);
seen[prev] = 1; seen[prev] = 1;
for (i = 0; i < MAX_CYLINDERS; i++)
last[i] = INT_MAX;
for (ev = get_next_event(dc->events, "gaschange"); ev != NULL; ev = get_next_event(ev->next, "gaschange")) { for (ev = get_next_event(dc->events, "gaschange"); ev != NULL; ev = get_next_event(ev->next, "gaschange")) {
int cyl = ev->gas.index; int cyl = ev->gas.index;
@ -909,6 +912,10 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive
continue; continue;
populate_secondary_sensor_data(dc, pi); populate_secondary_sensor_data(dc, pi);
} while ((secondary = secondary->next) != NULL); } while ((secondary = secondary->next) != NULL);
free(seen);
free(first);
free(last);
} }
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE