diff --git a/src/compositor/region-utils.c b/src/compositor/region-utils.c index 4f5a2346a..aabe76b52 100644 --- a/src/compositor/region-utils.c +++ b/src/compositor/region-utils.c @@ -26,6 +26,15 @@ #include +#define META_REGION_MAX_STACK_RECTS 256 + +#define META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED(n_rects, rects) \ + g_autofree cairo_rectangle_int_t *G_PASTE(__n, __LINE__) = NULL; \ + if (n_rects < META_REGION_MAX_STACK_RECTS) \ + rects = g_newa (cairo_rectangle_int_t, n_rects); \ + else \ + rects = G_PASTE(__n, __LINE__) = g_new (cairo_rectangle_int_t, n_rects); + /* MetaRegionBuilder */ /* Various algorithms in this file require unioning together a set of rectangles @@ -191,8 +200,7 @@ meta_region_scale_double (cairo_region_t *region, return cairo_region_copy (region); n_rects = cairo_region_num_rectangles (region); - - rects = g_malloc (sizeof(cairo_rectangle_int_t) * n_rects); + META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects); for (i = 0; i < n_rects; i++) { cairo_region_get_rectangle (region, i, &rects[i]); @@ -203,8 +211,6 @@ meta_region_scale_double (cairo_region_t *region, scaled_region = cairo_region_create_rectangles (rects, n_rects); - g_free (rects); - return scaled_region; } @@ -219,8 +225,7 @@ meta_region_scale (cairo_region_t *region, int scale) return cairo_region_copy (region); n_rects = cairo_region_num_rectangles (region); - - rects = g_malloc (sizeof(cairo_rectangle_int_t) * n_rects); + META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects); for (i = 0; i < n_rects; i++) { cairo_region_get_rectangle (region, i, &rects[i]); @@ -232,8 +237,6 @@ meta_region_scale (cairo_region_t *region, int scale) scaled_region = cairo_region_create_rectangles (rects, n_rects); - g_free (rects); - return scaled_region; } @@ -389,8 +392,7 @@ meta_region_transform (const cairo_region_t *region, return cairo_region_copy (region); n_rects = cairo_region_num_rectangles (region); - - rects = g_new0 (cairo_rectangle_int_t, n_rects); + META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects); for (i = 0; i < n_rects; i++) { cairo_region_get_rectangle (region, i, &rects[i]); @@ -404,8 +406,6 @@ meta_region_transform (const cairo_region_t *region, transformed_region = cairo_region_create_rectangles (rects, n_rects); - g_free (rects); - return transformed_region; } @@ -437,8 +437,7 @@ meta_region_crop_and_scale (cairo_region_t *region, } n_rects = cairo_region_num_rectangles (region); - - rects = g_new0 (cairo_rectangle_int_t, n_rects); + META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects); for (i = 0; i < n_rects; i++) { cairo_region_get_rectangle (region, i, &rects[i]); @@ -452,7 +451,5 @@ meta_region_crop_and_scale (cairo_region_t *region, viewport_region = cairo_region_create_rectangles (rects, n_rects); - g_free (rects); - return viewport_region; }