[cairo-commit] src/cairo-ps-surface.c
Behdad Esfahbod
behdad at kemper.freedesktop.org
Tue Nov 21 09:11:27 PST 2006
src/cairo-ps-surface.c | 28 +++++++++++++++-------------
1 files changed, 15 insertions(+), 13 deletions(-)
New commits:
diff-tree 75eeb889767468b374df5f0aa6fb7ba1fe6addf9 (from 2dbb3dfd5f7e3d802f1c3c57b39f9d69b2b7ee1e)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Nov 21 12:10:36 2006 -0500
[PS] Eliminate compiler warnings about unoptimizable loops
An innocient-looking loop like this:
for (j = 0; j <= last; j++)
something();
cannot be optimized, because it may loop forever!
Imagine the case that last is MAXINT, the loop will never end. The correct
way to write it is:
for (j = 0; j < last+1; j++)
something();
In this case, if last is MAXINT, the loop will never run. Not correct, but
better than looping forever.
Still better would be to correctly handle the MAXINT case (even though it
doesn't make any sense to show MAXINT number of glyphs in one operation!) To
do that, we can use the fact that the input num_glyphs is a signed. If
there is one good thing about using signed int as input length, it's that you
can use an unsigned looping variable to avoid looping forever. That is
exactly what this patch does.
diff --git a/src/cairo-ps-surface.c b/src/cairo-ps-surface.c
index d02b251..f9ca3a1 100644
--- a/src/cairo-ps-surface.c
+++ b/src/cairo-ps-surface.c
@@ -2146,7 +2146,7 @@ _cairo_ps_surface_show_glyphs (void
unsigned int font_id;
cairo_ps_glyph_id_t *glyph_ids;
cairo_status_t status;
- int i, j, last, end;
+ unsigned int n_glyphs, i, j, last, end;
cairo_bool_t vertical, horizontal;
cairo_output_stream_t *word_wrap;
@@ -2158,15 +2158,17 @@ _cairo_ps_surface_show_glyphs (void
_cairo_output_stream_printf (stream,
"%% _cairo_ps_surface_show_glyphs\n");
- if (num_glyphs == 0)
+ if (num_glyphs <= 0)
return CAIRO_STATUS_SUCCESS;
+ n_glyphs = num_glyphs;
+
emit_pattern (surface, source);
- glyph_ids = malloc (num_glyphs*sizeof (cairo_ps_glyph_id_t));
+ glyph_ids = malloc (n_glyphs*sizeof (cairo_ps_glyph_id_t));
if (glyph_ids == NULL)
return CAIRO_STATUS_NO_MEMORY;
- for (i = 0; i < num_glyphs; i++) {
+ for (i = 0; i < n_glyphs; i++) {
status = _cairo_scaled_font_subsets_map_glyph (surface->font_subsets,
scaled_font, glyphs[i].index,
&font_id,
@@ -2177,7 +2179,7 @@ _cairo_ps_surface_show_glyphs (void
}
i = 0;
- while (i < num_glyphs) {
+ while (i < n_glyphs) {
if (glyph_ids[i].subset_id != current_subset_id) {
_cairo_output_stream_printf (surface->stream,
"/CairoFont-%d-%d findfont\n"
@@ -2200,7 +2202,7 @@ _cairo_ps_surface_show_glyphs (void
horizontal = TRUE;
vertical = TRUE;
- end = num_glyphs;
+ end = n_glyphs;
if (end - i > MAX_GLYPHS_PER_SHOW)
end = i + MAX_GLYPHS_PER_SHOW;
last = end - 1;
@@ -2220,13 +2222,13 @@ _cairo_ps_surface_show_glyphs (void
} else {
word_wrap = _word_wrap_stream_create (surface->stream, 79);
_cairo_output_stream_printf (word_wrap, "<");
- for (j = i; j <= last; j++)
+ for (j = i; j < last+1; j++)
_cairo_output_stream_printf (word_wrap, "%02x", glyph_ids[j].glyph_id);
_cairo_output_stream_printf (word_wrap, ">\n[");
if (horizontal) {
- for (j = i; j <= last; j++) {
- if (j == num_glyphs - 1)
+ for (j = i; j < last+1; j++) {
+ if (j == n_glyphs - 1)
_cairo_output_stream_printf (word_wrap, "0 ");
else
_cairo_output_stream_printf (word_wrap,
@@ -2234,8 +2236,8 @@ _cairo_ps_surface_show_glyphs (void
}
_cairo_output_stream_printf (word_wrap, "] xS\n");
} else if (vertical) {
- for (j = i; j <= last; j++) {
- if (j == num_glyphs - 1)
+ for (j = i; j < last+1; j++) {
+ if (j == n_glyphs - 1)
_cairo_output_stream_printf (word_wrap, "0 ");
else
_cairo_output_stream_printf (word_wrap,
@@ -2243,8 +2245,8 @@ _cairo_ps_surface_show_glyphs (void
}
_cairo_output_stream_printf (word_wrap, "] yS\n");
} else {
- for (j = i; j <= last; j++) {
- if (j == num_glyphs - 1)
+ for (j = i; j < last+1; j++) {
+ if (j == n_glyphs - 1)
_cairo_output_stream_printf (word_wrap, "0 ");
else
_cairo_output_stream_printf (word_wrap,
More information about the cairo-commit
mailing list