Move 'plot_info' into 'struct graphics_context'

.. and then allocate just the plot-info entry array dynamically.

We want to have a longer lifetime for the basic plot_info data
structure, because we want to do computer selection and maximum
time/depth/temperature computations *before* we start plotting anything,
and before we allocate the plot entry array.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2012-12-06 10:01:16 -08:00 committed by Dirk Hohndel
parent 22aa6fa4ef
commit 0d1f396f3a
2 changed files with 56 additions and 59 deletions

View file

@ -10,6 +10,19 @@
extern void repaint_dive(void); extern void repaint_dive(void);
extern void do_print(void); extern void do_print(void);
/* Plot info with smoothing, velocity indication
* and one-, two- and three-minute minimums and maximums */
struct plot_info {
int nr;
int maxtime;
int meandepth, maxdepth;
int endpressure, maxpressure;
int mintemp, maxtemp, endtemp;
double endtempcoord;
gboolean has_ndl;
struct plot_data *entry;
};
/* /*
* Cairo scaling really is horribly horribly mis-designed. * Cairo scaling really is horribly horribly mis-designed.
* *
@ -26,7 +39,7 @@ struct graphics_context {
double leftx, rightx; double leftx, rightx;
double topy, bottomy; double topy, bottomy;
unsigned int maxtime; unsigned int maxtime;
void *plot_info; struct plot_info pi;
}; };
typedef enum { SC_SCREEN, SC_PRINT } scale_mode_t; typedef enum { SC_SCREEN, SC_PRINT } scale_mode_t;

100
profile.c
View file

@ -28,36 +28,25 @@ static double plot_scale = SCALE_SCREEN;
typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t; typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
/* Plot info with smoothing, velocity indication struct plot_data {
* and one-, two- and three-minute minimums and maximums */ unsigned int same_cylinder:1;
struct plot_info { unsigned int cylinderindex;
int nr; int sec;
int maxtime; /* pressure[0] is sensor pressure
int meandepth, maxdepth; * pressure[1] is interpolated pressure */
int endpressure, maxpressure; int pressure[2];
int mintemp, maxtemp, endtemp; int temperature;
double endtempcoord; /* Depth info */
gboolean has_ndl; int depth;
struct plot_data { int ndl;
unsigned int same_cylinder:1; int stoptime;
unsigned int cylinderindex; int stopdepth;
int sec; int smoothed;
/* pressure[0] is sensor pressure double po2, pn2, phe;
* pressure[1] is interpolated pressure */ velocity_t velocity;
int pressure[2]; struct plot_data *min[3];
int temperature; struct plot_data *max[3];
/* Depth info */ int avg[3];
int depth;
int ndl;
int stoptime;
int stopdepth;
int smoothed;
double po2, pn2, phe;
velocity_t velocity;
struct plot_data *min[3];
struct plot_data *max[3];
int avg[3];
} entry[];
}; };
#define SENSOR_PR 0 #define SENSOR_PR 0
@ -145,8 +134,6 @@ static const color_t profile_color[] = {
}; };
#define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
/* Scale to 0,0 -> maxx,maxy */ /* Scale to 0,0 -> maxx,maxy */
#define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx) #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
#define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy) #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
@ -1688,12 +1675,11 @@ static int count_gas_change_events(struct divecomputer *dc)
* sides, so that you can do end-points without having to worry * sides, so that you can do end-points without having to worry
* about it. * about it.
*/ */
static struct plot_info *create_plot_info(struct dive *dive, struct divecomputer *dc) static struct plot_info *create_plot_info(struct dive *dive, struct divecomputer *dc, struct graphics_context *gc)
{ {
int cylinderindex = -1; int cylinderindex = -1;
int lastdepth, lastindex; int lastdepth, lastindex;
int i, pi_idx, nr, sec, cyl, stoptime, ndl, stopdepth; int i, pi_idx, nr, sec, cyl, stoptime, ndl, stopdepth;
size_t alloc_size;
struct plot_info *pi; struct plot_info *pi;
pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, }; pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
pr_track_t *pr_track, *current; pr_track_t *pr_track, *current;
@ -1702,13 +1688,15 @@ static struct plot_info *create_plot_info(struct dive *dive, struct divecomputer
struct event *ev; struct event *ev;
double amb_pressure; double amb_pressure;
/* The plot-info is embedded in the graphics context */
pi = &gc->pi;
memset(pi, 0, sizeof(*pi));
/* we want to potentially add synthetic plot_info elements for the gas changes */ /* we want to potentially add synthetic plot_info elements for the gas changes */
nr = dc->samples + 4 + 2 * count_gas_change_events(dc); nr = dc->samples + 4 + 2 * count_gas_change_events(dc);
alloc_size = plot_info_size(nr); pi->entry = calloc(nr, sizeof(struct plot_data));
pi = malloc(alloc_size); if (!pi->entry)
if (!pi) return NULL;
return pi;
memset(pi, 0, alloc_size);
pi->nr = nr; pi->nr = nr;
pi_idx = 2; /* the two extra events at the start */ pi_idx = 2; /* the two extra events at the start */
/* check for gas changes before the samples start */ /* check for gas changes before the samples start */
@ -1971,7 +1959,7 @@ void plot(struct graphics_context *gc, struct dive *dive, scale_mode_t scale)
dc = &fakedc; dc = &fakedc;
} }
pi = create_plot_info(dive, dc); pi = create_plot_info(dive, dc, gc);
/* shift the drawing area so we have a nice margin around it */ /* shift the drawing area so we have a nice margin around it */
cairo_translate(gc->cr, drawing_area->x, drawing_area->y); cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
@ -2038,10 +2026,9 @@ void plot(struct graphics_context *gc, struct dive *dive, scale_mode_t scale)
plot_depth_scale(gc, pi); plot_depth_scale(gc, pi);
if (gc->printer) { if (gc->printer) {
free(pi); free(pi->entry);
} else { pi->entry = NULL;
free(gc->plot_info); pi->nr = 0;
gc->plot_info = pi;
} }
} }
@ -2106,22 +2093,19 @@ static void plot_string(struct plot_data *entry, char *buf, size_t bufsize,
void get_plot_details(struct graphics_context *gc, int time, char *buf, size_t bufsize) void get_plot_details(struct graphics_context *gc, int time, char *buf, size_t bufsize)
{ {
struct plot_info *pi = gc->plot_info; struct plot_info *pi = &gc->pi;
int pressure = 0, temp = 0; int pressure = 0, temp = 0;
struct plot_data *entry; struct plot_data *entry;
int i;
*buf = 0; for (i = 0; i < pi->nr; i++) {
if (pi) { entry = pi->entry + i;
int i; if (entry->temperature)
for (i = 0; i < pi->nr; i++) { temp = entry->temperature;
entry = pi->entry + i; if (GET_PRESSURE(entry))
if (entry->temperature) pressure = GET_PRESSURE(entry);
temp = entry->temperature; if (entry->sec >= time)
if (GET_PRESSURE(entry)) break;
pressure = GET_PRESSURE(entry);
if (entry->sec >= time)
break;
}
plot_string(entry, buf, bufsize, entry->depth, pressure, temp, pi->has_ndl);
} }
plot_string(entry, buf, bufsize, entry->depth, pressure, temp, pi->has_ndl);
} }