summaryrefslogtreecommitdiff
path: root/src/cairo-spline.c
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2007-03-08 22:34:44 -0500
committerBehdad Esfahbod <behdad@behdad.org>2007-03-13 05:14:20 -0400
commit9eee167210b6cc562014652572872cf0a7ccb00e (patch)
tree8c03b00db3b3183fd34d10ddef855bf3a8118603 /src/cairo-spline.c
parent1e64ecf0758a208b469ae0a87a747b3a70c70ceb (diff)
[cairo-spline] Add a cache of eight points to cairo_spline_t
Most of the splines need not more than eight points. This avoids calling malloc() for those cases, and eight-points take only 64 bytes.
Diffstat (limited to 'src/cairo-spline.c')
-rw-r--r--src/cairo-spline.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index 726dc8606..d8dd477eb 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -90,22 +90,40 @@ _cairo_spline_init (cairo_spline_t *spline,
void
_cairo_spline_fini (cairo_spline_t *spline)
{
- spline->num_points = 0;
- spline->points_size = 0;
- free (spline->points);
+ if (spline->points != spline->points_embedded)
+ free (spline->points);
+
spline->points = NULL;
+ spline->points_size = 0;
+ spline->num_points = 0;
}
+/* make room for at least one more trap */
static cairo_status_t
_cairo_spline_grow (cairo_spline_t *spline)
{
cairo_point_t *new_points;
int old_size = spline->points_size;
- int new_size = old_size ? 2 * old_size : 32;
+ int embedded_size = sizeof (spline->points_embedded) / sizeof (spline->points_embedded[0]);
+ int new_size = 2 * MAX (old_size, 16);
+
+ /* we have a local buffer at spline->points_embedded. try to fulfill the request
+ * from there. */
+ if (old_size < embedded_size) {
+ spline->points = spline->points_embedded;
+ spline->points_size = embedded_size;
+ return CAIRO_STATUS_SUCCESS;
+ }
assert (spline->num_points <= spline->points_size);
- new_points = realloc (spline->points, new_size * sizeof (cairo_point_t));
+ if (spline->points == spline->points_embedded) {
+ new_points = malloc (new_size * sizeof (cairo_point_t));
+ if (new_points)
+ memcpy (new_points, spline->points, old_size * sizeof (cairo_point_t));
+ } else {
+ new_points = realloc (spline->points, new_size * sizeof (cairo_point_t));
+ }
if (new_points == NULL) {
return CAIRO_STATUS_NO_MEMORY;