[cairo-commit] 3 commits - src/cairo-array.c src/cairoint.h src/cairo-types-private.h

Andrea Canciani ranma42 at kemper.freedesktop.org
Wed Nov 24 05:47:17 PST 2010


 src/cairo-array.c         |  127 ++++++++++++++++++++++------------------------
 src/cairo-types-private.h |    4 -
 src/cairoint.h            |   25 ++++-----
 3 files changed, 75 insertions(+), 81 deletions(-)

New commits:
commit 493aaf0f15bfedc88371ffab07d862a400b0da38
Author: Andrea Canciani <ranma42 at gmail.com>
Date:   Wed Nov 24 12:08:03 2010 +0100

    array: Cleanup types
    
    Const-ify where appropriate and make all index and element counts
    unsigned int.
    
    This is needed to enable accessing const cairo_array_t's without
    having to manually remove the const qualifier (which happens in the
    to-be-merged mesh pattern code, for example).

diff --git a/src/cairo-array.c b/src/cairo-array.c
index fde5d73..673ae5e 100644
--- a/src/cairo-array.c
+++ b/src/cairo-array.c
@@ -1,3 +1,4 @@
+/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
 /* cairo - a vector graphics library with display and print output
  *
  * Copyright © 2004 Red Hat, Inc
@@ -53,7 +54,7 @@
  * called to free resources allocated during use of the array.
  **/
 void
