summaryrefslogtreecommitdiff
path: root/src/cairo-hull.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2008-05-07 09:18:30 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2008-06-13 21:34:41 +0100
commitba6b2d092ab45e9d28ab5c016315458d1ad670ff (patch)
tree1b1d6ac9b8aa284371acc5b598b692b8d6c61fed /src/cairo-hull.c
parentb2eadb94f8e34d7c997b5ccfbca4d21e173fdd56 (diff)
downloadcairo-ba6b2d092ab45e9d28ab5c016315458d1ad670ff.tar.gz
[cairo-hull] Attempt to allocate vertices on stack.
First try to allocate the vertices using an on-stack array, otherwise, if we need more vertices than can be accomodated, fallback to using a heap array.
Diffstat (limited to 'src/cairo-hull.c')
-rw-r--r--src/cairo-hull.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/cairo-hull.c b/src/cairo-hull.c
index ccdb34d45..77ee7908a 100644
--- a/src/cairo-hull.c
+++ b/src/cairo-hull.c
@@ -36,22 +36,20 @@
#include "cairoint.h"
-typedef struct cairo_hull
-{
+typedef struct cairo_hull {
cairo_point_t point;
cairo_slope_t slope;
int discard;
int id;
} cairo_hull_t;
-static cairo_status_t
-_cairo_hull_create (cairo_pen_vertex_t *vertices,
- int num_vertices,
- cairo_hull_t **out)
+static void
+_cairo_hull_init (cairo_hull_t *hull,
+ cairo_pen_vertex_t *vertices,
+ int num_vertices)
{
- int i;
- cairo_hull_t *hull;
cairo_point_t *p, *extremum, tmp;
+ int i;
extremum = &vertices[0].point;
for (i = 1; i < num_vertices; i++) {
@@ -64,10 +62,6 @@ _cairo_hull_create (cairo_pen_vertex_t *vertices,
*extremum = vertices[0].point;
vertices[0].point = tmp;
- hull = _cairo_malloc_ab (num_vertices, sizeof (cairo_hull_t));
- if (hull == NULL)
- return _cairo_error (CAIRO_STATUS_NO_MEMORY);
-
for (i = 0; i < num_vertices; i++) {
hull[i].point = vertices[i].point;
_cairo_slope_init (&hull[i].slope, &hull[0].point, &hull[i].point);
@@ -82,9 +76,6 @@ _cairo_hull_create (cairo_pen_vertex_t *vertices,
if (i != 0 && hull[i].slope.dx == 0 && hull[i].slope.dy == 0)
hull[i].discard = 1;
}
-
- *out = hull;
- return CAIRO_STATUS_SUCCESS;
}
static int
@@ -196,13 +187,19 @@ _cairo_hull_to_pen (cairo_hull_t *hull, cairo_pen_vertex_t *vertices, int *num_v
cairo_status_t
_cairo_hull_compute (cairo_pen_vertex_t *vertices, int *num_vertices)
{
- cairo_status_t status;
- cairo_hull_t *hull = NULL;
+ cairo_hull_t hull_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_hull_t)];
+ cairo_hull_t *hull;
int num_hull = *num_vertices;
- status = _cairo_hull_create (vertices, num_hull, &hull);
- if (status)
- return status;
+ if (num_hull > ARRAY_LENGTH (hull_stack)) {
+ hull = _cairo_malloc_ab (num_hull, sizeof (cairo_hull_t));
+ if (hull == NULL)
+ return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+ } else {
+ hull = hull_stack;
+ }
+
+ _cairo_hull_init (hull, vertices, num_hull);
qsort (hull + 1, num_hull - 1,
sizeof (cairo_hull_t), _cairo_hull_vertex_compare);
@@ -211,7 +208,8 @@ _cairo_hull_compute (cairo_pen_vertex_t *vertices, int *num_vertices)
_cairo_hull_to_pen (hull, vertices, num_vertices);
- free (hull);
+ if (hull != hull_stack)
+ free (hull);
return CAIRO_STATUS_SUCCESS;
}