diff options
author | Behdad Esfahbod <behdad@behdad.org> | 2007-03-13 06:07:03 -0400 |
---|---|---|
committer | Behdad Esfahbod <behdad@behdad.org> | 2007-03-13 06:07:03 -0400 |
commit | b706bd9b38d505d2b09e932a8f362fac76ef64de (patch) | |
tree | e36cf4f8883de0fdf318ac062406ee93a9765c6b /src/cairo-polygon.c | |
parent | d8165d5424718404946c3a72d59396fc604d1bce (diff) |
[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.
Diffstat (limited to 'src/cairo-polygon.c')
-rw-r--r-- | src/cairo-polygon.c | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/src/cairo-polygon.c b/src/cairo-polygon.c index 99c42981..6ed0165e 100644 --- a/src/cairo-polygon.c +++ b/src/cairo-polygon.c @@ -56,26 +56,42 @@ _cairo_polygon_init (cairo_polygon_t *polygon) 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; |