-_cairo_array_init (cairo_array_t *array, int element_size)
+_cairo_array_init (cairo_array_t *array, unsigned int element_size)
 {
     array->size = 0;
     array->num_elements = 0;
@@ -234,7 +235,9 @@ _cairo_array_index_const (const cairo_array_t *array, unsigned int index)
  * location pointed to by @dst.
  **/
 void
-_cairo_array_copy_element (cairo_array_t *array, int index, void *dst)
+_cairo_array_copy_element (const cairo_array_t *array,
+			   unsigned int         index,
+			   void                *dst)
 {
     memcpy (dst, _cairo_array_index_const (array, index), array->element_size);
 }
@@ -276,7 +279,7 @@ _cairo_array_append (cairo_array_t	*array,
 cairo_status_t
 _cairo_array_append_multiple (cairo_array_t	*array,
 			      const void	*elements,
-			      int		 num_elements)
+			      unsigned int	 num_elements)
 {
     cairo_status_t status;
     void *dest;
@@ -330,8 +333,8 @@ _cairo_array_allocate (cairo_array_t	 *array,
  *
  * This space was left intentionally blank, but gtk-doc filled it.
  **/
-int
-_cairo_array_num_elements (cairo_array_t *array)
+unsigned int
+_cairo_array_num_elements (const cairo_array_t *array)
 {
     return array->num_elements;
 }
@@ -344,8 +347,8 @@ _cairo_array_num_elements (cairo_array_t *array)
  *
  * This space was left intentionally blank, but gtk-doc filled it.
  **/
-int
-_cairo_array_size (cairo_array_t *array)
+unsigned int
+_cairo_array_size (const cairo_array_t *array)
 {
     return array->size;
 }
@@ -490,7 +493,7 @@ _cairo_user_data_array_set_data (cairo_user_data_array_t     *array,
 
 cairo_status_t
 _cairo_user_data_array_copy (cairo_user_data_array_t	*dst,
-			     cairo_user_data_array_t	*src)
+			     const cairo_user_data_array_t	*src)
 {
     /* discard any existing user-data */
     if (dst->num_elements != 0) {
diff --git a/src/cairoint.h b/src/cairoint.h
index 85e9856..cf0d459 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -301,7 +301,7 @@ _cairo_box_intersects_line_segment (cairo_box_t *box,
 /* cairo-array.c structures and functions */
 
 cairo_private void
-_cairo_array_init (cairo_array_t *array, int element_size);
+_cairo_array_init (cairo_array_t *array, unsigned int element_size);
 
 cairo_private void
 _cairo_array_fini (cairo_array_t *array);
@@ -318,7 +318,7 @@ _cairo_array_append (cairo_array_t *array, const void *element);
 cairo_private cairo_status_t
 _cairo_array_append_multiple (cairo_array_t	*array,
 			      const void	*elements,
-			      int		 num_elements);
+			      unsigned int	 num_elements);
 
 cairo_private cairo_status_t
 _cairo_array_allocate (cairo_array_t	 *array,
@@ -332,13 +332,13 @@ cairo_private const void *
 _cairo_array_index_const (const cairo_array_t *array, unsigned int index);
 
 cairo_private void
-_cairo_array_copy_element (cairo_array_t *array, int index, void *dst);
+_cairo_array_copy_element (const cairo_array_t *array, unsigned int index, void *dst);
 
-cairo_private int
-_cairo_array_num_elements (cairo_array_t *array);
+cairo_private unsigned int
+_cairo_array_num_elements (const cairo_array_t *array);
 
-cairo_private int
-_cairo_array_size (cairo_array_t *array);
+cairo_private unsigned int
+_cairo_array_size (const cairo_array_t *array);
 
 typedef struct {
     const cairo_user_data_key_t *key;
@@ -363,8 +363,8 @@ _cairo_user_data_array_set_data (cairo_user_data_array_t     *array,
 				 cairo_destroy_func_t	      destroy);
 
 cairo_private cairo_status_t
-_cairo_user_data_array_copy (cairo_user_data_array_t	*dst,
-			     cairo_user_data_array_t	*src);
+_cairo_user_data_array_copy (cairo_user_data_array_t		*dst,
+			     const cairo_user_data_array_t	*src);
 
 cairo_private void
 _cairo_user_data_array_foreach (cairo_user_data_array_t     *array,
commit 9960f299ac3476d6d4c0d25d95a5891f6eab15dd
Author: Andrea Canciani <ranma42 at gmail.com>
Date:   Wed Nov 24 12:07:51 2010 +0100

    array: Add read-only accessor
    
    It is sometimes useful to read the elements of a const cairo_array_t,
    but it is currently only possible by ignoring the const qualifier.
    The _cairo_array_index_const function allows read-only access to the
    array elements.
    
    This is needed to enable accessing const cairo_array_t's without
    having to manually remove the const qualifier (which happens in the
    to-be-merged mesh pattern code, for example).

diff --git a/src/cairo-array.c b/src/cairo-array.c
index 44ea3bd..fde5d73 100644
--- a/src/cairo-array.c
+++ b/src/cairo-array.c
@@ -183,6 +183,50 @@ _cairo_array_index (cairo_array_t *array, unsigned int index)
 }
 
 /**
+ * _cairo_array_index_const:
+ * @array: a #cairo_array_t
+ * Returns: A pointer to the object stored at @index.
+ *
+ * If the resulting value is assigned to a pointer to an object of the same
+ * element_size as initially passed to _cairo_array_init() then that
+ * pointer may be used for further direct indexing with []. For
+ * example:
+ *
+ * <informalexample><programlisting>
+ *	cairo_array_t array;
+ *	const double *values;
+ *
+ *	_cairo_array_init (&array, sizeof(double));
+ *	... calls to _cairo_array_append() here ...
+ *
+ *	values = _cairo_array_index_const (&array, 0);
+ *      for (i = 0; i < _cairo_array_num_elements (&array); i++)
+ *	    ... read values[i] here ...
+ * </programlisting></informalexample>
+ **/
+const void *
+_cairo_array_index_const (const cairo_array_t *array, unsigned int index)
+{
+    /* We allow an index of 0 for the no-elements case.
+     * This makes for cleaner calling code which will often look like:
+     *
+     *    elements = _cairo_array_index_const (array, num_elements);
+     *	  for (i=0; i < num_elements; i++) {
+     *        ... read elements[i] here ...
+     *    }
+     *
+     * which in the num_elements==0 case gets the NULL pointer here,
+     * but never dereferences it.
+     */
+    if (index == 0 && array->num_elements == 0)
+	return NULL;
+
+    assert (index < array->num_elements);
+
+    return array->elements + index * array->element_size;
+}
+
+/**
  * _cairo_array_copy_element:
  * @array: a #cairo_array_t
  *
@@ -192,7 +236,7 @@ _cairo_array_index (cairo_array_t *array, unsigned int index)
 void
 _cairo_array_copy_element (cairo_array_t *array, int index, void *dst)
 {
-    memcpy (dst, _cairo_array_index (array, index), array->element_size);
+    memcpy (dst, _cairo_array_index_const (array, index), array->element_size);
 }
 
 /**
@@ -454,11 +498,8 @@ _cairo_user_data_array_copy (cairo_user_data_array_t	*dst,
 	_cairo_user_data_array_init (dst);
     }
 
-    if (src->num_elements == 0)
-	return CAIRO_STATUS_SUCCESS;
-
     return _cairo_array_append_multiple (dst,
-					 _cairo_array_index (src, 0),
+					 _cairo_array_index_const (src, 0),
 					 src->num_elements);
 }
 
diff --git a/src/cairoint.h b/src/cairoint.h
index 030c265..85e9856 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -328,6 +328,9 @@ _cairo_array_allocate (cairo_array_t	 *array,
 cairo_private void *
 _cairo_array_index (cairo_array_t *array, unsigned int index);
 
+cairo_private const void *
+_cairo_array_index_const (const cairo_array_t *array, unsigned int index);
+
 cairo_private void
 _cairo_array_copy_element (cairo_array_t *array, int index, void *dst);
 
commit ae6a0de23b11e1cf34a2751fd6d4339b20340e67
Author: Andrea Canciani <ranma42 at gmail.com>
Date:   Wed Nov 24 11:16:56 2010 +0100

    array: Remove snapshot support
    
    Array snapshots are not used anymore and just bloat the implementation
    of cairo_array_t.
    
    In particular, double indirection was needed to implement array
    snapshots, as explained in c78685399307431903613733ddc936a104376943.

diff --git a/src/cairo-array.c b/src/cairo-array.c
index 942c9b9..44ea3bd 100644
--- a/src/cairo-array.c
+++ b/src/cairo-array.c
@@ -59,29 +59,6 @@ _cairo_array_init (cairo_array_t *array, int element_size)
     array->num_elements = 0;
     array->element_size = element_size;
     array->elements = NULL;
-
-    array->is_snapshot = FALSE;
-}
-
-/**
- * _cairo_array_init_snapshot:
- * @array: A #cairo_array_t to be initialized as a snapshot
- * @other: The #cairo_array_t from which to create the snapshot
- *
- * Initialize @array as an immutable copy of @other. It is an error to
- * call an array-modifying function (other than _cairo_array_fini) on
- * @array after calling this function.
- **/
-void
-_cairo_array_init_snapshot (cairo_array_t	*array,
-			    const cairo_array_t *other)
-{
-    array->size = other->size;
-    array->num_elements = other->num_elements;
-    array->element_size = other->element_size;
-    array->elements = other->elements;
-
-    array->is_snapshot = TRUE;
 }
 
 /**
@@ -95,13 +72,7 @@ _cairo_array_init_snapshot (cairo_array_t	*array,
 void
 _cairo_array_fini (cairo_array_t *array)
 {
-    if (array->is_snapshot)
-	return;
-
-    if (array->elements) {
-	free (* array->elements);
-	free (array->elements);
-    }
+    free (array->elements);
 }
 
 /**
@@ -120,8 +91,6 @@ _cairo_array_grow_by (cairo_array_t *array, unsigned int additional)
     unsigned int required_size = array->num_elements + additional;
     unsigned int new_size;
 
-    assert (! array->is_snapshot);
-
     /* check for integer overflow */
     if (required_size > INT_MAX || required_size < array->num_elements)
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
@@ -140,16 +109,8 @@ _cairo_array_grow_by (cairo_array_t *array, unsigned int additional)
     while (new_size < required_size)
 	new_size = new_size * 2;
 
-    if (array->elements == NULL) {
-	array->elements = malloc (sizeof (char *));
-	if (unlikely (array->elements == NULL))
-	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
-
-	*array->elements = NULL;
-    }
-
     array->size = new_size;
-    new_elements = _cairo_realloc_ab (*array->elements,
+    new_elements = _cairo_realloc_ab (array->elements,
 			              array->size, array->element_size);
 
     if (unlikely (new_elements == NULL)) {
@@ -157,7 +118,7 @@ _cairo_array_grow_by (cairo_array_t *array, unsigned int additional)
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
     }
 
-    *array->elements = new_elements;
+    array->elements = new_elements;
 
     return CAIRO_STATUS_SUCCESS;
 }
@@ -173,8 +134,6 @@ _cairo_array_grow_by (cairo_array_t *array, unsigned int additional)
 void
 _cairo_array_truncate (cairo_array_t *array, unsigned int num_elements)
 {
-    assert (! array->is_snapshot);
-
     if (num_elements < array->num_elements)
 	array->num_elements = num_elements;
 }
@@ -220,7 +179,7 @@ _cairo_array_index (cairo_array_t *array, unsigned int index)
 
     assert (index < array->num_elements);
 
-    return (void *) &(*array->elements)[index * array->element_size];
+    return array->elements + index * array->element_size;
 }
 
 /**
@@ -255,8 +214,6 @@ cairo_status_t
 _cairo_array_append (cairo_array_t	*array,
 		     const void		*element)
 {
-    assert (! array->is_snapshot);
-
     return _cairo_array_append_multiple (array, element, 1);
 }
 
@@ -280,8 +237,6 @@ _cairo_array_append_multiple (cairo_array_t	*array,
     cairo_status_t status;
     void *dest;
 
-    assert (! array->is_snapshot);
-
     status = _cairo_array_allocate (array, num_elements, &dest);
     if (unlikely (status))
 	return status;
@@ -311,15 +266,13 @@ _cairo_array_allocate (cairo_array_t	 *array,
 {
     cairo_status_t status;
 
-    assert (! array->is_snapshot);
-
     status = _cairo_array_grow_by (array, num_elements);
     if (unlikely (status))
 	return status;
 
     assert (array->num_elements + num_elements <= array->size);
 
-    *elements = &(*array->elements)[array->num_elements * array->element_size];
+    *elements = array->elements + array->num_elements * array->element_size;
 
     array->num_elements += num_elements;
 
diff --git a/src/cairo-types-private.h b/src/cairo-types-private.h
index 93b035d..b5e76c7 100644
--- a/src/cairo-types-private.h
+++ b/src/cairo-types-private.h
@@ -132,9 +132,7 @@ struct _cairo_array {
     unsigned int size;
     unsigned int num_elements;
     unsigned int element_size;
-    char **elements;
-
-    cairo_bool_t is_snapshot;
+    char *elements;
 };
 
 /**
diff --git a/src/cairoint.h b/src/cairoint.h
index 1510f8e..030c265 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -304,10 +304,6 @@ cairo_private void
 _cairo_array_init (cairo_array_t *array, int element_size);
 
 cairo_private void
-_cairo_array_init_snapshot (cairo_array_t	*array,
-			    const cairo_array_t *other);
-
-cairo_private void
 _cairo_array_fini (cairo_array_t *array);
 
 cairo_private cairo_status_t


More information about the cairo-commit mailing list