mirror of
https://gitlab.freedesktop.org/cairo/cairo.git
synced 2026-05-05 00:38:06 +02:00
[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.
This commit is contained in:
parent
1e64ecf075
commit
9eee167210
2 changed files with 24 additions and 5 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -441,6 +441,7 @@ typedef struct _cairo_spline {
|
|||
int num_points;
|
||||
int points_size;
|
||||
cairo_point_t *points;
|
||||
cairo_point_t points_embedded[8];
|
||||
} cairo_spline_t;
|
||||
|
||||
typedef struct _cairo_pen_vertex {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue