[vte] Fix ring access const correctness



commit eb0444e52d7995f50326109cd6d4cf254b2a4774
Author: Behdad Esfahbod <behdad behdad org>
Date:   Mon Sep 7 22:05:08 2009 -0400

    Fix ring access const correctness

 src/ring.c   |   14 +++++++++++-
 src/ring.h   |   14 +++++++++--
 src/vte.c    |   65 ++++++++++++++++++++++++++++++++++-----------------------
 src/vteseq.c |   24 ++++++++++----------
 4 files changed, 75 insertions(+), 42 deletions(-)
---
diff --git a/src/ring.c b/src/ring.c
index 0bd32af..963c031 100644
--- a/src/ring.c
+++ b/src/ring.c
@@ -419,6 +419,9 @@ _vte_ring_chunk_insert_chunk_before (VteRingChunk *chunk, VteRingChunk *new)
 
 /* Compact chunk type */
 
+/* TODO Get rid of the temp cell */
+static const VteCellInt temp_cell;
+
 typedef struct _VteRingChunkCompact {
 	VteRingChunk base;
 
@@ -664,7 +667,7 @@ _vte_ring_find_chunk (VteRing *ring, guint position)
 	return ring->cursor;
 }
 
-VteRowData *
+const VteRowData *
 _vte_ring_index (VteRing *ring, guint position)
 {
 	VteRingChunk *chunk;
@@ -678,6 +681,15 @@ _vte_ring_index (VteRing *ring, guint position)
 	return &chunk->array[(position - chunk->offset) & chunk->mask];
 }
 
+static void _vte_ring_ensure_writable (VteRing *ring, guint position);
+
+VteRowData *
+_vte_ring_index_writable (VteRing *ring, guint position)
+{
+	_vte_ring_ensure_writable (ring, position);
+	return _vte_ring_chunk_writable_index (ring->head, position);
+}
+
 static void
 _vte_ring_free_chunk (VteRing *ring, VteRingChunk *chunk)
 {
diff --git a/src/ring.h b/src/ring.h
index 956ee33..6955e06 100644
--- a/src/ring.h
+++ b/src/ring.h
@@ -145,9 +145,17 @@ typedef struct _VteRowData {
 } VteRowData;
 
 
-#define _vte_row_data_get(__row, __col)			((const VteCell *) _vte_row_data_get_writable (__row, __col))
 #define _vte_row_data_length(__row)			((__row)->len + 0)
 
+static inline const VteCell *
+_vte_row_data_get (const VteRowData *row, guint col)
+{
+	if (G_UNLIKELY (row->len <= col))
+		return NULL;
+
+	return &row->data.cells[col];
+}
+
 static inline VteCell *
 _vte_row_data_get_writable (VteRowData *row, guint col)
 {
@@ -210,8 +218,8 @@ struct _VteRing {
 #define _vte_ring_length(__ring) ((__ring)->head->end - (__ring)->tail->start)
 #define _vte_ring_next(__ring) ((__ring)->head->end + 0)
 
-VteRowData *
-_vte_ring_index (VteRing *ring, guint position);
+const VteRowData *_vte_ring_index (VteRing *ring, guint position);
+VteRowData *_vte_ring_index_writable (VteRing *ring, guint position);
 
 void _vte_ring_init (VteRing *ring, guint max_rows);
 void _vte_ring_fini (VteRing *ring);
diff --git a/src/vte.c b/src/vte.c
index a83cd81..096b149 100644
--- a/src/vte.c
+++ b/src/vte.c
@@ -482,26 +482,39 @@ _vte_terminal_scroll_region (VteTerminal *terminal,
 }
 
 /* Find the row in the given position in the backscroll buffer. */
+static inline const VteRowData *
+_vte_terminal_find_row_data (VteTerminal *terminal, glong row)
+{
+	const VteRowData *rowdata = NULL;
+	VteScreen *screen = terminal->pvt->screen;
+	if (G_LIKELY (_vte_ring_contains (screen->row_data, row))) {
+		rowdata = _vte_ring_index (screen->row_data, row);
+	}
+	return rowdata;
+}
+
+/* Find the row in the given position in the backscroll buffer. */
 static inline VteRowData *
-_vte_terminal_find_row_data(VteTerminal *terminal, glong row)
+_vte_terminal_find_row_data_writable (VteTerminal *terminal, glong row)
 {
 	VteRowData *rowdata = NULL;
 	VteScreen *screen = terminal->pvt->screen;
-	if (_vte_ring_contains(screen->row_data, row)) {
-		rowdata = _vte_ring_index(screen->row_data, row);
+	if (G_LIKELY (_vte_ring_contains (screen->row_data, row))) {
+		rowdata = _vte_ring_index_writable (screen->row_data, row);
 	}
 	return rowdata;
 }
+
 /* Find the character an the given position in the backscroll buffer. */
 static const VteCell *
 vte_terminal_find_charcell(VteTerminal *terminal, gulong col, glong row)
 {
-	VteRowData *rowdata;
+	const VteRowData *rowdata;
 	const VteCell *ret = NULL;
 	VteScreen *screen;
 	screen = terminal->pvt->screen;
-	if (_vte_ring_contains(screen->row_data, row)) {
-		rowdata = _vte_ring_index(screen->row_data, row);
+	if (_vte_ring_contains (screen->row_data, row)) {
+		rowdata = _vte_ring_index (screen->row_data, row);
 		ret = _vte_row_data_get (rowdata, col);
 	}
 	return ret;
@@ -510,7 +523,7 @@ vte_terminal_find_charcell(VteTerminal *terminal, gulong col, glong row)
 static glong
 find_start_column (VteTerminal *terminal, glong col, glong row)
 {
-	VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
+	const VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
 	if (G_UNLIKELY (col < 0))
 		return col;
 	if (row_data != NULL) {
@@ -524,7 +537,7 @@ find_start_column (VteTerminal *terminal, glong col, glong row)
 static glong
 find_end_column (VteTerminal *terminal, glong col, glong row)
 {
-	VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
+	const VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
 	gint columns = 0;
 	if (G_UNLIKELY (col < 0))
 		return col;
@@ -593,7 +606,7 @@ vte_terminal_preedit_length(VteTerminal *terminal, gboolean left_only)
 void
 _vte_invalidate_cell(VteTerminal *terminal, glong col, glong row)
 {
-	VteRowData *row_data;
+	const VteRowData *row_data;
 	int columns;
 
 	if (G_UNLIKELY (!GTK_WIDGET_DRAWABLE(terminal) || terminal->pvt->invalidated_all)) {
@@ -2245,13 +2258,13 @@ vte_terminal_get_encoding(VteTerminal *terminal)
 	return terminal->pvt->encoding;
 }
 
-static inline VteRowData*
+static inline VteRowData *
 vte_terminal_insert_rows (VteTerminal *terminal, guint cnt)
 {
 	VteRowData *row;
-	const VteScreen *screen = terminal->pvt->screen;
+	VteScreen *screen = terminal->pvt->screen;
 	do {
-		row = _vte_ring_append(screen->row_data);
+		row = _vte_ring_append (screen->row_data);
 	} while(--cnt);
 	return row;
 }
@@ -2263,7 +2276,7 @@ VteRowData *
 _vte_terminal_ensure_row (VteTerminal *terminal)
 {
 	VteRowData *row;
-	const VteScreen *screen;
+	VteScreen *screen;
 	gint delta;
 	glong v;
 
@@ -2280,7 +2293,7 @@ _vte_terminal_ensure_row (VteTerminal *terminal)
 		_vte_terminal_adjust_adjustments(terminal);
 	} else {
 		/* Find the row the cursor is in. */
-		row = _vte_ring_index(screen->row_data, v);
+		row = _vte_ring_index_writable (screen->row_data, v);
 	}
 	g_assert(row != NULL);
 
@@ -2957,7 +2970,7 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 			/* XXX clear to the end of line */
 			col = screen->cursor_current.col = 0;
 			/* Mark this line as soft-wrapped. */
-			row = _vte_terminal_ensure_row(terminal);
+			row = _vte_terminal_ensure_row (terminal);
 			row->soft_wrapped = 1;
 			_vte_terminal_cursor_down (terminal);
 		} else {
@@ -2994,7 +3007,7 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 
 			if (G_LIKELY (row_num > 0)) {
 				row_num--;
-				row = _vte_terminal_find_row_data (terminal, row_num);
+				row = _vte_terminal_find_row_data_writable (terminal, row_num);
 
 				if (row) {
 					if (!row->soft_wrapped)
@@ -3004,7 +3017,7 @@ _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
 				}
 			}
 		} else {
-			row = _vte_terminal_find_row_data (terminal, row_num);
+			row = _vte_terminal_find_row_data_writable (terminal, row_num);
 		}
 
 		if (G_UNLIKELY (!row || !col))
@@ -5155,7 +5168,7 @@ vte_same_class(VteTerminal *terminal, glong acol, glong arow,
 static gboolean
 vte_line_is_wrappable(VteTerminal *terminal, glong row)
 {
-	VteRowData *rowdata;
+	const VteRowData *rowdata;
 	rowdata = _vte_terminal_find_row_data(terminal, row);
 	return rowdata && rowdata->soft_wrapped;
 }
@@ -5763,7 +5776,7 @@ vte_terminal_get_text_range_maybe_wrapped(VteTerminal *terminal,
 	palette = terminal->pvt->palette;
 	col = start_col;
 	for (row = start_row; row <= end_row; row++, col = 0) {
-		VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
+		const VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
 		last_empty = last_nonempty = string->len;
 		last_emptycol = last_nonemptycol = -1;
 
@@ -6142,7 +6155,7 @@ vte_terminal_extend_selection_expand (VteTerminal *terminal)
 {
 	long i, j;
 	VteScreen *screen;
-	VteRowData *rowdata;
+	const VteRowData *rowdata;
 	const VteCell *cell;
 	struct selection_cell_coords *sc, *ec;
 
@@ -6220,7 +6233,7 @@ vte_terminal_extend_selection_expand (VteTerminal *terminal)
 		 * look at is of the same class as the current start point. */
 		i = sc->col;
 		j = sc->row;
-		while (_vte_ring_contains(screen->row_data, j)) {
+		while (_vte_ring_contains (screen->row_data, j)) {
 			/* Get the data for the row we're looking at. */
 			rowdata = _vte_ring_index(screen->row_data, j);
 			if (rowdata == NULL) {
@@ -6266,7 +6279,7 @@ vte_terminal_extend_selection_expand (VteTerminal *terminal)
 		 * look at is of the same class as the current end point. */
 		i = ec->col;
 		j = ec->row;
-		while (_vte_ring_contains(screen->row_data, j)) {
+		while (_vte_ring_contains (screen->row_data, j)) {
 			/* Get the data for the row we're looking at. */
 			rowdata = _vte_ring_index(screen->row_data, j);
 			if (rowdata == NULL) {
@@ -6314,21 +6327,21 @@ vte_terminal_extend_selection_expand (VteTerminal *terminal)
 		sc->col = 0;
 		/* Now back up as far as we can go. */
 		j = sc->row;
-		while (_vte_ring_contains(screen->row_data, j - 1) &&
+		while (_vte_ring_contains (screen->row_data, j - 1) &&
 		       vte_line_is_wrappable(terminal, j - 1)) {
 			j--;
 			sc->row = j;
 		}
 		/* And move forward as far as we can go. */
 		j = ec->row;
-		while (_vte_ring_contains(screen->row_data, j) &&
+		while (_vte_ring_contains (screen->row_data, j) &&
 		       vte_line_is_wrappable(terminal, j)) {
 			j++;
 			ec->row = j;
 		}
 		/* Make sure we include all of the last line. */
 		ec->col = terminal->column_count - 1;
-		if (_vte_ring_contains(screen->row_data, ec->row)) {
+		if (_vte_ring_contains (screen->row_data, ec->row)) {
 			rowdata = _vte_ring_index(screen->row_data, ec->row);
 			if (rowdata != NULL) {
 				ec->col = MAX(ec->col, (long) _vte_row_data_length (rowdata));
@@ -9900,7 +9913,7 @@ vte_terminal_draw_rows(VteTerminal *terminal,
 		 selected, nselected, strikethrough, nstrikethrough;
 	guint item_count;
 	const VteCell *cell;
-	VteRowData *row_data;
+	const VteRowData *row_data;
 
 	reverse = terminal->pvt->screen->reverse_mode;
 
diff --git a/src/vteseq.c b/src/vteseq.c
index fb3e0bb..5143a59 100644
--- a/src/vteseq.c
+++ b/src/vteseq.c
@@ -77,15 +77,15 @@ display_control_sequence(const char *name, GValueArray *params)
 
 /* Find the character an the given position in the backscroll buffer. */
 static VteCell *
-vte_terminal_find_charcell(VteTerminal *terminal, glong col, glong row)
+vte_terminal_find_charcell (VteTerminal *terminal, glong col, glong row)
 {
 	VteRowData *rowdata;
 	VteCell *ret = NULL;
 	VteScreen *screen;
 	g_assert(VTE_IS_TERMINAL(terminal));
 	screen = terminal->pvt->screen;
-	if (_vte_ring_contains(screen->row_data, row)) {
-		rowdata = _vte_ring_index(screen->row_data, row);
+	if (_vte_ring_contains (screen->row_data, row)) {
+		rowdata = _vte_ring_index_writable (screen->row_data, row);
 		ret = _vte_row_data_get_writable (rowdata, col);
 	}
 	return ret;
@@ -302,7 +302,7 @@ _vte_terminal_clear_current_line (VteTerminal *terminal)
 	 * which corresponds to the cursor. */
 	if (_vte_ring_next(screen->row_data) > screen->cursor_current.row) {
 		/* Get the data for the row which the cursor points to. */
-		rowdata = _vte_ring_index(screen->row_data, screen->cursor_current.row);
+		rowdata = _vte_ring_index_writable (screen->row_data, screen->cursor_current.row);
 		g_assert(rowdata != NULL);
 		/* Remove it. */
 		_vte_row_data_shrink (rowdata, 0);
@@ -332,7 +332,7 @@ _vte_terminal_clear_above_current (VteTerminal *terminal)
 	for (i = screen->insert_delta; i < screen->cursor_current.row; i++) {
 		if (_vte_ring_next(screen->row_data) > i) {
 			/* Get the data for the row we're erasing. */
-			rowdata = _vte_ring_index(screen->row_data, i);
+			rowdata = _vte_ring_index_writable (screen->row_data, i);
 			g_assert(rowdata != NULL);
 			/* Remove it. */
 			_vte_row_data_shrink (rowdata, 0);
@@ -1039,7 +1039,7 @@ vte_sequence_handler_cd (VteTerminal *terminal, GValueArray *params)
 	i = screen->cursor_current.row;
 	if (i < _vte_ring_next(screen->row_data)) {
 		/* Get the data for the row we're clipping. */
-		rowdata = _vte_ring_index(screen->row_data, i);
+		rowdata = _vte_ring_index_writable (screen->row_data, i);
 		/* Clear everything to the right of the cursor. */
 		if (rowdata)
 			_vte_row_data_shrink (rowdata, screen->cursor_current.col);
@@ -1049,7 +1049,7 @@ vte_sequence_handler_cd (VteTerminal *terminal, GValueArray *params)
 	     i < _vte_ring_next(screen->row_data);
 	     i++) {
 		/* Get the data for the row we're removing. */
-		rowdata = _vte_ring_index(screen->row_data, i);
+		rowdata = _vte_ring_index_writable (screen->row_data, i);
 		/* Remove it. */
 		if (rowdata)
 			_vte_row_data_shrink (rowdata, 0);
@@ -1059,11 +1059,11 @@ vte_sequence_handler_cd (VteTerminal *terminal, GValueArray *params)
 	     i < screen->insert_delta + terminal->row_count;
 	     i++) {
 		/* Retrieve the row's data, creating it if necessary. */
-		if (_vte_ring_contains(screen->row_data, i)) {
-			rowdata = _vte_ring_index(screen->row_data, i);
+		if (_vte_ring_contains (screen->row_data, i)) {
+			rowdata = _vte_ring_index_writable (screen->row_data, i);
 			g_assert(rowdata != NULL);
 		} else {
-			rowdata = _vte_ring_append(screen->row_data);
+			rowdata = _vte_ring_append (screen->row_data);
 		}
 		/* Pad out the row. */
 		_vte_row_data_fill (rowdata, &screen->fill_defaults, terminal->column_count);
@@ -1350,7 +1350,7 @@ vte_sequence_handler_dc (VteTerminal *terminal, GValueArray *params)
 	if (_vte_ring_next(screen->row_data) > screen->cursor_current.row) {
 		long len;
 		/* Get the data for the row which the cursor points to. */
-		rowdata = _vte_ring_index(screen->row_data, screen->cursor_current.row);
+		rowdata = _vte_ring_index_writable (screen->row_data, screen->cursor_current.row);
 		g_assert(rowdata != NULL);
 		col = screen->cursor_current.col;
 		len = _vte_row_data_length (rowdata);
@@ -2975,7 +2975,7 @@ vte_sequence_handler_screen_alignment_test (VteTerminal *terminal, GValueArray *
 		while (_vte_ring_next(screen->row_data) <= row)
 			_vte_ring_append(screen->row_data);
 		_vte_terminal_adjust_adjustments(terminal);
-		rowdata = _vte_ring_index(screen->row_data, row);
+		rowdata = _vte_ring_index_writable (screen->row_data, row);
 		g_assert(rowdata != NULL);
 		/* Clear this row. */
 		_vte_row_data_shrink (rowdata, 0);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]