[cairo] [PATCH 1/5] Improve cairo_rectangle_list_t error handling

Andrea Canciani ranma42 at gmail.com
Fri Mar 18 10:13:19 PDT 2011


Unify the _cairo_rectangle_list_create_in_error() functions, keeping
the best features from both (the one in cairo-clip.c statically
allocates the most common cases, the one in cairo.c throws a NO_MEMORY
error when it cannot malloc() instead of rethrowing the same error).

The same function can be used to return an error-list in
_cairo_gstate_copy_clip_rectangle_list() when _cairo_clip_rectangle()
fails (for example becaouse of an OOM).
---
 src/cairo-clip-private.h |    3 +++
 src/cairo-clip.c         |    5 ++---
 src/cairo-gstate.c       |    8 +++++++-
 src/cairo.c              |   20 --------------------
 4 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/src/cairo-clip-private.h b/src/cairo-clip-private.h
index faf4864..bfbb539 100644
--- a/src/cairo-clip-private.h
+++ b/src/cairo-clip-private.h
@@ -148,4 +148,7 @@ _cairo_clip_drop_cache (cairo_clip_t  *clip);
 cairo_private cairo_rectangle_list_t*
 _cairo_clip_copy_rectangle_list (cairo_clip_t *clip, cairo_gstate_t *gstate);
 
+cairo_private cairo_rectangle_list_t *
+_cairo_rectangle_list_create_in_error (cairo_status_t status);
+
 #endif /* CAIRO_CLIP_PRIVATE_H */
diff --git a/src/cairo-clip.c b/src/cairo-clip.c
index 696c5b1..c76beee 100644
--- a/src/cairo-clip.c
+++ b/src/cairo-clip.c
@@ -1465,8 +1465,7 @@ _cairo_clip_to_boxes (cairo_clip_t **clip,
     return status;
 }
 
-
-static cairo_rectangle_list_t *
+cairo_rectangle_list_t *
 _cairo_rectangle_list_create_in_error (cairo_status_t status)
 {
     cairo_rectangle_list_t *list;
@@ -1478,7 +1477,7 @@ _cairo_rectangle_list_create_in_error (cairo_status_t status)
 
     list = malloc (sizeof (*list));
     if (unlikely (list == NULL)) {
-	_cairo_error_throw (status);
+	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
 	return (cairo_rectangle_list_t*) &_cairo_rectangles_nil;
     }
 
diff --git a/src/cairo-gstate.c b/src/cairo-gstate.c
index eaf7a56..9fd3600 100644
--- a/src/cairo-gstate.c
+++ b/src/cairo-gstate.c
@@ -1560,11 +1560,17 @@ _cairo_gstate_copy_clip_rectangle_list (cairo_gstate_t *gstate)
     cairo_clip_t clip;
     cairo_rectangle_int_t extents;
     cairo_rectangle_list_t *list;
+    cairo_status_t status;
 
     _cairo_clip_init_copy (&clip, &gstate->clip);
 
     if (_cairo_surface_get_extents (gstate->target, &extents))
-        _cairo_clip_rectangle (&clip, &extents);
+	status = _cairo_clip_rectangle (&clip, &extents);
+    else
+	status = CAIRO_STATUS_SUCCESS;
+
+    if (unlikely (status))
+	return _cairo_rectangle_list_create_in_error (status);
 
     list = _cairo_clip_copy_rectangle_list (&clip, gstate);
     _cairo_clip_fini (&clip);
diff --git a/src/cairo.c b/src/cairo.c
index 9e12c6e..5969fe5 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -2843,26 +2843,6 @@ cairo_in_clip (cairo_t *cr, double x, double y)
     return _cairo_gstate_in_clip (cr->gstate, x, y);
 }
 
-static cairo_rectangle_list_t *
-_cairo_rectangle_list_create_in_error (cairo_status_t status)
-{
-    cairo_rectangle_list_t *list;
-
-    if (status == CAIRO_STATUS_NO_MEMORY)
-        return (cairo_rectangle_list_t*) &_cairo_rectangles_nil;
-
-    list = malloc (sizeof (cairo_rectangle_list_t));
-    if (unlikely (list == NULL)) {
-	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
-        return (cairo_rectangle_list_t*) &_cairo_rectangles_nil;
-    }
-
-    list->status = status;
-    list->rectangles = NULL;
-    list->num_rectangles = 0;
-    return list;
-}
-
 /**
  * cairo_copy_clip_rectangle_list:
  * @cr: a cairo context
-- 
1.7.1



More information about the cairo mailing list