[cairo] [cairo-commit] Branch '1.10' - src/cairo-image-surface.c

Behdad Esfahbod behdad at behdad.org
Tue Nov 2 07:03:32 PDT 2010


On 11/02/10 05:05, Chris Wilson wrote:
>     
>     In 06e9caf86199e8261a07db6d4774628fa147728d the type of the variables
>     was changed, but the type used to compute the allocation size was not.
...
> -    pixman_box32_t stack_boxes[CAIRO_STACK_ARRAY_LENGTH (pixman_rectangle16_t)];
> +    pixman_box32_t stack_boxes[CAIRO_STACK_ARRAY_LENGTH (pixman_box32_t)];

Maybe we should use a better macro then to avoid such errors.  Something like:

	CAIRO_STACK_ARRAY (pixman_box32_t, stack_boxes);


>      pixman_box32_t *pixman_boxes = stack_boxes;
...
>      if (num_rects > ARRAY_LENGTH (stack_boxes)) {
> -	pixman_boxes = _cairo_malloc_ab (num_rects, sizeof (pixman_rectangle16_t));
> +	pixman_boxes = _cairo_malloc_ab (num_rects, sizeof (pixman_box32_t));
>  	if (unlikely (pixman_boxes == NULL))
>  	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
>      }

In fact, most of these can also be macroized.  Something like:

#define CAIRO_STACK_ARRAY_DEFINE(Type, Name) \
	Type stack_##Name[CAIRO_STACK_ARRAY_LENGTH (Type)]; \
	Type *Name = stack_##Name;

#define CAIRO_STACK_ARRAY_ENSURE(Name, Size) \
        (likely ((Size) <= ARRAY_LENGTH (stack_##Name)) ? \
	  CAIRO_STATUS_SUCCESS : \
	  (Name = _cairo_malloc_ab ((Size), sizeof (*Name)), \
	   (unlikely (Name == NULL) ? \
	     _cairo_error (CAIRO_STATUS_NO_MEMORY) : \
	     CAIRO_STATUS_SUCCESS)))

#define CAIRO_STACK_ARRAY_FREE(Name) \
	do { if (Name != stack_##Name) free (Name); } while (0)

ENSURE can be rewritten to do realloc if necessary.

behdad


More information about the cairo mailing list