summaryrefslogtreecommitdiffstats
path: root/dive.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-09-12 12:56:34 -0700
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-09-12 12:56:34 -0700
commitaa416e3c96dfa53db5ae277e72f6a03821c45cac (patch)
treed0adaa4ac077f64fa08e084348281c49821ff3fe /dive.c
parentd45db9fac7b3d2ea0426a96158432f54d938f5c7 (diff)
downloadsubsurface-aa416e3c96dfa53db5ae277e72f6a03821c45cac.tar.gz
Abstract out dive/sample allocation a bit
We're going to start to want to allocate dives and samples for the libdivecomputer import too, so let's clean things up a bit for that. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'dive.c')
-rw-r--r--dive.c73
1 files changed, 53 insertions, 20 deletions
diff --git a/dive.c b/dive.c
index e899c6ccb..32ea2ffe1 100644
--- a/dive.c
+++ b/dive.c
@@ -3,6 +3,51 @@
#include "dive.h"
+struct dive *alloc_dive(void)
+{
+ const int initial_samples = 5;
+ unsigned int size;
+ struct dive *dive;
+
+ size = dive_size(initial_samples);
+ dive = malloc(size);
+ if (!dive)
+ exit(1);
+ memset(dive, 0, size);
+ dive->alloc_samples = initial_samples;
+ return dive;
+}
+
+struct sample *prepare_sample(struct dive **divep)
+{
+ struct dive *dive = *divep;
+ if (dive) {
+ int nr = dive->samples;
+ int alloc_samples = dive->alloc_samples;
+ struct sample *sample;
+ if (nr >= alloc_samples) {
+ unsigned int size;
+
+ alloc_samples = (alloc_samples * 3)/2 + 10;
+ size = dive_size(alloc_samples);
+ dive = realloc(dive, size);
+ if (!dive)
+ return NULL;
+ dive->alloc_samples = alloc_samples;
+ *divep = dive;
+ }
+ sample = dive->sample + nr;
+ memset(sample, 0, sizeof(*sample));
+ return sample;
+ }
+ return NULL;
+}
+
+void finish_sample(struct dive *dive, struct sample *sample)
+{
+ dive->samples++;
+}
+
/*
* So when we re-calculate maxdepth and meandepth, we will
* not override the old numbers if they are close to the
@@ -133,24 +178,15 @@ struct dive *fixup_dive(struct dive *dive)
#define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
#define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
-static int alloc_samples;
-
static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
{
- int nr = dive->samples;
- struct sample *d;
+ struct sample *p = prepare_sample(&dive);
- if (nr >= alloc_samples) {
- alloc_samples = (alloc_samples + 64) * 3 / 2;
- dive = realloc(dive, dive_size(alloc_samples));
- if (!dive)
- return NULL;
- }
- dive->samples = nr+1;
- d = dive->sample + nr;
-
- *d = *sample;
- d->time.seconds = time;
+ if (!p)
+ return NULL;
+ *p = *sample;
+ p->time.seconds = time;
+ finish_sample(dive, p);
return dive;
}
@@ -274,15 +310,12 @@ struct dive *try_to_merge(struct dive *a, struct dive *b)
if (a->when != b->when)
return NULL;
- alloc_samples = 5;
- res = malloc(dive_size(alloc_samples));
- if (!res)
- return NULL;
- memset(res, 0, dive_size(alloc_samples));
+ res = alloc_dive();
res->when = a->when;
res->location = merge_text(a->location, b->location);
res->notes = merge_text(a->notes, b->notes);
+ MERGE_MAX(res, a, b, number);
MERGE_MAX(res, a, b, maxdepth.mm);
res->meandepth.mm = 0;
MERGE_MAX(res, a, b, duration.seconds);