[cairo-commit] 7 commits - src/cairoint.h src/cairo-polygon.c
src/cairo-spline.c src/cairo-traps.c
Behdad Esfahbod
behdad at kemper.freedesktop.org
Tue Mar 13 03:09:07 PDT 2007
src/cairo-polygon.c | 48 ++++++++++++++++++++++++++++++------------------
src/cairo-spline.c | 4 ++--
src/cairo-traps.c | 2 +-
src/cairoint.h | 11 +++++------
4 files changed, 38 insertions(+), 27 deletions(-)
New commits:
diff-tree deed0f473400bc2e91dde8bad2537d499a5941ea (from b706bd9b38d505d2b09e932a8f362fac76ef64de)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 06:09:09 2007 -0400
[polygon,spline,traps] Avoid free(NULL)
diff --git a/src/cairo-polygon.c b/src/cairo-polygon.c
index 6ed0165..52c72b7 100644
--- a/src/cairo-polygon.c
+++ b/src/cairo-polygon.c
@@ -56,7 +56,7 @@ _cairo_polygon_init (cairo_polygon_t *po
void
_cairo_polygon_fini (cairo_polygon_t *polygon)
{
- if (polygon->edges != polygon->edges_embedded)
+ if (polygon->edges && polygon->edges != polygon->edges_embedded)
free (polygon->edges);
polygon->edges = NULL;
diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index 3fd0f13..3624bfc 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -90,7 +90,7 @@ _cairo_spline_init (cairo_spline_t *spli
void
_cairo_spline_fini (cairo_spline_t *spline)
{
- if (spline->points != spline->points_embedded)
+ if (spline->points && spline->points != spline->points_embedded)
free (spline->points);
spline->points = NULL;
diff --git a/src/cairo-traps.c b/src/cairo-traps.c
index 04228c1..b767db8 100644
--- a/src/cairo-traps.c
+++ b/src/cairo-traps.c
@@ -77,7 +77,7 @@ _cairo_traps_init (cairo_traps_t *traps)
void
_cairo_traps_fini (cairo_traps_t *traps)
{
- if (traps->traps != traps->traps_embedded)
+ if (traps->traps && traps->traps != traps->traps_embedded)
free (traps->traps);
traps->traps = NULL;
diff-tree b706bd9b38d505d2b09e932a8f362fac76ef64de (from d8165d5424718404946c3a72d59396fc604d1bce)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 06:07:03 2007 -0400
[cairo-polygon] Add a cache of eight edges to cairo_polygon_t
Many, if not most, of polygons have very few edges. This avoids
calling malloc() for those cases.
diff --git a/src/cairo-polygon.c b/src/cairo-polygon.c
index 99c4298..6ed0165 100644
--- a/src/cairo-polygon.c
+++ b/src/cairo-polygon.c
@@ -56,26 +56,42 @@ _cairo_polygon_init (cairo_polygon_t *po
void
_cairo_polygon_fini (cairo_polygon_t *polygon)
{
- if (polygon->edges_size) {
+ if (polygon->edges != polygon->edges_embedded)
free (polygon->edges);
- polygon->edges = NULL;
- polygon->edges_size = 0;
- polygon->num_edges = 0;
- }
+
+ polygon->edges = NULL;
+ polygon->edges_size = 0;
+ polygon->num_edges = 0;
polygon->has_current_point = FALSE;
}
+/* make room for at least one more edge */
static cairo_status_t
_cairo_polygon_grow (cairo_polygon_t *polygon)
{
cairo_edge_t *new_edges;
int old_size = polygon->edges_size;
- int new_size = old_size ? 2 * old_size : 16;
+ int embedded_size = sizeof (polygon->edges_embedded) / sizeof (polygon->edges_embedded[0]);
+ int new_size = 2 * MAX (old_size, 16);
+
+ /* we have a local buffer at polygon->edges_embedded. try to fulfill the request
+ * from there. */
+ if (old_size < embedded_size) {
+ polygon->edges = polygon->edges_embedded;
+ polygon->edges_size = embedded_size;
+ return CAIRO_STATUS_SUCCESS;
+ }
assert (polygon->num_edges <= polygon->edges_size);
- new_edges = realloc (polygon->edges, new_size * sizeof (cairo_edge_t));
+ if (polygon->edges == polygon->edges_embedded) {
+ new_edges = malloc (new_size * sizeof (cairo_edge_t));
+ if (new_edges)
+ memcpy (new_edges, polygon->edges, old_size * sizeof (cairo_edge_t));
+ } else {
+ new_edges = realloc (polygon->edges, new_size * sizeof (cairo_edge_t));
+ }
if (new_edges == NULL) {
return CAIRO_STATUS_NO_MEMORY;
diff --git a/src/cairoint.h b/src/cairoint.h
index 7a4c9d5..3251950 100755
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -428,6 +428,7 @@ typedef struct _cairo_polygon {
int num_edges;
int edges_size;
cairo_edge_t *edges;
+ cairo_edge_t edges_embedded[8];
} cairo_polygon_t;
typedef struct _cairo_spline {
diff-tree d8165d5424718404946c3a72d59396fc604d1bce (from cca7e3fec3a3ec68f812690aa7efafa7dc237849)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 05:59:33 2007 -0400
[cairo-spline] Fix typo in comment
diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index d8dd477..3fd0f13 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -98,7 +98,7 @@ _cairo_spline_fini (cairo_spline_t *spli
spline->num_points = 0;
}
-/* make room for at least one more trap */
+/* make room for at least one more point */
static cairo_status_t
_cairo_spline_grow (cairo_spline_t *spline)
{
diff-tree cca7e3fec3a3ec68f812690aa7efafa7dc237849 (from 81e15a454b188aa8dd03eb599d0fb4af834695f1)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 05:56:38 2007 -0400
[cairo_polygon_t] Change has_current_point from int to cairo_bool_t
diff --git a/src/cairo-polygon.c b/src/cairo-polygon.c
index bdd438a..99c4298 100644
--- a/src/cairo-polygon.c
+++ b/src/cairo-polygon.c
@@ -50,7 +50,7 @@ _cairo_polygon_init (cairo_polygon_t *po
polygon->edges_size = 0;
polygon->edges = NULL;
- polygon->has_current_point = 0;
+ polygon->has_current_point = FALSE;
}
void
@@ -63,7 +63,7 @@ _cairo_polygon_fini (cairo_polygon_t *po
polygon->num_edges = 0;
}
- polygon->has_current_point = 0;
+ polygon->has_current_point = FALSE;
}
static cairo_status_t
@@ -130,7 +130,7 @@ _cairo_polygon_move_to (cairo_polygon_t
if (! polygon->has_current_point)
polygon->first_point = *point;
polygon->current_point = *point;
- polygon->has_current_point = 1;
+ polygon->has_current_point = TRUE;
return CAIRO_STATUS_SUCCESS;
}
@@ -161,7 +161,7 @@ _cairo_polygon_close (cairo_polygon_t *p
if (status)
return status;
- polygon->has_current_point = 0;
+ polygon->has_current_point = FALSE;
}
return CAIRO_STATUS_SUCCESS;
diff --git a/src/cairoint.h b/src/cairoint.h
index cc971ed..7a4c9d5 100755
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -423,7 +423,7 @@ typedef struct _cairo_edge {
typedef struct _cairo_polygon {
cairo_point_t first_point;
cairo_point_t current_point;
- int has_current_point;
+ cairo_bool_t has_current_point;
int num_edges;
int edges_size;
diff-tree 81e15a454b188aa8dd03eb599d0fb4af834695f1 (from 2bf4ce389d05e07698e03374c3d05436f5d2b2e4)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 05:55:45 2007 -0400
[cairo_polygon_t] Remove unused member 'closed'
diff --git a/src/cairoint.h b/src/cairoint.h
index 9319931..cc971ed 100755
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -425,8 +425,6 @@ typedef struct _cairo_polygon {
cairo_point_t current_point;
int has_current_point;
- int closed;
-
int num_edges;
int edges_size;
cairo_edge_t *edges;
diff-tree 2bf4ce389d05e07698e03374c3d05436f5d2b2e4 (from 3ce84390de8be69f369a4da80c3039b716ebb0e4)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 05:53:13 2007 -0400
[cairo_polygon_t] Reorder struct members for clarity
diff --git a/src/cairoint.h b/src/cairoint.h
index aee5ad9..9319931 100755
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -421,15 +421,15 @@ typedef struct _cairo_edge {
} cairo_edge_t;
typedef struct _cairo_polygon {
- int num_edges;
- int edges_size;
- cairo_edge_t *edges;
-
cairo_point_t first_point;
cairo_point_t current_point;
int has_current_point;
int closed;
+
+ int num_edges;
+ int edges_size;
+ cairo_edge_t *edges;
} cairo_polygon_t;
typedef struct _cairo_spline {
diff-tree 3ce84390de8be69f369a4da80c3039b716ebb0e4 (from 6976ab1392e0fd07a081e6d6056510070e1d6df9)
Author: Behdad Esfahbod <behdad at behdad.org>
Date: Tue Mar 13 05:51:39 2007 -0400
[cairo-polygon] Clean-up realloc algorithm for clarity
diff --git a/src/cairo-polygon.c b/src/cairo-polygon.c
index dc9d380..bdd438a 100644
--- a/src/cairo-polygon.c
+++ b/src/cairo-polygon.c
@@ -40,7 +40,7 @@
/* private functions */
static cairo_status_t
-_cairo_polygon_grow_by (cairo_polygon_t *polygon, int additional);
+_cairo_polygon_grow (cairo_polygon_t *polygon);
void
_cairo_polygon_init (cairo_polygon_t *polygon)
@@ -67,25 +67,22 @@ _cairo_polygon_fini (cairo_polygon_t *po
}
static cairo_status_t
-_cairo_polygon_grow_by (cairo_polygon_t *polygon, int additional)
+_cairo_polygon_grow (cairo_polygon_t *polygon)
{
cairo_edge_t *new_edges;
int old_size = polygon->edges_size;
- int new_size = polygon->num_edges + additional;
+ int new_size = old_size ? 2 * old_size : 16;
- if (new_size <= polygon->edges_size) {
- return CAIRO_STATUS_SUCCESS;
- }
+ assert (polygon->num_edges <= polygon->edges_size);
- polygon->edges_size = new_size;
- new_edges = realloc (polygon->edges, polygon->edges_size * sizeof (cairo_edge_t));
+ new_edges = realloc (polygon->edges, new_size * sizeof (cairo_edge_t));
if (new_edges == NULL) {
- polygon->edges_size = old_size;
return CAIRO_STATUS_NO_MEMORY;
}
polygon->edges = new_edges;
+ polygon->edges_size = new_size;
return CAIRO_STATUS_SUCCESS;
}
@@ -102,8 +99,7 @@ _cairo_polygon_add_edge (cairo_polygon_t
}
if (polygon->num_edges >= polygon->edges_size) {
- int additional = polygon->edges_size ? polygon->edges_size : 16;
- status = _cairo_polygon_grow_by (polygon, additional);
+ status = _cairo_polygon_grow (polygon);
if (status) {
return status;
}
More information about the cairo-commit
mailing list