[cairo] Use stack buffer for _cairo_surface_fill_region()

Chris Wilson chris at chris-wilson.co.uk
Tue Mar 13 18:50:27 PDT 2007


I don't know just how many times this patch may arrive at the list as I
was experimenting with git-format-patch and piping to mutt. Since I've
not seen the message yet, I assume the experiment failed.

This is a simple patch to use a stack buffer for
_cairo_surface_fill_region() when the num_rects does not exceed
CAIRO_STACK_BUFFER_SIZE / sizeof (cairo_rectangle_in16_t). This is
another function that is called for almost every GTK+ expose event
(assuming double-buffered widgets) and so a frequent allocator.
--
Chris Wilson
-------------- next part --------------
>From ef3ecca582496f2370a5ed8b05aa3f791c9e3d65 Mon Sep 17 00:00:00 2001
From: Chris Wilson <chris at chris-wilson.co.uk>
Date: Wed, 14 Mar 2007 01:31:58 +0000
Subject: [PATCH] Use a stack buffer for small numbers of rectangles.

---
 src/cairo-surface.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/cairo-surface.c b/src/cairo-surface.c
index 5dbac58..da093db 100644
--- a/src/cairo-surface.c
+++ b/src/cairo-surface.c
@@ -1164,6 +1164,7 @@ _cairo_surface_fill_region (cairo_surface_t	   *surface,
 {
     int num_rects = pixman_region_num_rects (region);
     pixman_box16_t *boxes = pixman_region_rects (region);
+    cairo_rectangle_int16_t stack_rects[CAIRO_STACK_BUFFER_SIZE / sizeof (cairo_rectangle_int16_t)];
     cairo_rectangle_int16_t *rects;
     cairo_status_t status;
     int i;
@@ -1173,9 +1174,12 @@ _cairo_surface_fill_region (cairo_surface_t	   *surface,
     if (!num_rects)
 	return CAIRO_STATUS_SUCCESS;
 
-    rects = malloc (sizeof (pixman_rectangle_t) * num_rects);
-    if (!rects)
-	return CAIRO_STATUS_NO_MEMORY;
+    rects = stack_rects;
+    if (num_rects > sizeof (stack_rects) / sizeof (stack_rects[0])) {
+	rects = malloc (sizeof (cairo_rectangle_int16_t) * num_rects);
+	if (!rects)
+	    return CAIRO_STATUS_NO_MEMORY;
+    }
 
     for (i = 0; i < num_rects; i++) {
 	rects[i].x = boxes[i].x1;
@@ -1187,7 +1191,8 @@ _cairo_surface_fill_region (cairo_surface_t	   *surface,
     status =  _cairo_surface_fill_rectangles (surface, op,
 					      color, rects, num_rects);
 
-    free (rects);
+    if (rects != stack_rects)
+	free (rects);
 
     return status;
 }
-- 
1.4.4.2



More information about the cairo mailing list