mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Planner: don't return static data in fake_dc()
fake_dc() used to return a statically allocated dc with statically allocated samples. This is of course a questionable practice in the light of multi-threading / resource ownership. Once these problems were recognized, the parameter "alloc" was added. If set to true, the function would still return a statically allocated dc, but heap-allocated samples, which could then be copied in a different dc. All in all an ownership nightmare and a recipie for disaster. The returned static dc was only used as a pointer to the samples anyway. There are four callers of fake_dc() and they all have access to a dc-structure without samples. Therefore, change the semantics of fake_dc() to fill out the passed in dc. If the caller does not care about the samples, it can simply reset the sample number to zero after work. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
450f0992a0
commit
920ff15f71
7 changed files with 39 additions and 40 deletions
28
core/dive.c
28
core/dive.c
|
|
@ -714,22 +714,26 @@ void copy_samples(struct divecomputer *s, struct divecomputer *d)
|
|||
memcpy(d->sample, s->sample, nr * sizeof(struct sample));
|
||||
}
|
||||
|
||||
/* make room for num samples; if not enough space is available, the sample
|
||||
* array is reallocated and the existing samples are copied. */
|
||||
void alloc_samples(struct divecomputer *dc, int num)
|
||||
{
|
||||
if (num > dc->alloc_samples) {
|
||||
dc->alloc_samples = (dc->alloc_samples * 3) / 2 + 10;
|
||||
dc->sample = realloc(dc->sample, dc->alloc_samples * sizeof(struct sample));
|
||||
if (!dc->sample)
|
||||
dc->samples = dc->alloc_samples = 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct sample *prepare_sample(struct divecomputer *dc)
|
||||
{
|
||||
if (dc) {
|
||||
int nr = dc->samples;
|
||||
int alloc_samples = dc->alloc_samples;
|
||||
struct sample *sample;
|
||||
if (nr >= alloc_samples) {
|
||||
struct sample *newsamples;
|
||||
|
||||
alloc_samples = (alloc_samples * 3) / 2 + 10;
|
||||
newsamples = realloc(dc->sample, alloc_samples * sizeof(struct sample));
|
||||
if (!newsamples)
|
||||
return NULL;
|
||||
dc->alloc_samples = alloc_samples;
|
||||
dc->sample = newsamples;
|
||||
}
|
||||
alloc_samples(dc, nr + 1);
|
||||
if (!dc->sample)
|
||||
return NULL;
|
||||
sample = dc->sample + nr;
|
||||
memset(sample, 0, sizeof(*sample));
|
||||
|
||||
|
|
@ -922,7 +926,7 @@ void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *me
|
|||
return;
|
||||
}
|
||||
if (!dc->samples)
|
||||
dc = fake_dc(dc, false);
|
||||
fake_dc(dc);
|
||||
struct event *ev = get_next_event(dc->events, "gaschange");
|
||||
for (i = 0; i < dc->samples; i++) {
|
||||
struct sample *sample = dc->sample + i;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue