[PATCH 2/2] Move 'plot_info' into 'struct graphics_context'

Linus Torvalds torvalds at linux-foundation.org
Thu Dec 6 11:13:02 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Thu, 6 Dec 2012 10:01:16 -0800
Subject: [PATCH 2/2] 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 at linux-foundation.org>
---

Again, this shouldn't actually change anything. The only difference is 
that now we could do things like fill in maxdepth/maxtime in struct 
plot_info independently of the *one* dive computer that we pass in to 
'create_plot_info()'.

So the point is that we will want to do some preliminary work that is 
independent of one particular dive computer, and then pick the one we want 
to show, and pass just that one down. And that really wants access to the 
plot info before..

 display.h |  15 +++++++++-
 profile.c | 100 ++++++++++++++++++++++++++------------------------------------
 2 files changed, 56 insertions(+), 59 deletions(-)

diff --git a/display.h b/display.h
index 6eea822aa0b9..6f9f6854cb15 100644
--- a/display.h
+++ b/display.h
@@ -10,6 +10,19 @@
 extern void repaint_dive(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.
  *
@@ -26,7 +39,7 @@ struct graphics_context {
 	double leftx, rightx;
 	double topy, bottomy;
 	unsigned int maxtime;
-	void *plot_info;
+	struct plot_info pi;
 };
 
 typedef enum { SC_SCREEN, SC_PRINT } scale_mode_t;
diff --git a/profile.c b/profile.c
index 9fdc204f2793..0b20a1ff2652 100644
--- a/profile.c
+++ b/profile.c
@@ -28,36 +28,25 @@ static double plot_scale = SCALE_SCREEN;
 
 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
 
-/* 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 {
-		unsigned int same_cylinder:1;
-		unsigned int cylinderindex;
-		int sec;
-		/* pressure[0] is sensor pressure
-		 * pressure[1] is interpolated pressure */
-		int pressure[2];
-		int temperature;
-		/* Depth info */
-		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[];
+struct plot_data {
+	unsigned int same_cylinder:1;
+	unsigned int cylinderindex;
+	int sec;
+	/* pressure[0] is sensor pressure
+	 * pressure[1] is interpolated pressure */
+	int pressure[2];
+	int temperature;
+	/* Depth info */
+	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];
 };
 
 #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 */
 #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)
@@ -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
  * 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 lastdepth, lastindex;
 	int i, pi_idx, nr, sec, cyl, stoptime, ndl, stopdepth;
-	size_t alloc_size;
 	struct plot_info *pi;
 	pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
 	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;
 	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 */
 	nr = dc->samples + 4 + 2 * count_gas_change_events(dc);
-	alloc_size = plot_info_size(nr);
-	pi = malloc(alloc_size);
-	if (!pi)
-		return pi;
-	memset(pi, 0, alloc_size);
+	pi->entry = calloc(nr, sizeof(struct plot_data));
+	if (!pi->entry)
+		return NULL;
 	pi->nr = nr;
 	pi_idx = 2; /* the two extra events at the 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;
 	}
 
-	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 */
 	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);
 
 	if (gc->printer) {
-		free(pi);
-	} else {
-		free(gc->plot_info);
-		gc->plot_info = pi;
+		free(pi->entry);
+		pi->entry = NULL;
+		pi->nr = 0;
 	}
 }
 
@@ -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)
 {
-	struct plot_info *pi = gc->plot_info;
+	struct plot_info *pi = &gc->pi;
 	int pressure = 0, temp = 0;
 	struct plot_data *entry;
+	int i;
 
-	*buf = 0;
-	if (pi) {
-		int i;
-		for (i = 0; i < pi->nr; i++) {
-			entry = pi->entry + i;
-			if (entry->temperature)
-				temp = entry->temperature;
-			if (GET_PRESSURE(entry))
-				pressure = GET_PRESSURE(entry);
-			if (entry->sec >= time)
-				break;
-		}
-		plot_string(entry, buf, bufsize, entry->depth, pressure, temp, pi->has_ndl);
+	for (i = 0; i < pi->nr; i++) {
+		entry = pi->entry + i;
+		if (entry->temperature)
+			temp = entry->temperature;
+		if (GET_PRESSURE(entry))
+			pressure = GET_PRESSURE(entry);
+		if (entry->sec >= time)
+			break;
 	}
+	plot_string(entry, buf, bufsize, entry->depth, pressure, temp, pi->has_ndl);
 }
-- 
1.8.0.dirty



More information about the subsurface mailing list